* elf-hppa.h (elf_hppa_reloc_final_type): Handle R_PARISC_GPREL64,
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
64d03ab5 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
74f0fb50 3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
252b5132 4
8fdd7217 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
8fdd7217
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
8fdd7217 10 (at your option) any later version.
252b5132 11
8fdd7217
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
8fdd7217
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.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
d98685ac
AM
32/* Define a symbol in a dynamic linkage section. */
33
34struct elf_link_hash_entry *
35_bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
37 asection *sec,
38 const char *name)
39{
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
ccabcbe5 42 const struct elf_backend_data *bed;
d98685ac
AM
43
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 if (h != NULL)
46 {
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
52 }
53
54 bh = &h->root;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 sec, 0, NULL, FALSE,
57 get_elf_backend_data (abfd)->collect,
58 &bh))
59 return NULL;
60 h = (struct elf_link_hash_entry *) bh;
61 h->def_regular = 1;
62 h->type = STT_OBJECT;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64
ccabcbe5
AM
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
67 return h;
68}
69
b34976b6 70bfd_boolean
268b6b39 71_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
72{
73 flagword flags;
aad5d350 74 asection *s;
252b5132 75 struct elf_link_hash_entry *h;
9c5bfbb7 76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
77 int ptralign;
78
79 /* This function may be called more than once. */
aad5d350
AM
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
b34976b6 82 return TRUE;
252b5132
RH
83
84 switch (bed->s->arch_size)
85 {
bb0deeff
AO
86 case 32:
87 ptralign = 2;
88 break;
89
90 case 64:
91 ptralign = 3;
92 break;
93
94 default:
95 bfd_set_error (bfd_error_bad_value);
b34976b6 96 return FALSE;
252b5132
RH
97 }
98
e5a52504 99 flags = bed->dynamic_sec_flags;
252b5132 100
3496cb2a 101 s = bfd_make_section_with_flags (abfd, ".got", flags);
252b5132 102 if (s == NULL
252b5132 103 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 104 return FALSE;
252b5132
RH
105
106 if (bed->want_got_plt)
107 {
3496cb2a 108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
252b5132 109 if (s == NULL
252b5132 110 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 111 return FALSE;
252b5132
RH
112 }
113
2517a57f
AM
114 if (bed->want_got_sym)
115 {
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
d98685ac 120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
2517a57f 121 elf_hash_table (info)->hgot = h;
d98685ac
AM
122 if (h == NULL)
123 return FALSE;
2517a57f 124 }
252b5132
RH
125
126 /* The first bit of the global offset table is the header. */
3b36f7e6 127 s->size += bed->got_header_size;
252b5132 128
b34976b6 129 return TRUE;
252b5132
RH
130}
131\f
7e9f0867
AM
132/* Create a strtab to hold the dynamic symbol names. */
133static bfd_boolean
134_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135{
136 struct elf_link_hash_table *hash_table;
137
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
141
142 if (hash_table->dynstr == NULL)
143 {
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
146 return FALSE;
147 }
148 return TRUE;
149}
150
45d6a902
AM
151/* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
252b5132 157
b34976b6 158bfd_boolean
268b6b39 159_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 160{
45d6a902
AM
161 flagword flags;
162 register asection *s;
9c5bfbb7 163 const struct elf_backend_data *bed;
252b5132 164
0eddce27 165 if (! is_elf_hash_table (info->hash))
45d6a902
AM
166 return FALSE;
167
168 if (elf_hash_table (info)->dynamic_sections_created)
169 return TRUE;
170
7e9f0867
AM
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 return FALSE;
45d6a902 173
7e9f0867 174 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
175 bed = get_elf_backend_data (abfd);
176
177 flags = bed->dynamic_sec_flags;
45d6a902
AM
178
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
36af4a4e 181 if (info->executable)
252b5132 182 {
3496cb2a
L
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
185 if (s == NULL)
45d6a902
AM
186 return FALSE;
187 }
bb0deeff 188
45d6a902
AM
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
3496cb2a
L
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
45d6a902 193 if (s == NULL
45d6a902
AM
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 return FALSE;
196
3496cb2a
L
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
45d6a902 199 if (s == NULL
45d6a902
AM
200 || ! bfd_set_section_alignment (abfd, s, 1))
201 return FALSE;
202
3496cb2a
L
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
45d6a902 205 if (s == NULL
45d6a902
AM
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
208
3496cb2a
L
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
45d6a902 211 if (s == NULL
45d6a902
AM
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
213 return FALSE;
214
3496cb2a
L
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
217 if (s == NULL)
45d6a902
AM
218 return FALSE;
219
3496cb2a 220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
45d6a902 221 if (s == NULL
45d6a902
AM
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 return FALSE;
224
225 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
d98685ac 231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
45d6a902
AM
232 return FALSE;
233
fdc90cb4
JJ
234 if (info->emit_hash)
235 {
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
241 }
242
243 if (info->emit_gnu_hash)
244 {
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 else
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
257 }
45d6a902
AM
258
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
263 return FALSE;
264
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
266
267 return TRUE;
268}
269
270/* Create dynamic sections when linking against a dynamic object. */
271
272bfd_boolean
268b6b39 273_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
274{
275 flagword flags, pltflags;
7325306f 276 struct elf_link_hash_entry *h;
45d6a902 277 asection *s;
9c5bfbb7 278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 279
252b5132
RH
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
e5a52504 282 flags = bed->dynamic_sec_flags;
252b5132
RH
283
284 pltflags = flags;
252b5132 285 if (bed->plt_not_loaded)
6df4d94c
MM
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
5d1634d7 289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
290 else
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
294
3496cb2a 295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
252b5132 296 if (s == NULL
252b5132 297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 298 return FALSE;
252b5132 299
d98685ac
AM
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 .plt section. */
7325306f
RS
302 if (bed->want_plt_sym)
303 {
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
307 if (h == NULL)
308 return FALSE;
309 }
252b5132 310
3496cb2a 311 s = bfd_make_section_with_flags (abfd,
d35fd659 312 (bed->rela_plts_and_copies_p
3496cb2a
L
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
252b5132 315 if (s == NULL
45d6a902 316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 317 return FALSE;
252b5132
RH
318
319 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 320 return FALSE;
252b5132 321
3018b441
RH
322 if (bed->want_dynbss)
323 {
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
3496cb2a
L
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 (SEC_ALLOC
332 | SEC_LINKER_CREATED));
333 if (s == NULL)
b34976b6 334 return FALSE;
252b5132 335
3018b441 336 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
346 copy relocs. */
3018b441
RH
347 if (! info->shared)
348 {
3496cb2a 349 s = bfd_make_section_with_flags (abfd,
d35fd659 350 (bed->rela_plts_and_copies_p
3496cb2a
L
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
3018b441 353 if (s == NULL
45d6a902 354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 355 return FALSE;
3018b441 356 }
252b5132
RH
357 }
358
b34976b6 359 return TRUE;
252b5132
RH
360}
361\f
252b5132
RH
362/* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
368 one. */
369
b34976b6 370bfd_boolean
c152c796
AM
371bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
252b5132
RH
373{
374 if (h->dynindx == -1)
375 {
2b0f7ef9 376 struct elf_strtab_hash *dynstr;
68b6ddd0 377 char *p;
252b5132 378 const char *name;
252b5132
RH
379 bfd_size_type indx;
380
7a13edea
NC
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
386 {
387 case STV_INTERNAL:
388 case STV_HIDDEN:
9d6eee78
L
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
38048eb9 391 {
f5385ebf 392 h->forced_local = 1;
67687978
PB
393 if (!elf_hash_table (info)->is_relocatable_executable)
394 return TRUE;
7a13edea 395 }
0444bdd4 396
7a13edea
NC
397 default:
398 break;
399 }
400
252b5132
RH
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
403
404 dynstr = elf_hash_table (info)->dynstr;
405 if (dynstr == NULL)
406 {
407 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 409 if (dynstr == NULL)
b34976b6 410 return FALSE;
252b5132
RH
411 }
412
413 /* We don't put any version information in the dynamic string
aad5d350 414 table. */
252b5132
RH
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
417 if (p != NULL)
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
423 *p = 0;
424
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
426
427 if (p != NULL)
428 *p = ELF_VER_CHR;
252b5132
RH
429
430 if (indx == (bfd_size_type) -1)
b34976b6 431 return FALSE;
252b5132
RH
432 h->dynstr_index = indx;
433 }
434
b34976b6 435 return TRUE;
252b5132 436}
45d6a902 437\f
55255dae
L
438/* Mark a symbol dynamic. */
439
440void
441bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
55255dae 444{
40b36307 445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 446
40b36307
L
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
55255dae
L
449 return;
450
40b36307
L
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
453 || (sym != NULL
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 455 || (d != NULL
40b36307
L
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
458 h->dynamic = 1;
459}
460
45d6a902
AM
461/* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
463
464bfd_boolean
fe21a8fc
L
465bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
268b6b39 467 const char *name,
fe21a8fc
L
468 bfd_boolean provide,
469 bfd_boolean hidden)
45d6a902 470{
00cbee0a 471 struct elf_link_hash_entry *h, *hv;
4ea42fb7 472 struct elf_link_hash_table *htab;
00cbee0a 473 const struct elf_backend_data *bed;
45d6a902 474
0eddce27 475 if (!is_elf_hash_table (info->hash))
45d6a902
AM
476 return TRUE;
477
4ea42fb7
AM
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 480 if (h == NULL)
4ea42fb7 481 return provide;
45d6a902 482
00cbee0a 483 switch (h->root.type)
77cfaee6 484 {
00cbee0a
L
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
488 break;
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
4ea42fb7 494 h->root.type = bfd_link_hash_new;
77cfaee6
AM
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
497 break;
498 case bfd_link_hash_new:
40b36307 499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 500 h->non_elf = 0;
00cbee0a
L
501 break;
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 504 the versioned symbol point to this one. */
00cbee0a
L
505 bed = get_elf_backend_data (output_bfd);
506 hv = h;
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
511 later. */
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
516 break;
517 case bfd_link_hash_warning:
518 abort ();
519 break;
55255dae 520 }
45d6a902
AM
521
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
526 if (provide
f5385ebf
AM
527 && h->def_dynamic
528 && !h->def_regular)
45d6a902
AM
529 h->root.type = bfd_link_hash_undefined;
530
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
535 if (!provide
f5385ebf
AM
536 && h->def_dynamic
537 && !h->def_regular)
45d6a902
AM
538 h->verinfo.verdef = NULL;
539
f5385ebf 540 h->def_regular = 1;
45d6a902 541
fe21a8fc
L
542 if (provide && hidden)
543 {
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
545
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
548 }
549
6fa3860b
PB
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
551 and executables. */
552 if (!info->relocatable
553 && h->dynindx != -1
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
556 h->forced_local = 1;
557
f5385ebf
AM
558 if ((h->def_dynamic
559 || h->ref_dynamic
67687978
PB
560 || info->shared
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
562 && h->dynindx == -1)
563 {
c152c796 564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
565 return FALSE;
566
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
f6e332e6
AM
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
45d6a902 572 {
f6e332e6 573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
574 return FALSE;
575 }
576 }
577
578 return TRUE;
579}
42751cf3 580
8c58d23b
AM
581/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
584
585int
c152c796
AM
586bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
587 bfd *input_bfd,
588 long input_indx)
8c58d23b
AM
589{
590 bfd_size_type amt;
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
595 char *name;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
598
0eddce27 599 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
600 return 0;
601
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
605 return 1;
606
607 amt = sizeof (*entry);
268b6b39 608 entry = bfd_alloc (input_bfd, amt);
8c58d23b
AM
609 if (entry == NULL)
610 return 0;
611
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 614 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
615 {
616 bfd_release (input_bfd, entry);
617 return 0;
618 }
619
620 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 621 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
622 {
623 asection *s;
624
625 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
626 if (s == NULL || bfd_is_abs_section (s->output_section))
627 {
628 /* We can still bfd_release here as nothing has done another
629 bfd_alloc. We can't do this later in this function. */
630 bfd_release (input_bfd, entry);
631 return 2;
632 }
633 }
634
635 name = (bfd_elf_string_from_elf_section
636 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
637 entry->isym.st_name));
638
639 dynstr = elf_hash_table (info)->dynstr;
640 if (dynstr == NULL)
641 {
642 /* Create a strtab to hold the dynamic symbol names. */
643 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
644 if (dynstr == NULL)
645 return 0;
646 }
647
b34976b6 648 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
649 if (dynstr_index == (unsigned long) -1)
650 return 0;
651 entry->isym.st_name = dynstr_index;
652
653 eht = elf_hash_table (info);
654
655 entry->next = eht->dynlocal;
656 eht->dynlocal = entry;
657 entry->input_bfd = input_bfd;
658 entry->input_indx = input_indx;
659 eht->dynsymcount++;
660
661 /* Whatever binding the symbol had before, it's now local. */
662 entry->isym.st_info
663 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
664
665 /* The dynindx will be set at the end of size_dynamic_sections. */
666
667 return 1;
668}
669
30b30c21 670/* Return the dynindex of a local dynamic symbol. */
42751cf3 671
30b30c21 672long
268b6b39
AM
673_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
674 bfd *input_bfd,
675 long input_indx)
30b30c21
RH
676{
677 struct elf_link_local_dynamic_entry *e;
678
679 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
680 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
681 return e->dynindx;
682 return -1;
683}
684
685/* This function is used to renumber the dynamic symbols, if some of
686 them are removed because they are marked as local. This is called
687 via elf_link_hash_traverse. */
688
b34976b6 689static bfd_boolean
268b6b39
AM
690elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
691 void *data)
42751cf3 692{
268b6b39 693 size_t *count = data;
30b30c21 694
e92d460e
AM
695 if (h->root.type == bfd_link_hash_warning)
696 h = (struct elf_link_hash_entry *) h->root.u.i.link;
697
6fa3860b
PB
698 if (h->forced_local)
699 return TRUE;
700
701 if (h->dynindx != -1)
702 h->dynindx = ++(*count);
703
704 return TRUE;
705}
706
707
708/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
709 STB_LOCAL binding. */
710
711static bfd_boolean
712elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
713 void *data)
714{
715 size_t *count = data;
716
717 if (h->root.type == bfd_link_hash_warning)
718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
719
720 if (!h->forced_local)
721 return TRUE;
722
42751cf3 723 if (h->dynindx != -1)
30b30c21
RH
724 h->dynindx = ++(*count);
725
b34976b6 726 return TRUE;
42751cf3 727}
30b30c21 728
aee6f5b4
AO
729/* Return true if the dynamic symbol for a given section should be
730 omitted when creating a shared library. */
731bfd_boolean
732_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
733 struct bfd_link_info *info,
734 asection *p)
735{
74541ad4
AM
736 struct elf_link_hash_table *htab;
737
aee6f5b4
AO
738 switch (elf_section_data (p)->this_hdr.sh_type)
739 {
740 case SHT_PROGBITS:
741 case SHT_NOBITS:
742 /* If sh_type is yet undecided, assume it could be
743 SHT_PROGBITS/SHT_NOBITS. */
744 case SHT_NULL:
74541ad4
AM
745 htab = elf_hash_table (info);
746 if (p == htab->tls_sec)
747 return FALSE;
748
749 if (htab->text_index_section != NULL)
750 return p != htab->text_index_section && p != htab->data_index_section;
751
aee6f5b4
AO
752 if (strcmp (p->name, ".got") == 0
753 || strcmp (p->name, ".got.plt") == 0
754 || strcmp (p->name, ".plt") == 0)
755 {
756 asection *ip;
aee6f5b4 757
74541ad4
AM
758 if (htab->dynobj != NULL
759 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
760 && (ip->flags & SEC_LINKER_CREATED)
761 && ip->output_section == p)
762 return TRUE;
763 }
764 return FALSE;
765
766 /* There shouldn't be section relative relocations
767 against any other section. */
768 default:
769 return TRUE;
770 }
771}
772
062e2358 773/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
774 symbol for each output section, which come first. Next come symbols
775 which have been forced to local binding. Then all of the back-end
776 allocated local dynamic syms, followed by the rest of the global
777 symbols. */
30b30c21 778
554220db
AM
779static unsigned long
780_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
781 struct bfd_link_info *info,
782 unsigned long *section_sym_count)
30b30c21
RH
783{
784 unsigned long dynsymcount = 0;
785
67687978 786 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 787 {
aee6f5b4 788 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
789 asection *p;
790 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 791 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
792 && (p->flags & SEC_ALLOC) != 0
793 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
794 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
795 else
796 elf_section_data (p)->dynindx = 0;
30b30c21 797 }
554220db 798 *section_sym_count = dynsymcount;
30b30c21 799
6fa3860b
PB
800 elf_link_hash_traverse (elf_hash_table (info),
801 elf_link_renumber_local_hash_table_dynsyms,
802 &dynsymcount);
803
30b30c21
RH
804 if (elf_hash_table (info)->dynlocal)
805 {
806 struct elf_link_local_dynamic_entry *p;
807 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
808 p->dynindx = ++dynsymcount;
809 }
810
811 elf_link_hash_traverse (elf_hash_table (info),
812 elf_link_renumber_hash_table_dynsyms,
813 &dynsymcount);
814
815 /* There is an unused NULL entry at the head of the table which
816 we must account for in our count. Unless there weren't any
817 symbols, which means we'll have no table at all. */
818 if (dynsymcount != 0)
819 ++dynsymcount;
820
ccabcbe5
AM
821 elf_hash_table (info)->dynsymcount = dynsymcount;
822 return dynsymcount;
30b30c21 823}
252b5132 824
45d6a902
AM
825/* This function is called when we want to define a new symbol. It
826 handles the various cases which arise when we find a definition in
827 a dynamic object, or when there is already a definition in a
828 dynamic object. The new symbol is described by NAME, SYM, PSEC,
829 and PVALUE. We set SYM_HASH to the hash table entry. We set
830 OVERRIDE if the old symbol is overriding a new definition. We set
831 TYPE_CHANGE_OK if it is OK for the type to change. We set
832 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
833 change, we mean that we shouldn't warn if the type or size does
af44c138
L
834 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
835 object is overridden by a regular object. */
45d6a902
AM
836
837bfd_boolean
268b6b39
AM
838_bfd_elf_merge_symbol (bfd *abfd,
839 struct bfd_link_info *info,
840 const char *name,
841 Elf_Internal_Sym *sym,
842 asection **psec,
843 bfd_vma *pvalue,
af44c138 844 unsigned int *pold_alignment,
268b6b39
AM
845 struct elf_link_hash_entry **sym_hash,
846 bfd_boolean *skip,
847 bfd_boolean *override,
848 bfd_boolean *type_change_ok,
0f8a2703 849 bfd_boolean *size_change_ok)
252b5132 850{
7479dfd4 851 asection *sec, *oldsec;
45d6a902
AM
852 struct elf_link_hash_entry *h;
853 struct elf_link_hash_entry *flip;
854 int bind;
855 bfd *oldbfd;
856 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
77cfaee6 857 bfd_boolean newweak, oldweak;
a4d8e49b 858 const struct elf_backend_data *bed;
45d6a902
AM
859
860 *skip = FALSE;
861 *override = FALSE;
862
863 sec = *psec;
864 bind = ELF_ST_BIND (sym->st_info);
865
cd7be95b
KH
866 /* Silently discard TLS symbols from --just-syms. There's no way to
867 combine a static TLS block with a new TLS block for this executable. */
868 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
869 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
870 {
871 *skip = TRUE;
872 return TRUE;
873 }
874
45d6a902
AM
875 if (! bfd_is_und_section (sec))
876 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
877 else
878 h = ((struct elf_link_hash_entry *)
879 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
880 if (h == NULL)
881 return FALSE;
882 *sym_hash = h;
252b5132 883
88ba32a0
L
884 bed = get_elf_backend_data (abfd);
885
45d6a902
AM
886 /* This code is for coping with dynamic objects, and is only useful
887 if we are doing an ELF link. */
88ba32a0 888 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
45d6a902 889 return TRUE;
252b5132 890
45d6a902
AM
891 /* For merging, we only care about real symbols. */
892
893 while (h->root.type == bfd_link_hash_indirect
894 || h->root.type == bfd_link_hash_warning)
895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
896
40b36307
L
897 /* We have to check it for every instance since the first few may be
898 refereences and not all compilers emit symbol type for undefined
899 symbols. */
900 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
901
45d6a902
AM
902 /* If we just created the symbol, mark it as being an ELF symbol.
903 Other than that, there is nothing to do--there is no merge issue
904 with a newly defined symbol--so we just return. */
905
906 if (h->root.type == bfd_link_hash_new)
252b5132 907 {
f5385ebf 908 h->non_elf = 0;
45d6a902
AM
909 return TRUE;
910 }
252b5132 911
7479dfd4
L
912 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
913 existing symbol. */
252b5132 914
45d6a902
AM
915 switch (h->root.type)
916 {
917 default:
918 oldbfd = NULL;
7479dfd4 919 oldsec = NULL;
45d6a902 920 break;
252b5132 921
45d6a902
AM
922 case bfd_link_hash_undefined:
923 case bfd_link_hash_undefweak:
924 oldbfd = h->root.u.undef.abfd;
7479dfd4 925 oldsec = NULL;
45d6a902
AM
926 break;
927
928 case bfd_link_hash_defined:
929 case bfd_link_hash_defweak:
930 oldbfd = h->root.u.def.section->owner;
7479dfd4 931 oldsec = h->root.u.def.section;
45d6a902
AM
932 break;
933
934 case bfd_link_hash_common:
935 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 936 oldsec = h->root.u.c.p->section;
45d6a902
AM
937 break;
938 }
939
940 /* In cases involving weak versioned symbols, we may wind up trying
941 to merge a symbol with itself. Catch that here, to avoid the
942 confusion that results if we try to override a symbol with
943 itself. The additional tests catch cases like
944 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
945 dynamic object, which we do want to handle here. */
946 if (abfd == oldbfd
947 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 948 || !h->def_regular))
45d6a902
AM
949 return TRUE;
950
951 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
952 respectively, is from a dynamic object. */
953
707bba77 954 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 955
707bba77 956 olddyn = FALSE;
45d6a902
AM
957 if (oldbfd != NULL)
958 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 959 else if (oldsec != NULL)
45d6a902 960 {
707bba77 961 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 962 indices used by MIPS ELF. */
707bba77 963 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 964 }
252b5132 965
45d6a902
AM
966 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
967 respectively, appear to be a definition rather than reference. */
968
707bba77 969 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 970
707bba77
AM
971 olddef = (h->root.type != bfd_link_hash_undefined
972 && h->root.type != bfd_link_hash_undefweak
973 && h->root.type != bfd_link_hash_common);
45d6a902 974
580a2b6e
L
975 /* When we try to create a default indirect symbol from the dynamic
976 definition with the default version, we skip it if its type and
977 the type of existing regular definition mismatch. We only do it
978 if the existing regular definition won't be dynamic. */
979 if (pold_alignment == NULL
980 && !info->shared
981 && !info->export_dynamic
982 && !h->ref_dynamic
983 && newdyn
984 && newdef
985 && !olddyn
986 && (olddef || h->root.type == bfd_link_hash_common)
987 && ELF_ST_TYPE (sym->st_info) != h->type
988 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf
PB
989 && h->type != STT_NOTYPE
990 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
991 && bed->is_function_type (h->type)))
580a2b6e
L
992 {
993 *skip = TRUE;
994 return TRUE;
995 }
996
68f49ba3
L
997 /* Check TLS symbol. We don't check undefined symbol introduced by
998 "ld -u". */
7479dfd4 999 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
68f49ba3
L
1000 && ELF_ST_TYPE (sym->st_info) != h->type
1001 && oldbfd != NULL)
7479dfd4
L
1002 {
1003 bfd *ntbfd, *tbfd;
1004 bfd_boolean ntdef, tdef;
1005 asection *ntsec, *tsec;
1006
1007 if (h->type == STT_TLS)
1008 {
3b36f7e6 1009 ntbfd = abfd;
7479dfd4
L
1010 ntsec = sec;
1011 ntdef = newdef;
1012 tbfd = oldbfd;
1013 tsec = oldsec;
1014 tdef = olddef;
1015 }
1016 else
1017 {
1018 ntbfd = oldbfd;
1019 ntsec = oldsec;
1020 ntdef = olddef;
1021 tbfd = abfd;
1022 tsec = sec;
1023 tdef = newdef;
1024 }
1025
1026 if (tdef && ntdef)
1027 (*_bfd_error_handler)
1028 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1029 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1030 else if (!tdef && !ntdef)
1031 (*_bfd_error_handler)
1032 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1033 tbfd, ntbfd, h->root.root.string);
1034 else if (tdef)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1037 tbfd, tsec, ntbfd, h->root.root.string);
1038 else
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1041 tbfd, ntbfd, ntsec, h->root.root.string);
1042
1043 bfd_set_error (bfd_error_bad_value);
1044 return FALSE;
1045 }
1046
4cc11e76 1047 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1048 object or is weak in all dynamic objects. Internal and hidden
1049 visibility will make it unavailable to dynamic objects. */
f5385ebf 1050 if (newdyn && !h->dynamic_def)
45d6a902
AM
1051 {
1052 if (!bfd_is_und_section (sec))
f5385ebf 1053 h->dynamic_def = 1;
45d6a902 1054 else
252b5132 1055 {
45d6a902
AM
1056 /* Check if this symbol is weak in all dynamic objects. If it
1057 is the first time we see it in a dynamic object, we mark
1058 if it is weak. Otherwise, we clear it. */
f5385ebf 1059 if (!h->ref_dynamic)
79349b09 1060 {
45d6a902 1061 if (bind == STB_WEAK)
f5385ebf 1062 h->dynamic_weak = 1;
252b5132 1063 }
45d6a902 1064 else if (bind != STB_WEAK)
f5385ebf 1065 h->dynamic_weak = 0;
252b5132 1066 }
45d6a902 1067 }
252b5132 1068
45d6a902
AM
1069 /* If the old symbol has non-default visibility, we ignore the new
1070 definition from a dynamic object. */
1071 if (newdyn
9c7a29a3 1072 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1073 && !bfd_is_und_section (sec))
1074 {
1075 *skip = TRUE;
1076 /* Make sure this symbol is dynamic. */
f5385ebf 1077 h->ref_dynamic = 1;
45d6a902
AM
1078 /* A protected symbol has external availability. Make sure it is
1079 recorded as dynamic.
1080
1081 FIXME: Should we check type and size for protected symbol? */
1082 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1083 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1084 else
1085 return TRUE;
1086 }
1087 else if (!newdyn
9c7a29a3 1088 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1089 && h->def_dynamic)
45d6a902
AM
1090 {
1091 /* If the new symbol with non-default visibility comes from a
1092 relocatable file and the old definition comes from a dynamic
1093 object, we remove the old definition. */
1094 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1095 {
1096 /* Handle the case where the old dynamic definition is
1097 default versioned. We need to copy the symbol info from
1098 the symbol with default version to the normal one if it
1099 was referenced before. */
1100 if (h->ref_regular)
1101 {
1102 const struct elf_backend_data *bed
1103 = get_elf_backend_data (abfd);
1104 struct elf_link_hash_entry *vh = *sym_hash;
1105 vh->root.type = h->root.type;
1106 h->root.type = bfd_link_hash_indirect;
1107 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1108 /* Protected symbols will override the dynamic definition
1109 with default version. */
1110 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1111 {
1112 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1113 vh->dynamic_def = 1;
1114 vh->ref_dynamic = 1;
1115 }
1116 else
1117 {
1118 h->root.type = vh->root.type;
1119 vh->ref_dynamic = 0;
1120 /* We have to hide it here since it was made dynamic
1121 global with extra bits when the symbol info was
1122 copied from the old dynamic definition. */
1123 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1124 }
1125 h = vh;
1126 }
1127 else
1128 h = *sym_hash;
1129 }
1de1a317 1130
f6e332e6 1131 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317
L
1132 && bfd_is_und_section (sec))
1133 {
1134 /* If the new symbol is undefined and the old symbol was
1135 also undefined before, we need to make sure
1136 _bfd_generic_link_add_one_symbol doesn't mess
f6e332e6 1137 up the linker hash table undefs list. Since the old
1de1a317
L
1138 definition came from a dynamic object, it is still on the
1139 undefs list. */
1140 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1141 h->root.u.undef.abfd = abfd;
1142 }
1143 else
1144 {
1145 h->root.type = bfd_link_hash_new;
1146 h->root.u.undef.abfd = NULL;
1147 }
1148
f5385ebf 1149 if (h->def_dynamic)
252b5132 1150 {
f5385ebf
AM
1151 h->def_dynamic = 0;
1152 h->ref_dynamic = 1;
1153 h->dynamic_def = 1;
45d6a902
AM
1154 }
1155 /* FIXME: Should we check type and size for protected symbol? */
1156 h->size = 0;
1157 h->type = 0;
1158 return TRUE;
1159 }
14a793b2 1160
79349b09
AM
1161 /* Differentiate strong and weak symbols. */
1162 newweak = bind == STB_WEAK;
1163 oldweak = (h->root.type == bfd_link_hash_defweak
1164 || h->root.type == bfd_link_hash_undefweak);
14a793b2 1165
15b43f48
AM
1166 /* If a new weak symbol definition comes from a regular file and the
1167 old symbol comes from a dynamic library, we treat the new one as
1168 strong. Similarly, an old weak symbol definition from a regular
1169 file is treated as strong when the new symbol comes from a dynamic
1170 library. Further, an old weak symbol from a dynamic library is
1171 treated as strong if the new symbol is from a dynamic library.
1172 This reflects the way glibc's ld.so works.
1173
1174 Do this before setting *type_change_ok or *size_change_ok so that
1175 we warn properly when dynamic library symbols are overridden. */
1176
1177 if (newdef && !newdyn && olddyn)
0f8a2703 1178 newweak = FALSE;
15b43f48 1179 if (olddef && newdyn)
0f8a2703
AM
1180 oldweak = FALSE;
1181
fcb93ecf
PB
1182 /* Allow changes between different types of funciton symbol. */
1183 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1184 && bed->is_function_type (h->type))
1185 *type_change_ok = TRUE;
1186
79349b09
AM
1187 /* It's OK to change the type if either the existing symbol or the
1188 new symbol is weak. A type change is also OK if the old symbol
1189 is undefined and the new symbol is defined. */
252b5132 1190
79349b09
AM
1191 if (oldweak
1192 || newweak
1193 || (newdef
1194 && h->root.type == bfd_link_hash_undefined))
1195 *type_change_ok = TRUE;
1196
1197 /* It's OK to change the size if either the existing symbol or the
1198 new symbol is weak, or if the old symbol is undefined. */
1199
1200 if (*type_change_ok
1201 || h->root.type == bfd_link_hash_undefined)
1202 *size_change_ok = TRUE;
45d6a902 1203
45d6a902
AM
1204 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1205 symbol, respectively, appears to be a common symbol in a dynamic
1206 object. If a symbol appears in an uninitialized section, and is
1207 not weak, and is not a function, then it may be a common symbol
1208 which was resolved when the dynamic object was created. We want
1209 to treat such symbols specially, because they raise special
1210 considerations when setting the symbol size: if the symbol
1211 appears as a common symbol in a regular object, and the size in
1212 the regular object is larger, we must make sure that we use the
1213 larger size. This problematic case can always be avoided in C,
1214 but it must be handled correctly when using Fortran shared
1215 libraries.
1216
1217 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1218 likewise for OLDDYNCOMMON and OLDDEF.
1219
1220 Note that this test is just a heuristic, and that it is quite
1221 possible to have an uninitialized symbol in a shared object which
1222 is really a definition, rather than a common symbol. This could
1223 lead to some minor confusion when the symbol really is a common
1224 symbol in some regular object. However, I think it will be
1225 harmless. */
1226
1227 if (newdyn
1228 && newdef
79349b09 1229 && !newweak
45d6a902
AM
1230 && (sec->flags & SEC_ALLOC) != 0
1231 && (sec->flags & SEC_LOAD) == 0
1232 && sym->st_size > 0
fcb93ecf 1233 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
45d6a902
AM
1234 newdyncommon = TRUE;
1235 else
1236 newdyncommon = FALSE;
1237
1238 if (olddyn
1239 && olddef
1240 && h->root.type == bfd_link_hash_defined
f5385ebf 1241 && h->def_dynamic
45d6a902
AM
1242 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1243 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1244 && h->size > 0
fcb93ecf 1245 && !bed->is_function_type (h->type))
45d6a902
AM
1246 olddyncommon = TRUE;
1247 else
1248 olddyncommon = FALSE;
1249
a4d8e49b
L
1250 /* We now know everything about the old and new symbols. We ask the
1251 backend to check if we can merge them. */
a4d8e49b
L
1252 if (bed->merge_symbol
1253 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1254 pold_alignment, skip, override,
1255 type_change_ok, size_change_ok,
1256 &newdyn, &newdef, &newdyncommon, &newweak,
1257 abfd, &sec,
1258 &olddyn, &olddef, &olddyncommon, &oldweak,
1259 oldbfd, &oldsec))
1260 return FALSE;
1261
45d6a902
AM
1262 /* If both the old and the new symbols look like common symbols in a
1263 dynamic object, set the size of the symbol to the larger of the
1264 two. */
1265
1266 if (olddyncommon
1267 && newdyncommon
1268 && sym->st_size != h->size)
1269 {
1270 /* Since we think we have two common symbols, issue a multiple
1271 common warning if desired. Note that we only warn if the
1272 size is different. If the size is the same, we simply let
1273 the old symbol override the new one as normally happens with
1274 symbols defined in dynamic objects. */
1275
1276 if (! ((*info->callbacks->multiple_common)
1277 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1278 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1279 return FALSE;
252b5132 1280
45d6a902
AM
1281 if (sym->st_size > h->size)
1282 h->size = sym->st_size;
252b5132 1283
45d6a902 1284 *size_change_ok = TRUE;
252b5132
RH
1285 }
1286
45d6a902
AM
1287 /* If we are looking at a dynamic object, and we have found a
1288 definition, we need to see if the symbol was already defined by
1289 some other object. If so, we want to use the existing
1290 definition, and we do not want to report a multiple symbol
1291 definition error; we do this by clobbering *PSEC to be
1292 bfd_und_section_ptr.
1293
1294 We treat a common symbol as a definition if the symbol in the
1295 shared library is a function, since common symbols always
1296 represent variables; this can cause confusion in principle, but
1297 any such confusion would seem to indicate an erroneous program or
1298 shared library. We also permit a common symbol in a regular
79349b09 1299 object to override a weak symbol in a shared object. */
45d6a902
AM
1300
1301 if (newdyn
1302 && newdef
77cfaee6 1303 && (olddef
45d6a902 1304 || (h->root.type == bfd_link_hash_common
79349b09 1305 && (newweak
fcb93ecf 1306 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
45d6a902
AM
1307 {
1308 *override = TRUE;
1309 newdef = FALSE;
1310 newdyncommon = FALSE;
252b5132 1311
45d6a902
AM
1312 *psec = sec = bfd_und_section_ptr;
1313 *size_change_ok = TRUE;
252b5132 1314
45d6a902
AM
1315 /* If we get here when the old symbol is a common symbol, then
1316 we are explicitly letting it override a weak symbol or
1317 function in a dynamic object, and we don't want to warn about
1318 a type change. If the old symbol is a defined symbol, a type
1319 change warning may still be appropriate. */
252b5132 1320
45d6a902
AM
1321 if (h->root.type == bfd_link_hash_common)
1322 *type_change_ok = TRUE;
1323 }
1324
1325 /* Handle the special case of an old common symbol merging with a
1326 new symbol which looks like a common symbol in a shared object.
1327 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1328 common symbol, and let _bfd_generic_link_add_one_symbol do the
1329 right thing. */
45d6a902
AM
1330
1331 if (newdyncommon
1332 && h->root.type == bfd_link_hash_common)
1333 {
1334 *override = TRUE;
1335 newdef = FALSE;
1336 newdyncommon = FALSE;
1337 *pvalue = sym->st_size;
a4d8e49b 1338 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1339 *size_change_ok = TRUE;
1340 }
1341
c5e2cead 1342 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1343 if (newdef && olddef && newweak)
c5e2cead
L
1344 *skip = TRUE;
1345
45d6a902
AM
1346 /* If the old symbol is from a dynamic object, and the new symbol is
1347 a definition which is not from a dynamic object, then the new
1348 symbol overrides the old symbol. Symbols from regular files
1349 always take precedence over symbols from dynamic objects, even if
1350 they are defined after the dynamic object in the link.
1351
1352 As above, we again permit a common symbol in a regular object to
1353 override a definition in a shared object if the shared object
0f8a2703 1354 symbol is a function or is weak. */
45d6a902
AM
1355
1356 flip = NULL;
77cfaee6 1357 if (!newdyn
45d6a902
AM
1358 && (newdef
1359 || (bfd_is_com_section (sec)
79349b09 1360 && (oldweak
fcb93ecf 1361 || bed->is_function_type (h->type))))
45d6a902
AM
1362 && olddyn
1363 && olddef
f5385ebf 1364 && h->def_dynamic)
45d6a902
AM
1365 {
1366 /* Change the hash table entry to undefined, and let
1367 _bfd_generic_link_add_one_symbol do the right thing with the
1368 new definition. */
1369
1370 h->root.type = bfd_link_hash_undefined;
1371 h->root.u.undef.abfd = h->root.u.def.section->owner;
1372 *size_change_ok = TRUE;
1373
1374 olddef = FALSE;
1375 olddyncommon = FALSE;
1376
1377 /* We again permit a type change when a common symbol may be
1378 overriding a function. */
1379
1380 if (bfd_is_com_section (sec))
1381 *type_change_ok = TRUE;
1382
1383 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1384 flip = *sym_hash;
1385 else
1386 /* This union may have been set to be non-NULL when this symbol
1387 was seen in a dynamic object. We must force the union to be
1388 NULL, so that it is correct for a regular symbol. */
1389 h->verinfo.vertree = NULL;
1390 }
1391
1392 /* Handle the special case of a new common symbol merging with an
1393 old symbol that looks like it might be a common symbol defined in
1394 a shared object. Note that we have already handled the case in
1395 which a new common symbol should simply override the definition
1396 in the shared library. */
1397
1398 if (! newdyn
1399 && bfd_is_com_section (sec)
1400 && olddyncommon)
1401 {
1402 /* It would be best if we could set the hash table entry to a
1403 common symbol, but we don't know what to use for the section
1404 or the alignment. */
1405 if (! ((*info->callbacks->multiple_common)
1406 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1407 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1408 return FALSE;
1409
4cc11e76 1410 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1411 larger, pretend that the new symbol has its size. */
1412
1413 if (h->size > *pvalue)
1414 *pvalue = h->size;
1415
af44c138
L
1416 /* We need to remember the alignment required by the symbol
1417 in the dynamic object. */
1418 BFD_ASSERT (pold_alignment);
1419 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1420
1421 olddef = FALSE;
1422 olddyncommon = FALSE;
1423
1424 h->root.type = bfd_link_hash_undefined;
1425 h->root.u.undef.abfd = h->root.u.def.section->owner;
1426
1427 *size_change_ok = TRUE;
1428 *type_change_ok = TRUE;
1429
1430 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1431 flip = *sym_hash;
1432 else
1433 h->verinfo.vertree = NULL;
1434 }
1435
1436 if (flip != NULL)
1437 {
1438 /* Handle the case where we had a versioned symbol in a dynamic
1439 library and now find a definition in a normal object. In this
1440 case, we make the versioned symbol point to the normal one. */
9c5bfbb7 1441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 1442 flip->root.type = h->root.type;
00cbee0a 1443 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1444 h->root.type = bfd_link_hash_indirect;
1445 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1446 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1447 if (h->def_dynamic)
45d6a902 1448 {
f5385ebf
AM
1449 h->def_dynamic = 0;
1450 flip->ref_dynamic = 1;
45d6a902
AM
1451 }
1452 }
1453
45d6a902
AM
1454 return TRUE;
1455}
1456
1457/* This function is called to create an indirect symbol from the
1458 default for the symbol with the default version if needed. The
1459 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1460 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902
AM
1461
1462bfd_boolean
268b6b39
AM
1463_bfd_elf_add_default_symbol (bfd *abfd,
1464 struct bfd_link_info *info,
1465 struct elf_link_hash_entry *h,
1466 const char *name,
1467 Elf_Internal_Sym *sym,
1468 asection **psec,
1469 bfd_vma *value,
1470 bfd_boolean *dynsym,
0f8a2703 1471 bfd_boolean override)
45d6a902
AM
1472{
1473 bfd_boolean type_change_ok;
1474 bfd_boolean size_change_ok;
1475 bfd_boolean skip;
1476 char *shortname;
1477 struct elf_link_hash_entry *hi;
1478 struct bfd_link_hash_entry *bh;
9c5bfbb7 1479 const struct elf_backend_data *bed;
45d6a902
AM
1480 bfd_boolean collect;
1481 bfd_boolean dynamic;
1482 char *p;
1483 size_t len, shortlen;
1484 asection *sec;
1485
1486 /* If this symbol has a version, and it is the default version, we
1487 create an indirect symbol from the default name to the fully
1488 decorated name. This will cause external references which do not
1489 specify a version to be bound to this version of the symbol. */
1490 p = strchr (name, ELF_VER_CHR);
1491 if (p == NULL || p[1] != ELF_VER_CHR)
1492 return TRUE;
1493
1494 if (override)
1495 {
4cc11e76 1496 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1497 need to create the indirect symbol from the default name. */
1498 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1499 FALSE, FALSE);
1500 BFD_ASSERT (hi != NULL);
1501 if (hi == h)
1502 return TRUE;
1503 while (hi->root.type == bfd_link_hash_indirect
1504 || hi->root.type == bfd_link_hash_warning)
1505 {
1506 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1507 if (hi == h)
1508 return TRUE;
1509 }
1510 }
1511
1512 bed = get_elf_backend_data (abfd);
1513 collect = bed->collect;
1514 dynamic = (abfd->flags & DYNAMIC) != 0;
1515
1516 shortlen = p - name;
1517 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1518 if (shortname == NULL)
1519 return FALSE;
1520 memcpy (shortname, name, shortlen);
1521 shortname[shortlen] = '\0';
1522
1523 /* We are going to create a new symbol. Merge it with any existing
1524 symbol with this name. For the purposes of the merge, act as
1525 though we were defining the symbol we just defined, although we
1526 actually going to define an indirect symbol. */
1527 type_change_ok = FALSE;
1528 size_change_ok = FALSE;
1529 sec = *psec;
1530 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1531 NULL, &hi, &skip, &override,
1532 &type_change_ok, &size_change_ok))
45d6a902
AM
1533 return FALSE;
1534
1535 if (skip)
1536 goto nondefault;
1537
1538 if (! override)
1539 {
1540 bh = &hi->root;
1541 if (! (_bfd_generic_link_add_one_symbol
1542 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1543 0, name, FALSE, collect, &bh)))
45d6a902
AM
1544 return FALSE;
1545 hi = (struct elf_link_hash_entry *) bh;
1546 }
1547 else
1548 {
1549 /* In this case the symbol named SHORTNAME is overriding the
1550 indirect symbol we want to add. We were planning on making
1551 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1552 is the name without a version. NAME is the fully versioned
1553 name, and it is the default version.
1554
1555 Overriding means that we already saw a definition for the
1556 symbol SHORTNAME in a regular object, and it is overriding
1557 the symbol defined in the dynamic object.
1558
1559 When this happens, we actually want to change NAME, the
1560 symbol we just added, to refer to SHORTNAME. This will cause
1561 references to NAME in the shared object to become references
1562 to SHORTNAME in the regular object. This is what we expect
1563 when we override a function in a shared object: that the
1564 references in the shared object will be mapped to the
1565 definition in the regular object. */
1566
1567 while (hi->root.type == bfd_link_hash_indirect
1568 || hi->root.type == bfd_link_hash_warning)
1569 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1570
1571 h->root.type = bfd_link_hash_indirect;
1572 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1573 if (h->def_dynamic)
45d6a902 1574 {
f5385ebf
AM
1575 h->def_dynamic = 0;
1576 hi->ref_dynamic = 1;
1577 if (hi->ref_regular
1578 || hi->def_regular)
45d6a902 1579 {
c152c796 1580 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1581 return FALSE;
1582 }
1583 }
1584
1585 /* Now set HI to H, so that the following code will set the
1586 other fields correctly. */
1587 hi = h;
1588 }
1589
fab4a87f
L
1590 /* Check if HI is a warning symbol. */
1591 if (hi->root.type == bfd_link_hash_warning)
1592 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1593
45d6a902
AM
1594 /* If there is a duplicate definition somewhere, then HI may not
1595 point to an indirect symbol. We will have reported an error to
1596 the user in that case. */
1597
1598 if (hi->root.type == bfd_link_hash_indirect)
1599 {
1600 struct elf_link_hash_entry *ht;
1601
45d6a902 1602 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1603 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902
AM
1604
1605 /* See if the new flags lead us to realize that the symbol must
1606 be dynamic. */
1607 if (! *dynsym)
1608 {
1609 if (! dynamic)
1610 {
1611 if (info->shared
f5385ebf 1612 || hi->ref_dynamic)
45d6a902
AM
1613 *dynsym = TRUE;
1614 }
1615 else
1616 {
f5385ebf 1617 if (hi->ref_regular)
45d6a902
AM
1618 *dynsym = TRUE;
1619 }
1620 }
1621 }
1622
1623 /* We also need to define an indirection from the nondefault version
1624 of the symbol. */
1625
1626nondefault:
1627 len = strlen (name);
1628 shortname = bfd_hash_allocate (&info->hash->table, len);
1629 if (shortname == NULL)
1630 return FALSE;
1631 memcpy (shortname, name, shortlen);
1632 memcpy (shortname + shortlen, p + 1, len - shortlen);
1633
1634 /* Once again, merge with any existing symbol. */
1635 type_change_ok = FALSE;
1636 size_change_ok = FALSE;
1637 sec = *psec;
1638 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1639 NULL, &hi, &skip, &override,
1640 &type_change_ok, &size_change_ok))
45d6a902
AM
1641 return FALSE;
1642
1643 if (skip)
1644 return TRUE;
1645
1646 if (override)
1647 {
1648 /* Here SHORTNAME is a versioned name, so we don't expect to see
1649 the type of override we do in the case above unless it is
4cc11e76 1650 overridden by a versioned definition. */
45d6a902
AM
1651 if (hi->root.type != bfd_link_hash_defined
1652 && hi->root.type != bfd_link_hash_defweak)
1653 (*_bfd_error_handler)
d003868e
AM
1654 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1655 abfd, shortname);
45d6a902
AM
1656 }
1657 else
1658 {
1659 bh = &hi->root;
1660 if (! (_bfd_generic_link_add_one_symbol
1661 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1662 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1663 return FALSE;
1664 hi = (struct elf_link_hash_entry *) bh;
1665
1666 /* If there is a duplicate definition somewhere, then HI may not
1667 point to an indirect symbol. We will have reported an error
1668 to the user in that case. */
1669
1670 if (hi->root.type == bfd_link_hash_indirect)
1671 {
fcfa13d2 1672 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
45d6a902
AM
1673
1674 /* See if the new flags lead us to realize that the symbol
1675 must be dynamic. */
1676 if (! *dynsym)
1677 {
1678 if (! dynamic)
1679 {
1680 if (info->shared
f5385ebf 1681 || hi->ref_dynamic)
45d6a902
AM
1682 *dynsym = TRUE;
1683 }
1684 else
1685 {
f5385ebf 1686 if (hi->ref_regular)
45d6a902
AM
1687 *dynsym = TRUE;
1688 }
1689 }
1690 }
1691 }
1692
1693 return TRUE;
1694}
1695\f
1696/* This routine is used to export all defined symbols into the dynamic
1697 symbol table. It is called via elf_link_hash_traverse. */
1698
1699bfd_boolean
268b6b39 1700_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1701{
268b6b39 1702 struct elf_info_failed *eif = data;
45d6a902 1703
55255dae
L
1704 /* Ignore this if we won't export it. */
1705 if (!eif->info->export_dynamic && !h->dynamic)
1706 return TRUE;
1707
45d6a902
AM
1708 /* Ignore indirect symbols. These are added by the versioning code. */
1709 if (h->root.type == bfd_link_hash_indirect)
1710 return TRUE;
1711
1712 if (h->root.type == bfd_link_hash_warning)
1713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1714
1715 if (h->dynindx == -1
f5385ebf
AM
1716 && (h->def_regular
1717 || h->ref_regular))
45d6a902
AM
1718 {
1719 struct bfd_elf_version_tree *t;
1720 struct bfd_elf_version_expr *d;
1721
1722 for (t = eif->verdefs; t != NULL; t = t->next)
1723 {
108ba305 1724 if (t->globals.list != NULL)
45d6a902 1725 {
108ba305
JJ
1726 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1727 if (d != NULL)
1728 goto doit;
45d6a902
AM
1729 }
1730
108ba305 1731 if (t->locals.list != NULL)
45d6a902 1732 {
108ba305
JJ
1733 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1734 if (d != NULL)
1735 return TRUE;
45d6a902
AM
1736 }
1737 }
1738
1739 if (!eif->verdefs)
1740 {
1741 doit:
c152c796 1742 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1743 {
1744 eif->failed = TRUE;
1745 return FALSE;
1746 }
1747 }
1748 }
1749
1750 return TRUE;
1751}
1752\f
1753/* Look through the symbols which are defined in other shared
1754 libraries and referenced here. Update the list of version
1755 dependencies. This will be put into the .gnu.version_r section.
1756 This function is called via elf_link_hash_traverse. */
1757
1758bfd_boolean
268b6b39
AM
1759_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1760 void *data)
45d6a902 1761{
268b6b39 1762 struct elf_find_verdep_info *rinfo = data;
45d6a902
AM
1763 Elf_Internal_Verneed *t;
1764 Elf_Internal_Vernaux *a;
1765 bfd_size_type amt;
1766
1767 if (h->root.type == bfd_link_hash_warning)
1768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1769
1770 /* We only care about symbols defined in shared objects with version
1771 information. */
f5385ebf
AM
1772 if (!h->def_dynamic
1773 || h->def_regular
45d6a902
AM
1774 || h->dynindx == -1
1775 || h->verinfo.verdef == NULL)
1776 return TRUE;
1777
1778 /* See if we already know about this version. */
1779 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1780 {
1781 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1782 continue;
1783
1784 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1785 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1786 return TRUE;
1787
1788 break;
1789 }
1790
1791 /* This is a new version. Add it to tree we are building. */
1792
1793 if (t == NULL)
1794 {
1795 amt = sizeof *t;
268b6b39 1796 t = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1797 if (t == NULL)
1798 {
1799 rinfo->failed = TRUE;
1800 return FALSE;
1801 }
1802
1803 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1804 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1805 elf_tdata (rinfo->output_bfd)->verref = t;
1806 }
1807
1808 amt = sizeof *a;
268b6b39 1809 a = bfd_zalloc (rinfo->output_bfd, amt);
14b1c01e
AM
1810 if (a == NULL)
1811 {
1812 rinfo->failed = TRUE;
1813 return FALSE;
1814 }
45d6a902
AM
1815
1816 /* Note that we are copying a string pointer here, and testing it
1817 above. If bfd_elf_string_from_elf_section is ever changed to
1818 discard the string data when low in memory, this will have to be
1819 fixed. */
1820 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1821
1822 a->vna_flags = h->verinfo.verdef->vd_flags;
1823 a->vna_nextptr = t->vn_auxptr;
1824
1825 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1826 ++rinfo->vers;
1827
1828 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1829
1830 t->vn_auxptr = a;
1831
1832 return TRUE;
1833}
1834
1835/* Figure out appropriate versions for all the symbols. We may not
1836 have the version number script until we have read all of the input
1837 files, so until that point we don't know which symbols should be
1838 local. This function is called via elf_link_hash_traverse. */
1839
1840bfd_boolean
268b6b39 1841_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
1842{
1843 struct elf_assign_sym_version_info *sinfo;
1844 struct bfd_link_info *info;
9c5bfbb7 1845 const struct elf_backend_data *bed;
45d6a902
AM
1846 struct elf_info_failed eif;
1847 char *p;
1848 bfd_size_type amt;
1849
268b6b39 1850 sinfo = data;
45d6a902
AM
1851 info = sinfo->info;
1852
1853 if (h->root.type == bfd_link_hash_warning)
1854 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1855
1856 /* Fix the symbol flags. */
1857 eif.failed = FALSE;
1858 eif.info = info;
1859 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1860 {
1861 if (eif.failed)
1862 sinfo->failed = TRUE;
1863 return FALSE;
1864 }
1865
1866 /* We only need version numbers for symbols defined in regular
1867 objects. */
f5385ebf 1868 if (!h->def_regular)
45d6a902
AM
1869 return TRUE;
1870
1871 bed = get_elf_backend_data (sinfo->output_bfd);
1872 p = strchr (h->root.root.string, ELF_VER_CHR);
1873 if (p != NULL && h->verinfo.vertree == NULL)
1874 {
1875 struct bfd_elf_version_tree *t;
1876 bfd_boolean hidden;
1877
1878 hidden = TRUE;
1879
1880 /* There are two consecutive ELF_VER_CHR characters if this is
1881 not a hidden symbol. */
1882 ++p;
1883 if (*p == ELF_VER_CHR)
1884 {
1885 hidden = FALSE;
1886 ++p;
1887 }
1888
1889 /* If there is no version string, we can just return out. */
1890 if (*p == '\0')
1891 {
1892 if (hidden)
f5385ebf 1893 h->hidden = 1;
45d6a902
AM
1894 return TRUE;
1895 }
1896
1897 /* Look for the version. If we find it, it is no longer weak. */
1898 for (t = sinfo->verdefs; t != NULL; t = t->next)
1899 {
1900 if (strcmp (t->name, p) == 0)
1901 {
1902 size_t len;
1903 char *alc;
1904 struct bfd_elf_version_expr *d;
1905
1906 len = p - h->root.root.string;
268b6b39 1907 alc = bfd_malloc (len);
45d6a902 1908 if (alc == NULL)
14b1c01e
AM
1909 {
1910 sinfo->failed = TRUE;
1911 return FALSE;
1912 }
45d6a902
AM
1913 memcpy (alc, h->root.root.string, len - 1);
1914 alc[len - 1] = '\0';
1915 if (alc[len - 2] == ELF_VER_CHR)
1916 alc[len - 2] = '\0';
1917
1918 h->verinfo.vertree = t;
1919 t->used = TRUE;
1920 d = NULL;
1921
108ba305
JJ
1922 if (t->globals.list != NULL)
1923 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
1924
1925 /* See if there is anything to force this symbol to
1926 local scope. */
108ba305 1927 if (d == NULL && t->locals.list != NULL)
45d6a902 1928 {
108ba305
JJ
1929 d = (*t->match) (&t->locals, NULL, alc);
1930 if (d != NULL
1931 && h->dynindx != -1
108ba305
JJ
1932 && ! info->export_dynamic)
1933 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
1934 }
1935
1936 free (alc);
1937 break;
1938 }
1939 }
1940
1941 /* If we are building an application, we need to create a
1942 version node for this version. */
36af4a4e 1943 if (t == NULL && info->executable)
45d6a902
AM
1944 {
1945 struct bfd_elf_version_tree **pp;
1946 int version_index;
1947
1948 /* If we aren't going to export this symbol, we don't need
1949 to worry about it. */
1950 if (h->dynindx == -1)
1951 return TRUE;
1952
1953 amt = sizeof *t;
108ba305 1954 t = bfd_zalloc (sinfo->output_bfd, amt);
45d6a902
AM
1955 if (t == NULL)
1956 {
1957 sinfo->failed = TRUE;
1958 return FALSE;
1959 }
1960
45d6a902 1961 t->name = p;
45d6a902
AM
1962 t->name_indx = (unsigned int) -1;
1963 t->used = TRUE;
1964
1965 version_index = 1;
1966 /* Don't count anonymous version tag. */
1967 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1968 version_index = 0;
1969 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1970 ++version_index;
1971 t->vernum = version_index;
1972
1973 *pp = t;
1974
1975 h->verinfo.vertree = t;
1976 }
1977 else if (t == NULL)
1978 {
1979 /* We could not find the version for a symbol when
1980 generating a shared archive. Return an error. */
1981 (*_bfd_error_handler)
c55fe096 1982 (_("%B: version node not found for symbol %s"),
d003868e 1983 sinfo->output_bfd, h->root.root.string);
45d6a902
AM
1984 bfd_set_error (bfd_error_bad_value);
1985 sinfo->failed = TRUE;
1986 return FALSE;
1987 }
1988
1989 if (hidden)
f5385ebf 1990 h->hidden = 1;
45d6a902
AM
1991 }
1992
1993 /* If we don't have a version for this symbol, see if we can find
1994 something. */
1995 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1996 {
1997 struct bfd_elf_version_tree *t;
1998 struct bfd_elf_version_tree *local_ver;
1999 struct bfd_elf_version_expr *d;
2000
2001 /* See if can find what version this symbol is in. If the
2002 symbol is supposed to be local, then don't actually register
2003 it. */
2004 local_ver = NULL;
2005 for (t = sinfo->verdefs; t != NULL; t = t->next)
2006 {
108ba305 2007 if (t->globals.list != NULL)
45d6a902
AM
2008 {
2009 bfd_boolean matched;
2010
2011 matched = FALSE;
108ba305
JJ
2012 d = NULL;
2013 while ((d = (*t->match) (&t->globals, d,
2014 h->root.root.string)) != NULL)
2015 if (d->symver)
2016 matched = TRUE;
2017 else
2018 {
2019 /* There is a version without definition. Make
2020 the symbol the default definition for this
2021 version. */
2022 h->verinfo.vertree = t;
2023 local_ver = NULL;
2024 d->script = 1;
2025 break;
2026 }
45d6a902
AM
2027 if (d != NULL)
2028 break;
2029 else if (matched)
2030 /* There is no undefined version for this symbol. Hide the
2031 default one. */
2032 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2033 }
2034
108ba305 2035 if (t->locals.list != NULL)
45d6a902 2036 {
108ba305
JJ
2037 d = NULL;
2038 while ((d = (*t->match) (&t->locals, d,
2039 h->root.root.string)) != NULL)
45d6a902 2040 {
108ba305 2041 local_ver = t;
45d6a902 2042 /* If the match is "*", keep looking for a more
108ba305
JJ
2043 explicit, perhaps even global, match.
2044 XXX: Shouldn't this be !d->wildcard instead? */
2045 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2046 break;
45d6a902
AM
2047 }
2048
2049 if (d != NULL)
2050 break;
2051 }
2052 }
2053
2054 if (local_ver != NULL)
2055 {
2056 h->verinfo.vertree = local_ver;
2057 if (h->dynindx != -1
45d6a902
AM
2058 && ! info->export_dynamic)
2059 {
2060 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2061 }
2062 }
2063 }
2064
2065 return TRUE;
2066}
2067\f
45d6a902
AM
2068/* Read and swap the relocs from the section indicated by SHDR. This
2069 may be either a REL or a RELA section. The relocations are
2070 translated into RELA relocations and stored in INTERNAL_RELOCS,
2071 which should have already been allocated to contain enough space.
2072 The EXTERNAL_RELOCS are a buffer where the external form of the
2073 relocations should be stored.
2074
2075 Returns FALSE if something goes wrong. */
2076
2077static bfd_boolean
268b6b39 2078elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2079 asection *sec,
268b6b39
AM
2080 Elf_Internal_Shdr *shdr,
2081 void *external_relocs,
2082 Elf_Internal_Rela *internal_relocs)
45d6a902 2083{
9c5bfbb7 2084 const struct elf_backend_data *bed;
268b6b39 2085 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2086 const bfd_byte *erela;
2087 const bfd_byte *erelaend;
2088 Elf_Internal_Rela *irela;
243ef1e0
L
2089 Elf_Internal_Shdr *symtab_hdr;
2090 size_t nsyms;
45d6a902 2091
45d6a902
AM
2092 /* Position ourselves at the start of the section. */
2093 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2094 return FALSE;
2095
2096 /* Read the relocations. */
2097 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2098 return FALSE;
2099
243ef1e0
L
2100 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2101 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2102
45d6a902
AM
2103 bed = get_elf_backend_data (abfd);
2104
2105 /* Convert the external relocations to the internal format. */
2106 if (shdr->sh_entsize == bed->s->sizeof_rel)
2107 swap_in = bed->s->swap_reloc_in;
2108 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2109 swap_in = bed->s->swap_reloca_in;
2110 else
2111 {
2112 bfd_set_error (bfd_error_wrong_format);
2113 return FALSE;
2114 }
2115
2116 erela = external_relocs;
51992aec 2117 erelaend = erela + shdr->sh_size;
45d6a902
AM
2118 irela = internal_relocs;
2119 while (erela < erelaend)
2120 {
243ef1e0
L
2121 bfd_vma r_symndx;
2122
45d6a902 2123 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2124 r_symndx = ELF32_R_SYM (irela->r_info);
2125 if (bed->s->arch_size == 64)
2126 r_symndx >>= 24;
2127 if ((size_t) r_symndx >= nsyms)
2128 {
2129 (*_bfd_error_handler)
d003868e
AM
2130 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2131 " for offset 0x%lx in section `%A'"),
2132 abfd, sec,
2133 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2134 bfd_set_error (bfd_error_bad_value);
2135 return FALSE;
2136 }
45d6a902
AM
2137 irela += bed->s->int_rels_per_ext_rel;
2138 erela += shdr->sh_entsize;
2139 }
2140
2141 return TRUE;
2142}
2143
2144/* Read and swap the relocs for a section O. They may have been
2145 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2146 not NULL, they are used as buffers to read into. They are known to
2147 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2148 the return value is allocated using either malloc or bfd_alloc,
2149 according to the KEEP_MEMORY argument. If O has two relocation
2150 sections (both REL and RELA relocations), then the REL_HDR
2151 relocations will appear first in INTERNAL_RELOCS, followed by the
2152 REL_HDR2 relocations. */
2153
2154Elf_Internal_Rela *
268b6b39
AM
2155_bfd_elf_link_read_relocs (bfd *abfd,
2156 asection *o,
2157 void *external_relocs,
2158 Elf_Internal_Rela *internal_relocs,
2159 bfd_boolean keep_memory)
45d6a902
AM
2160{
2161 Elf_Internal_Shdr *rel_hdr;
268b6b39 2162 void *alloc1 = NULL;
45d6a902 2163 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2164 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
2165
2166 if (elf_section_data (o)->relocs != NULL)
2167 return elf_section_data (o)->relocs;
2168
2169 if (o->reloc_count == 0)
2170 return NULL;
2171
2172 rel_hdr = &elf_section_data (o)->rel_hdr;
2173
2174 if (internal_relocs == NULL)
2175 {
2176 bfd_size_type size;
2177
2178 size = o->reloc_count;
2179 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2180 if (keep_memory)
4dd07732 2181 internal_relocs = alloc2 = bfd_alloc (abfd, size);
45d6a902 2182 else
268b6b39 2183 internal_relocs = alloc2 = bfd_malloc (size);
45d6a902
AM
2184 if (internal_relocs == NULL)
2185 goto error_return;
2186 }
2187
2188 if (external_relocs == NULL)
2189 {
2190 bfd_size_type size = rel_hdr->sh_size;
2191
2192 if (elf_section_data (o)->rel_hdr2)
2193 size += elf_section_data (o)->rel_hdr2->sh_size;
268b6b39 2194 alloc1 = bfd_malloc (size);
45d6a902
AM
2195 if (alloc1 == NULL)
2196 goto error_return;
2197 external_relocs = alloc1;
2198 }
2199
243ef1e0 2200 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
45d6a902
AM
2201 external_relocs,
2202 internal_relocs))
2203 goto error_return;
51992aec
AM
2204 if (elf_section_data (o)->rel_hdr2
2205 && (!elf_link_read_relocs_from_section
2206 (abfd, o,
2207 elf_section_data (o)->rel_hdr2,
2208 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2209 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2210 * bed->s->int_rels_per_ext_rel))))
45d6a902
AM
2211 goto error_return;
2212
2213 /* Cache the results for next time, if we can. */
2214 if (keep_memory)
2215 elf_section_data (o)->relocs = internal_relocs;
2216
2217 if (alloc1 != NULL)
2218 free (alloc1);
2219
2220 /* Don't free alloc2, since if it was allocated we are passing it
2221 back (under the name of internal_relocs). */
2222
2223 return internal_relocs;
2224
2225 error_return:
2226 if (alloc1 != NULL)
2227 free (alloc1);
2228 if (alloc2 != NULL)
4dd07732
AM
2229 {
2230 if (keep_memory)
2231 bfd_release (abfd, alloc2);
2232 else
2233 free (alloc2);
2234 }
45d6a902
AM
2235 return NULL;
2236}
2237
2238/* Compute the size of, and allocate space for, REL_HDR which is the
2239 section header for a section containing relocations for O. */
2240
2241bfd_boolean
268b6b39
AM
2242_bfd_elf_link_size_reloc_section (bfd *abfd,
2243 Elf_Internal_Shdr *rel_hdr,
2244 asection *o)
45d6a902
AM
2245{
2246 bfd_size_type reloc_count;
2247 bfd_size_type num_rel_hashes;
2248
2249 /* Figure out how many relocations there will be. */
2250 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2251 reloc_count = elf_section_data (o)->rel_count;
2252 else
2253 reloc_count = elf_section_data (o)->rel_count2;
2254
2255 num_rel_hashes = o->reloc_count;
2256 if (num_rel_hashes < reloc_count)
2257 num_rel_hashes = reloc_count;
2258
2259 /* That allows us to calculate the size of the section. */
2260 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2261
2262 /* The contents field must last into write_object_contents, so we
2263 allocate it with bfd_alloc rather than malloc. Also since we
2264 cannot be sure that the contents will actually be filled in,
2265 we zero the allocated space. */
268b6b39 2266 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2267 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2268 return FALSE;
2269
2270 /* We only allocate one set of hash entries, so we only do it the
2271 first time we are called. */
2272 if (elf_section_data (o)->rel_hashes == NULL
2273 && num_rel_hashes)
2274 {
2275 struct elf_link_hash_entry **p;
2276
268b6b39 2277 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2278 if (p == NULL)
2279 return FALSE;
2280
2281 elf_section_data (o)->rel_hashes = p;
2282 }
2283
2284 return TRUE;
2285}
2286
2287/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2288 originated from the section given by INPUT_REL_HDR) to the
2289 OUTPUT_BFD. */
2290
2291bfd_boolean
268b6b39
AM
2292_bfd_elf_link_output_relocs (bfd *output_bfd,
2293 asection *input_section,
2294 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2295 Elf_Internal_Rela *internal_relocs,
2296 struct elf_link_hash_entry **rel_hash
2297 ATTRIBUTE_UNUSED)
45d6a902
AM
2298{
2299 Elf_Internal_Rela *irela;
2300 Elf_Internal_Rela *irelaend;
2301 bfd_byte *erel;
2302 Elf_Internal_Shdr *output_rel_hdr;
2303 asection *output_section;
2304 unsigned int *rel_countp = NULL;
9c5bfbb7 2305 const struct elf_backend_data *bed;
268b6b39 2306 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
45d6a902
AM
2307
2308 output_section = input_section->output_section;
2309 output_rel_hdr = NULL;
2310
2311 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2312 == input_rel_hdr->sh_entsize)
2313 {
2314 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2315 rel_countp = &elf_section_data (output_section)->rel_count;
2316 }
2317 else if (elf_section_data (output_section)->rel_hdr2
2318 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2319 == input_rel_hdr->sh_entsize))
2320 {
2321 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2322 rel_countp = &elf_section_data (output_section)->rel_count2;
2323 }
2324 else
2325 {
2326 (*_bfd_error_handler)
d003868e
AM
2327 (_("%B: relocation size mismatch in %B section %A"),
2328 output_bfd, input_section->owner, input_section);
297d8443 2329 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2330 return FALSE;
2331 }
2332
2333 bed = get_elf_backend_data (output_bfd);
2334 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2335 swap_out = bed->s->swap_reloc_out;
2336 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2337 swap_out = bed->s->swap_reloca_out;
2338 else
2339 abort ();
2340
2341 erel = output_rel_hdr->contents;
2342 erel += *rel_countp * input_rel_hdr->sh_entsize;
2343 irela = internal_relocs;
2344 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2345 * bed->s->int_rels_per_ext_rel);
2346 while (irela < irelaend)
2347 {
2348 (*swap_out) (output_bfd, irela, erel);
2349 irela += bed->s->int_rels_per_ext_rel;
2350 erel += input_rel_hdr->sh_entsize;
2351 }
2352
2353 /* Bump the counter, so that we know where to add the next set of
2354 relocations. */
2355 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2356
2357 return TRUE;
2358}
2359\f
508c3946
L
2360/* Make weak undefined symbols in PIE dynamic. */
2361
2362bfd_boolean
2363_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2364 struct elf_link_hash_entry *h)
2365{
2366 if (info->pie
2367 && h->dynindx == -1
2368 && h->root.type == bfd_link_hash_undefweak)
2369 return bfd_elf_link_record_dynamic_symbol (info, h);
2370
2371 return TRUE;
2372}
2373
45d6a902
AM
2374/* Fix up the flags for a symbol. This handles various cases which
2375 can only be fixed after all the input files are seen. This is
2376 currently called by both adjust_dynamic_symbol and
2377 assign_sym_version, which is unnecessary but perhaps more robust in
2378 the face of future changes. */
2379
2380bfd_boolean
268b6b39
AM
2381_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2382 struct elf_info_failed *eif)
45d6a902 2383{
33774f08 2384 const struct elf_backend_data *bed;
508c3946 2385
45d6a902
AM
2386 /* If this symbol was mentioned in a non-ELF file, try to set
2387 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2388 permit a non-ELF file to correctly refer to a symbol defined in
2389 an ELF dynamic object. */
f5385ebf 2390 if (h->non_elf)
45d6a902
AM
2391 {
2392 while (h->root.type == bfd_link_hash_indirect)
2393 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2394
2395 if (h->root.type != bfd_link_hash_defined
2396 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2397 {
2398 h->ref_regular = 1;
2399 h->ref_regular_nonweak = 1;
2400 }
45d6a902
AM
2401 else
2402 {
2403 if (h->root.u.def.section->owner != NULL
2404 && (bfd_get_flavour (h->root.u.def.section->owner)
2405 == bfd_target_elf_flavour))
f5385ebf
AM
2406 {
2407 h->ref_regular = 1;
2408 h->ref_regular_nonweak = 1;
2409 }
45d6a902 2410 else
f5385ebf 2411 h->def_regular = 1;
45d6a902
AM
2412 }
2413
2414 if (h->dynindx == -1
f5385ebf
AM
2415 && (h->def_dynamic
2416 || h->ref_dynamic))
45d6a902 2417 {
c152c796 2418 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2419 {
2420 eif->failed = TRUE;
2421 return FALSE;
2422 }
2423 }
2424 }
2425 else
2426 {
f5385ebf 2427 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2428 was first seen in a non-ELF file. Fortunately, if the symbol
2429 was first seen in an ELF file, we're probably OK unless the
2430 symbol was defined in a non-ELF file. Catch that case here.
2431 FIXME: We're still in trouble if the symbol was first seen in
2432 a dynamic object, and then later in a non-ELF regular object. */
2433 if ((h->root.type == bfd_link_hash_defined
2434 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2435 && !h->def_regular
45d6a902
AM
2436 && (h->root.u.def.section->owner != NULL
2437 ? (bfd_get_flavour (h->root.u.def.section->owner)
2438 != bfd_target_elf_flavour)
2439 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2440 && !h->def_dynamic)))
2441 h->def_regular = 1;
45d6a902
AM
2442 }
2443
508c3946 2444 /* Backend specific symbol fixup. */
33774f08
AM
2445 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2446 if (bed->elf_backend_fixup_symbol
2447 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2448 return FALSE;
508c3946 2449
45d6a902
AM
2450 /* If this is a final link, and the symbol was defined as a common
2451 symbol in a regular object file, and there was no definition in
2452 any dynamic object, then the linker will have allocated space for
f5385ebf 2453 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2454 flag will not have been set. */
2455 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2456 && !h->def_regular
2457 && h->ref_regular
2458 && !h->def_dynamic
45d6a902 2459 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2460 h->def_regular = 1;
45d6a902
AM
2461
2462 /* If -Bsymbolic was used (which means to bind references to global
2463 symbols to the definition within the shared object), and this
2464 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2465 need a PLT entry. Likewise, if the symbol has non-default
2466 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2467 will force it local. */
f5385ebf 2468 if (h->needs_plt
45d6a902 2469 && eif->info->shared
0eddce27 2470 && is_elf_hash_table (eif->info->hash)
55255dae 2471 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2472 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2473 && h->def_regular)
45d6a902 2474 {
45d6a902
AM
2475 bfd_boolean force_local;
2476
45d6a902
AM
2477 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2478 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2479 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2480 }
2481
2482 /* If a weak undefined symbol has non-default visibility, we also
2483 hide it from the dynamic linker. */
9c7a29a3 2484 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2485 && h->root.type == bfd_link_hash_undefweak)
33774f08 2486 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2487
2488 /* If this is a weak defined symbol in a dynamic object, and we know
2489 the real definition in the dynamic object, copy interesting flags
2490 over to the real definition. */
f6e332e6 2491 if (h->u.weakdef != NULL)
45d6a902
AM
2492 {
2493 struct elf_link_hash_entry *weakdef;
2494
f6e332e6 2495 weakdef = h->u.weakdef;
45d6a902
AM
2496 if (h->root.type == bfd_link_hash_indirect)
2497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2498
2499 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2500 || h->root.type == bfd_link_hash_defweak);
f5385ebf 2501 BFD_ASSERT (weakdef->def_dynamic);
45d6a902
AM
2502
2503 /* If the real definition is defined by a regular object file,
2504 don't do anything special. See the longer description in
2505 _bfd_elf_adjust_dynamic_symbol, below. */
f5385ebf 2506 if (weakdef->def_regular)
f6e332e6 2507 h->u.weakdef = NULL;
45d6a902 2508 else
a26587ba
RS
2509 {
2510 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2511 || weakdef->root.type == bfd_link_hash_defweak);
2512 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2513 }
45d6a902
AM
2514 }
2515
2516 return TRUE;
2517}
2518
2519/* Make the backend pick a good value for a dynamic symbol. This is
2520 called via elf_link_hash_traverse, and also calls itself
2521 recursively. */
2522
2523bfd_boolean
268b6b39 2524_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2525{
268b6b39 2526 struct elf_info_failed *eif = data;
45d6a902 2527 bfd *dynobj;
9c5bfbb7 2528 const struct elf_backend_data *bed;
45d6a902 2529
0eddce27 2530 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2531 return FALSE;
2532
2533 if (h->root.type == bfd_link_hash_warning)
2534 {
a6aa5195
AM
2535 h->got = elf_hash_table (eif->info)->init_got_offset;
2536 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2537
2538 /* When warning symbols are created, they **replace** the "real"
2539 entry in the hash table, thus we never get to see the real
2540 symbol in a hash traversal. So look at it now. */
2541 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2542 }
2543
2544 /* Ignore indirect symbols. These are added by the versioning code. */
2545 if (h->root.type == bfd_link_hash_indirect)
2546 return TRUE;
2547
2548 /* Fix the symbol flags. */
2549 if (! _bfd_elf_fix_symbol_flags (h, eif))
2550 return FALSE;
2551
2552 /* If this symbol does not require a PLT entry, and it is not
2553 defined by a dynamic object, or is not referenced by a regular
2554 object, ignore it. We do have to handle a weak defined symbol,
2555 even if no regular object refers to it, if we decided to add it
2556 to the dynamic symbol table. FIXME: Do we normally need to worry
2557 about symbols which are defined by one dynamic object and
2558 referenced by another one? */
f5385ebf
AM
2559 if (!h->needs_plt
2560 && (h->def_regular
2561 || !h->def_dynamic
2562 || (!h->ref_regular
f6e332e6 2563 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2564 {
a6aa5195 2565 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2566 return TRUE;
2567 }
2568
2569 /* If we've already adjusted this symbol, don't do it again. This
2570 can happen via a recursive call. */
f5385ebf 2571 if (h->dynamic_adjusted)
45d6a902
AM
2572 return TRUE;
2573
2574 /* Don't look at this symbol again. Note that we must set this
2575 after checking the above conditions, because we may look at a
2576 symbol once, decide not to do anything, and then get called
2577 recursively later after REF_REGULAR is set below. */
f5385ebf 2578 h->dynamic_adjusted = 1;
45d6a902
AM
2579
2580 /* If this is a weak definition, and we know a real definition, and
2581 the real symbol is not itself defined by a regular object file,
2582 then get a good value for the real definition. We handle the
2583 real symbol first, for the convenience of the backend routine.
2584
2585 Note that there is a confusing case here. If the real definition
2586 is defined by a regular object file, we don't get the real symbol
2587 from the dynamic object, but we do get the weak symbol. If the
2588 processor backend uses a COPY reloc, then if some routine in the
2589 dynamic object changes the real symbol, we will not see that
2590 change in the corresponding weak symbol. This is the way other
2591 ELF linkers work as well, and seems to be a result of the shared
2592 library model.
2593
2594 I will clarify this issue. Most SVR4 shared libraries define the
2595 variable _timezone and define timezone as a weak synonym. The
2596 tzset call changes _timezone. If you write
2597 extern int timezone;
2598 int _timezone = 5;
2599 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2600 you might expect that, since timezone is a synonym for _timezone,
2601 the same number will print both times. However, if the processor
2602 backend uses a COPY reloc, then actually timezone will be copied
2603 into your process image, and, since you define _timezone
2604 yourself, _timezone will not. Thus timezone and _timezone will
2605 wind up at different memory locations. The tzset call will set
2606 _timezone, leaving timezone unchanged. */
2607
f6e332e6 2608 if (h->u.weakdef != NULL)
45d6a902
AM
2609 {
2610 /* If we get to this point, we know there is an implicit
2611 reference by a regular object file via the weak symbol H.
2612 FIXME: Is this really true? What if the traversal finds
f6e332e6
AM
2613 H->U.WEAKDEF before it finds H? */
2614 h->u.weakdef->ref_regular = 1;
45d6a902 2615
f6e332e6 2616 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2617 return FALSE;
2618 }
2619
2620 /* If a symbol has no type and no size and does not require a PLT
2621 entry, then we are probably about to do the wrong thing here: we
2622 are probably going to create a COPY reloc for an empty object.
2623 This case can arise when a shared object is built with assembly
2624 code, and the assembly code fails to set the symbol type. */
2625 if (h->size == 0
2626 && h->type == STT_NOTYPE
f5385ebf 2627 && !h->needs_plt)
45d6a902
AM
2628 (*_bfd_error_handler)
2629 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2630 h->root.root.string);
2631
2632 dynobj = elf_hash_table (eif->info)->dynobj;
2633 bed = get_elf_backend_data (dynobj);
2634 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2635 {
2636 eif->failed = TRUE;
2637 return FALSE;
2638 }
2639
2640 return TRUE;
2641}
2642
027297b7
L
2643/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2644 DYNBSS. */
2645
2646bfd_boolean
2647_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2648 asection *dynbss)
2649{
91ac5911 2650 unsigned int power_of_two;
027297b7
L
2651 bfd_vma mask;
2652 asection *sec = h->root.u.def.section;
2653
2654 /* The section aligment of definition is the maximum alignment
91ac5911
L
2655 requirement of symbols defined in the section. Since we don't
2656 know the symbol alignment requirement, we start with the
2657 maximum alignment and check low bits of the symbol address
2658 for the minimum alignment. */
2659 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2660 mask = ((bfd_vma) 1 << power_of_two) - 1;
2661 while ((h->root.u.def.value & mask) != 0)
2662 {
2663 mask >>= 1;
2664 --power_of_two;
2665 }
027297b7 2666
91ac5911
L
2667 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2668 dynbss))
027297b7
L
2669 {
2670 /* Adjust the section alignment if needed. */
2671 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2672 power_of_two))
027297b7
L
2673 return FALSE;
2674 }
2675
91ac5911 2676 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2677 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2678
2679 /* Define the symbol as being at this point in DYNBSS. */
2680 h->root.u.def.section = dynbss;
2681 h->root.u.def.value = dynbss->size;
2682
2683 /* Increment the size of DYNBSS to make room for the symbol. */
2684 dynbss->size += h->size;
2685
2686 return TRUE;
2687}
2688
45d6a902
AM
2689/* Adjust all external symbols pointing into SEC_MERGE sections
2690 to reflect the object merging within the sections. */
2691
2692bfd_boolean
268b6b39 2693_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2694{
2695 asection *sec;
2696
2697 if (h->root.type == bfd_link_hash_warning)
2698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2699
2700 if ((h->root.type == bfd_link_hash_defined
2701 || h->root.type == bfd_link_hash_defweak)
2702 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2703 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2704 {
268b6b39 2705 bfd *output_bfd = data;
45d6a902
AM
2706
2707 h->root.u.def.value =
2708 _bfd_merged_section_offset (output_bfd,
2709 &h->root.u.def.section,
2710 elf_section_data (sec)->sec_info,
753731ee 2711 h->root.u.def.value);
45d6a902
AM
2712 }
2713
2714 return TRUE;
2715}
986a241f
RH
2716
2717/* Returns false if the symbol referred to by H should be considered
2718 to resolve local to the current module, and true if it should be
2719 considered to bind dynamically. */
2720
2721bfd_boolean
268b6b39
AM
2722_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2723 struct bfd_link_info *info,
2724 bfd_boolean ignore_protected)
986a241f
RH
2725{
2726 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2727 const struct elf_backend_data *bed;
2728 struct elf_link_hash_table *hash_table;
986a241f
RH
2729
2730 if (h == NULL)
2731 return FALSE;
2732
2733 while (h->root.type == bfd_link_hash_indirect
2734 || h->root.type == bfd_link_hash_warning)
2735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2736
2737 /* If it was forced local, then clearly it's not dynamic. */
2738 if (h->dynindx == -1)
2739 return FALSE;
f5385ebf 2740 if (h->forced_local)
986a241f
RH
2741 return FALSE;
2742
2743 /* Identify the cases where name binding rules say that a
2744 visible symbol resolves locally. */
55255dae 2745 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2746
2747 switch (ELF_ST_VISIBILITY (h->other))
2748 {
2749 case STV_INTERNAL:
2750 case STV_HIDDEN:
2751 return FALSE;
2752
2753 case STV_PROTECTED:
fcb93ecf
PB
2754 hash_table = elf_hash_table (info);
2755 if (!is_elf_hash_table (hash_table))
2756 return FALSE;
2757
2758 bed = get_elf_backend_data (hash_table->dynobj);
2759
986a241f
RH
2760 /* Proper resolution for function pointer equality may require
2761 that these symbols perhaps be resolved dynamically, even though
2762 we should be resolving them to the current module. */
fcb93ecf 2763 if (!ignore_protected || !bed->is_function_type (h->type))
986a241f
RH
2764 binding_stays_local_p = TRUE;
2765 break;
2766
2767 default:
986a241f
RH
2768 break;
2769 }
2770
aa37626c 2771 /* If it isn't defined locally, then clearly it's dynamic. */
f5385ebf 2772 if (!h->def_regular)
aa37626c
L
2773 return TRUE;
2774
986a241f
RH
2775 /* Otherwise, the symbol is dynamic if binding rules don't tell
2776 us that it remains local. */
2777 return !binding_stays_local_p;
2778}
f6c52c13
AM
2779
2780/* Return true if the symbol referred to by H should be considered
2781 to resolve local to the current module, and false otherwise. Differs
2782 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2783 undefined symbols and weak symbols. */
2784
2785bfd_boolean
268b6b39
AM
2786_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2787 struct bfd_link_info *info,
2788 bfd_boolean local_protected)
f6c52c13 2789{
fcb93ecf
PB
2790 const struct elf_backend_data *bed;
2791 struct elf_link_hash_table *hash_table;
2792
f6c52c13
AM
2793 /* If it's a local sym, of course we resolve locally. */
2794 if (h == NULL)
2795 return TRUE;
2796
d95edcac
L
2797 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2798 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2799 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2800 return TRUE;
2801
7e2294f9
AO
2802 /* Common symbols that become definitions don't get the DEF_REGULAR
2803 flag set, so test it first, and don't bail out. */
2804 if (ELF_COMMON_DEF_P (h))
2805 /* Do nothing. */;
f6c52c13 2806 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2807 resolve locally. The sym is either undefined or dynamic. */
2808 else if (!h->def_regular)
f6c52c13
AM
2809 return FALSE;
2810
2811 /* Forced local symbols resolve locally. */
f5385ebf 2812 if (h->forced_local)
f6c52c13
AM
2813 return TRUE;
2814
2815 /* As do non-dynamic symbols. */
2816 if (h->dynindx == -1)
2817 return TRUE;
2818
2819 /* At this point, we know the symbol is defined and dynamic. In an
2820 executable it must resolve locally, likewise when building symbolic
2821 shared libraries. */
55255dae 2822 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2823 return TRUE;
2824
2825 /* Now deal with defined dynamic symbols in shared libraries. Ones
2826 with default visibility might not resolve locally. */
2827 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2828 return FALSE;
2829
fcb93ecf
PB
2830 hash_table = elf_hash_table (info);
2831 if (!is_elf_hash_table (hash_table))
2832 return TRUE;
2833
2834 bed = get_elf_backend_data (hash_table->dynobj);
2835
1c16dfa5 2836 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2837 if (!bed->is_function_type (h->type))
1c16dfa5
L
2838 return TRUE;
2839
f6c52c13
AM
2840 /* Function pointer equality tests may require that STV_PROTECTED
2841 symbols be treated as dynamic symbols, even when we know that the
2842 dynamic linker will resolve them locally. */
2843 return local_protected;
2844}
e1918d23
AM
2845
2846/* Caches some TLS segment info, and ensures that the TLS segment vma is
2847 aligned. Returns the first TLS output section. */
2848
2849struct bfd_section *
2850_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2851{
2852 struct bfd_section *sec, *tls;
2853 unsigned int align = 0;
2854
2855 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2856 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2857 break;
2858 tls = sec;
2859
2860 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2861 if (sec->alignment_power > align)
2862 align = sec->alignment_power;
2863
2864 elf_hash_table (info)->tls_sec = tls;
2865
2866 /* Ensure the alignment of the first section is the largest alignment,
2867 so that the tls segment starts aligned. */
2868 if (tls != NULL)
2869 tls->alignment_power = align;
2870
2871 return tls;
2872}
0ad989f9
L
2873
2874/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2875static bfd_boolean
2876is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2877 Elf_Internal_Sym *sym)
2878{
a4d8e49b
L
2879 const struct elf_backend_data *bed;
2880
0ad989f9
L
2881 /* Local symbols do not count, but target specific ones might. */
2882 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2883 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2884 return FALSE;
2885
fcb93ecf 2886 bed = get_elf_backend_data (abfd);
0ad989f9 2887 /* Function symbols do not count. */
fcb93ecf 2888 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2889 return FALSE;
2890
2891 /* If the section is undefined, then so is the symbol. */
2892 if (sym->st_shndx == SHN_UNDEF)
2893 return FALSE;
2894
2895 /* If the symbol is defined in the common section, then
2896 it is a common definition and so does not count. */
a4d8e49b 2897 if (bed->common_definition (sym))
0ad989f9
L
2898 return FALSE;
2899
2900 /* If the symbol is in a target specific section then we
2901 must rely upon the backend to tell us what it is. */
2902 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2903 /* FIXME - this function is not coded yet:
2904
2905 return _bfd_is_global_symbol_definition (abfd, sym);
2906
2907 Instead for now assume that the definition is not global,
2908 Even if this is wrong, at least the linker will behave
2909 in the same way that it used to do. */
2910 return FALSE;
2911
2912 return TRUE;
2913}
2914
2915/* Search the symbol table of the archive element of the archive ABFD
2916 whose archive map contains a mention of SYMDEF, and determine if
2917 the symbol is defined in this element. */
2918static bfd_boolean
2919elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2920{
2921 Elf_Internal_Shdr * hdr;
2922 bfd_size_type symcount;
2923 bfd_size_type extsymcount;
2924 bfd_size_type extsymoff;
2925 Elf_Internal_Sym *isymbuf;
2926 Elf_Internal_Sym *isym;
2927 Elf_Internal_Sym *isymend;
2928 bfd_boolean result;
2929
2930 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2931 if (abfd == NULL)
2932 return FALSE;
2933
2934 if (! bfd_check_format (abfd, bfd_object))
2935 return FALSE;
2936
2937 /* If we have already included the element containing this symbol in the
2938 link then we do not need to include it again. Just claim that any symbol
2939 it contains is not a definition, so that our caller will not decide to
2940 (re)include this element. */
2941 if (abfd->archive_pass)
2942 return FALSE;
2943
2944 /* Select the appropriate symbol table. */
2945 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2946 hdr = &elf_tdata (abfd)->symtab_hdr;
2947 else
2948 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2949
2950 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2951
2952 /* The sh_info field of the symtab header tells us where the
2953 external symbols start. We don't care about the local symbols. */
2954 if (elf_bad_symtab (abfd))
2955 {
2956 extsymcount = symcount;
2957 extsymoff = 0;
2958 }
2959 else
2960 {
2961 extsymcount = symcount - hdr->sh_info;
2962 extsymoff = hdr->sh_info;
2963 }
2964
2965 if (extsymcount == 0)
2966 return FALSE;
2967
2968 /* Read in the symbol table. */
2969 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2970 NULL, NULL, NULL);
2971 if (isymbuf == NULL)
2972 return FALSE;
2973
2974 /* Scan the symbol table looking for SYMDEF. */
2975 result = FALSE;
2976 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2977 {
2978 const char *name;
2979
2980 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2981 isym->st_name);
2982 if (name == NULL)
2983 break;
2984
2985 if (strcmp (name, symdef->name) == 0)
2986 {
2987 result = is_global_data_symbol_definition (abfd, isym);
2988 break;
2989 }
2990 }
2991
2992 free (isymbuf);
2993
2994 return result;
2995}
2996\f
5a580b3a
AM
2997/* Add an entry to the .dynamic table. */
2998
2999bfd_boolean
3000_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3001 bfd_vma tag,
3002 bfd_vma val)
3003{
3004 struct elf_link_hash_table *hash_table;
3005 const struct elf_backend_data *bed;
3006 asection *s;
3007 bfd_size_type newsize;
3008 bfd_byte *newcontents;
3009 Elf_Internal_Dyn dyn;
3010
3011 hash_table = elf_hash_table (info);
3012 if (! is_elf_hash_table (hash_table))
3013 return FALSE;
3014
3015 bed = get_elf_backend_data (hash_table->dynobj);
3016 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3017 BFD_ASSERT (s != NULL);
3018
eea6121a 3019 newsize = s->size + bed->s->sizeof_dyn;
5a580b3a
AM
3020 newcontents = bfd_realloc (s->contents, newsize);
3021 if (newcontents == NULL)
3022 return FALSE;
3023
3024 dyn.d_tag = tag;
3025 dyn.d_un.d_val = val;
eea6121a 3026 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3027
eea6121a 3028 s->size = newsize;
5a580b3a
AM
3029 s->contents = newcontents;
3030
3031 return TRUE;
3032}
3033
3034/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3035 otherwise just check whether one already exists. Returns -1 on error,
3036 1 if a DT_NEEDED tag already exists, and 0 on success. */
3037
4ad4eba5 3038static int
7e9f0867
AM
3039elf_add_dt_needed_tag (bfd *abfd,
3040 struct bfd_link_info *info,
4ad4eba5
AM
3041 const char *soname,
3042 bfd_boolean do_it)
5a580b3a
AM
3043{
3044 struct elf_link_hash_table *hash_table;
3045 bfd_size_type oldsize;
3046 bfd_size_type strindex;
3047
7e9f0867
AM
3048 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3049 return -1;
3050
5a580b3a
AM
3051 hash_table = elf_hash_table (info);
3052 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3053 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3054 if (strindex == (bfd_size_type) -1)
3055 return -1;
3056
3057 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3058 {
3059 asection *sdyn;
3060 const struct elf_backend_data *bed;
3061 bfd_byte *extdyn;
3062
3063 bed = get_elf_backend_data (hash_table->dynobj);
3064 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3065 if (sdyn != NULL)
3066 for (extdyn = sdyn->contents;
3067 extdyn < sdyn->contents + sdyn->size;
3068 extdyn += bed->s->sizeof_dyn)
3069 {
3070 Elf_Internal_Dyn dyn;
5a580b3a 3071
7e9f0867
AM
3072 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3073 if (dyn.d_tag == DT_NEEDED
3074 && dyn.d_un.d_val == strindex)
3075 {
3076 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3077 return 1;
3078 }
3079 }
5a580b3a
AM
3080 }
3081
3082 if (do_it)
3083 {
7e9f0867
AM
3084 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3085 return -1;
3086
5a580b3a
AM
3087 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3088 return -1;
3089 }
3090 else
3091 /* We were just checking for existence of the tag. */
3092 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3093
3094 return 0;
3095}
3096
3097/* Sort symbol by value and section. */
4ad4eba5
AM
3098static int
3099elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3100{
3101 const struct elf_link_hash_entry *h1;
3102 const struct elf_link_hash_entry *h2;
10b7e05b 3103 bfd_signed_vma vdiff;
5a580b3a
AM
3104
3105 h1 = *(const struct elf_link_hash_entry **) arg1;
3106 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3107 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3108 if (vdiff != 0)
3109 return vdiff > 0 ? 1 : -1;
3110 else
3111 {
3112 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3113 if (sdiff != 0)
3114 return sdiff > 0 ? 1 : -1;
3115 }
5a580b3a
AM
3116 return 0;
3117}
4ad4eba5 3118
5a580b3a
AM
3119/* This function is used to adjust offsets into .dynstr for
3120 dynamic symbols. This is called via elf_link_hash_traverse. */
3121
3122static bfd_boolean
3123elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3124{
3125 struct elf_strtab_hash *dynstr = data;
3126
3127 if (h->root.type == bfd_link_hash_warning)
3128 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3129
3130 if (h->dynindx != -1)
3131 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3132 return TRUE;
3133}
3134
3135/* Assign string offsets in .dynstr, update all structures referencing
3136 them. */
3137
4ad4eba5
AM
3138static bfd_boolean
3139elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3140{
3141 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3142 struct elf_link_local_dynamic_entry *entry;
3143 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3144 bfd *dynobj = hash_table->dynobj;
3145 asection *sdyn;
3146 bfd_size_type size;
3147 const struct elf_backend_data *bed;
3148 bfd_byte *extdyn;
3149
3150 _bfd_elf_strtab_finalize (dynstr);
3151 size = _bfd_elf_strtab_size (dynstr);
3152
3153 bed = get_elf_backend_data (dynobj);
3154 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3155 BFD_ASSERT (sdyn != NULL);
3156
3157 /* Update all .dynamic entries referencing .dynstr strings. */
3158 for (extdyn = sdyn->contents;
eea6121a 3159 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3160 extdyn += bed->s->sizeof_dyn)
3161 {
3162 Elf_Internal_Dyn dyn;
3163
3164 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3165 switch (dyn.d_tag)
3166 {
3167 case DT_STRSZ:
3168 dyn.d_un.d_val = size;
3169 break;
3170 case DT_NEEDED:
3171 case DT_SONAME:
3172 case DT_RPATH:
3173 case DT_RUNPATH:
3174 case DT_FILTER:
3175 case DT_AUXILIARY:
3176 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3177 break;
3178 default:
3179 continue;
3180 }
3181 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3182 }
3183
3184 /* Now update local dynamic symbols. */
3185 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3186 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3187 entry->isym.st_name);
3188
3189 /* And the rest of dynamic symbols. */
3190 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3191
3192 /* Adjust version definitions. */
3193 if (elf_tdata (output_bfd)->cverdefs)
3194 {
3195 asection *s;
3196 bfd_byte *p;
3197 bfd_size_type i;
3198 Elf_Internal_Verdef def;
3199 Elf_Internal_Verdaux defaux;
3200
3201 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3202 p = s->contents;
3203 do
3204 {
3205 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3206 &def);
3207 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3208 if (def.vd_aux != sizeof (Elf_External_Verdef))
3209 continue;
5a580b3a
AM
3210 for (i = 0; i < def.vd_cnt; ++i)
3211 {
3212 _bfd_elf_swap_verdaux_in (output_bfd,
3213 (Elf_External_Verdaux *) p, &defaux);
3214 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3215 defaux.vda_name);
3216 _bfd_elf_swap_verdaux_out (output_bfd,
3217 &defaux, (Elf_External_Verdaux *) p);
3218 p += sizeof (Elf_External_Verdaux);
3219 }
3220 }
3221 while (def.vd_next);
3222 }
3223
3224 /* Adjust version references. */
3225 if (elf_tdata (output_bfd)->verref)
3226 {
3227 asection *s;
3228 bfd_byte *p;
3229 bfd_size_type i;
3230 Elf_Internal_Verneed need;
3231 Elf_Internal_Vernaux needaux;
3232
3233 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3234 p = s->contents;
3235 do
3236 {
3237 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3238 &need);
3239 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3240 _bfd_elf_swap_verneed_out (output_bfd, &need,
3241 (Elf_External_Verneed *) p);
3242 p += sizeof (Elf_External_Verneed);
3243 for (i = 0; i < need.vn_cnt; ++i)
3244 {
3245 _bfd_elf_swap_vernaux_in (output_bfd,
3246 (Elf_External_Vernaux *) p, &needaux);
3247 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3248 needaux.vna_name);
3249 _bfd_elf_swap_vernaux_out (output_bfd,
3250 &needaux,
3251 (Elf_External_Vernaux *) p);
3252 p += sizeof (Elf_External_Vernaux);
3253 }
3254 }
3255 while (need.vn_next);
3256 }
3257
3258 return TRUE;
3259}
3260\f
13285a1b
AM
3261/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3262 The default is to only match when the INPUT and OUTPUT are exactly
3263 the same target. */
3264
3265bfd_boolean
3266_bfd_elf_default_relocs_compatible (const bfd_target *input,
3267 const bfd_target *output)
3268{
3269 return input == output;
3270}
3271
3272/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3273 This version is used when different targets for the same architecture
3274 are virtually identical. */
3275
3276bfd_boolean
3277_bfd_elf_relocs_compatible (const bfd_target *input,
3278 const bfd_target *output)
3279{
3280 const struct elf_backend_data *obed, *ibed;
3281
3282 if (input == output)
3283 return TRUE;
3284
3285 ibed = xvec_get_elf_backend_data (input);
3286 obed = xvec_get_elf_backend_data (output);
3287
3288 if (ibed->arch != obed->arch)
3289 return FALSE;
3290
3291 /* If both backends are using this function, deem them compatible. */
3292 return ibed->relocs_compatible == obed->relocs_compatible;
3293}
3294
4ad4eba5
AM
3295/* Add symbols from an ELF object file to the linker hash table. */
3296
3297static bfd_boolean
3298elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3299{
4ad4eba5
AM
3300 Elf_Internal_Shdr *hdr;
3301 bfd_size_type symcount;
3302 bfd_size_type extsymcount;
3303 bfd_size_type extsymoff;
3304 struct elf_link_hash_entry **sym_hash;
3305 bfd_boolean dynamic;
3306 Elf_External_Versym *extversym = NULL;
3307 Elf_External_Versym *ever;
3308 struct elf_link_hash_entry *weaks;
3309 struct elf_link_hash_entry **nondeflt_vers = NULL;
3310 bfd_size_type nondeflt_vers_cnt = 0;
3311 Elf_Internal_Sym *isymbuf = NULL;
3312 Elf_Internal_Sym *isym;
3313 Elf_Internal_Sym *isymend;
3314 const struct elf_backend_data *bed;
3315 bfd_boolean add_needed;
66eb6687 3316 struct elf_link_hash_table *htab;
4ad4eba5 3317 bfd_size_type amt;
66eb6687 3318 void *alloc_mark = NULL;
4f87808c
AM
3319 struct bfd_hash_entry **old_table = NULL;
3320 unsigned int old_size = 0;
3321 unsigned int old_count = 0;
66eb6687
AM
3322 void *old_tab = NULL;
3323 void *old_hash;
3324 void *old_ent;
3325 struct bfd_link_hash_entry *old_undefs = NULL;
3326 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3327 long old_dynsymcount = 0;
3328 size_t tabsize = 0;
3329 size_t hashsize = 0;
4ad4eba5 3330
66eb6687 3331 htab = elf_hash_table (info);
4ad4eba5 3332 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3333
3334 if ((abfd->flags & DYNAMIC) == 0)
3335 dynamic = FALSE;
3336 else
3337 {
3338 dynamic = TRUE;
3339
3340 /* You can't use -r against a dynamic object. Also, there's no
3341 hope of using a dynamic object which does not exactly match
3342 the format of the output file. */
3343 if (info->relocatable
66eb6687 3344 || !is_elf_hash_table (htab)
f13a99db 3345 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3346 {
9a0789ec
NC
3347 if (info->relocatable)
3348 bfd_set_error (bfd_error_invalid_operation);
3349 else
3350 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3351 goto error_return;
3352 }
3353 }
3354
3355 /* As a GNU extension, any input sections which are named
3356 .gnu.warning.SYMBOL are treated as warning symbols for the given
3357 symbol. This differs from .gnu.warning sections, which generate
3358 warnings when they are included in an output file. */
3359 if (info->executable)
3360 {
3361 asection *s;
3362
3363 for (s = abfd->sections; s != NULL; s = s->next)
3364 {
3365 const char *name;
3366
3367 name = bfd_get_section_name (abfd, s);
0112cd26 3368 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3369 {
3370 char *msg;
3371 bfd_size_type sz;
4ad4eba5
AM
3372
3373 name += sizeof ".gnu.warning." - 1;
3374
3375 /* If this is a shared object, then look up the symbol
3376 in the hash table. If it is there, and it is already
3377 been defined, then we will not be using the entry
3378 from this shared object, so we don't need to warn.
3379 FIXME: If we see the definition in a regular object
3380 later on, we will warn, but we shouldn't. The only
3381 fix is to keep track of what warnings we are supposed
3382 to emit, and then handle them all at the end of the
3383 link. */
3384 if (dynamic)
3385 {
3386 struct elf_link_hash_entry *h;
3387
66eb6687 3388 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3389
3390 /* FIXME: What about bfd_link_hash_common? */
3391 if (h != NULL
3392 && (h->root.type == bfd_link_hash_defined
3393 || h->root.type == bfd_link_hash_defweak))
3394 {
3395 /* We don't want to issue this warning. Clobber
3396 the section size so that the warning does not
3397 get copied into the output file. */
eea6121a 3398 s->size = 0;
4ad4eba5
AM
3399 continue;
3400 }
3401 }
3402
eea6121a 3403 sz = s->size;
370a0e1b 3404 msg = bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3405 if (msg == NULL)
3406 goto error_return;
3407
370a0e1b 3408 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3409 goto error_return;
3410
370a0e1b 3411 msg[sz] = '\0';
4ad4eba5
AM
3412
3413 if (! (_bfd_generic_link_add_one_symbol
3414 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3415 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3416 goto error_return;
3417
3418 if (! info->relocatable)
3419 {
3420 /* Clobber the section size so that the warning does
3421 not get copied into the output file. */
eea6121a 3422 s->size = 0;
11d2f718
AM
3423
3424 /* Also set SEC_EXCLUDE, so that symbols defined in
3425 the warning section don't get copied to the output. */
3426 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3427 }
3428 }
3429 }
3430 }
3431
3432 add_needed = TRUE;
3433 if (! dynamic)
3434 {
3435 /* If we are creating a shared library, create all the dynamic
3436 sections immediately. We need to attach them to something,
3437 so we attach them to this BFD, provided it is the right
3438 format. FIXME: If there are no input BFD's of the same
3439 format as the output, we can't make a shared library. */
3440 if (info->shared
66eb6687 3441 && is_elf_hash_table (htab)
f13a99db 3442 && info->output_bfd->xvec == abfd->xvec
66eb6687 3443 && !htab->dynamic_sections_created)
4ad4eba5
AM
3444 {
3445 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3446 goto error_return;
3447 }
3448 }
66eb6687 3449 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3450 goto error_return;
3451 else
3452 {
3453 asection *s;
3454 const char *soname = NULL;
3455 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3456 int ret;
3457
3458 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3459 ld shouldn't allow it. */
4ad4eba5
AM
3460 if ((s = abfd->sections) != NULL
3461 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3462 abort ();
4ad4eba5
AM
3463
3464 /* If this dynamic lib was specified on the command line with
3465 --as-needed in effect, then we don't want to add a DT_NEEDED
3466 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3467 in by another lib's DT_NEEDED. When --no-add-needed is used
3468 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3469 any dynamic library in DT_NEEDED tags in the dynamic lib at
3470 all. */
3471 add_needed = (elf_dyn_lib_class (abfd)
3472 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3473 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3474
3475 s = bfd_get_section_by_name (abfd, ".dynamic");
3476 if (s != NULL)
3477 {
3478 bfd_byte *dynbuf;
3479 bfd_byte *extdyn;
cb33740c 3480 unsigned int elfsec;
4ad4eba5
AM
3481 unsigned long shlink;
3482
eea6121a 3483 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4ad4eba5
AM
3484 goto error_free_dyn;
3485
3486 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3487 if (elfsec == SHN_BAD)
4ad4eba5
AM
3488 goto error_free_dyn;
3489 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3490
3491 for (extdyn = dynbuf;
eea6121a 3492 extdyn < dynbuf + s->size;
4ad4eba5
AM
3493 extdyn += bed->s->sizeof_dyn)
3494 {
3495 Elf_Internal_Dyn dyn;
3496
3497 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3498 if (dyn.d_tag == DT_SONAME)
3499 {
3500 unsigned int tagv = dyn.d_un.d_val;
3501 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3502 if (soname == NULL)
3503 goto error_free_dyn;
3504 }
3505 if (dyn.d_tag == DT_NEEDED)
3506 {
3507 struct bfd_link_needed_list *n, **pn;
3508 char *fnm, *anm;
3509 unsigned int tagv = dyn.d_un.d_val;
3510
3511 amt = sizeof (struct bfd_link_needed_list);
3512 n = bfd_alloc (abfd, amt);
3513 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3514 if (n == NULL || fnm == NULL)
3515 goto error_free_dyn;
3516 amt = strlen (fnm) + 1;
3517 anm = bfd_alloc (abfd, amt);
3518 if (anm == NULL)
3519 goto error_free_dyn;
3520 memcpy (anm, fnm, amt);
3521 n->name = anm;
3522 n->by = abfd;
3523 n->next = NULL;
66eb6687 3524 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3525 ;
3526 *pn = n;
3527 }
3528 if (dyn.d_tag == DT_RUNPATH)
3529 {
3530 struct bfd_link_needed_list *n, **pn;
3531 char *fnm, *anm;
3532 unsigned int tagv = dyn.d_un.d_val;
3533
3534 amt = sizeof (struct bfd_link_needed_list);
3535 n = bfd_alloc (abfd, amt);
3536 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3537 if (n == NULL || fnm == NULL)
3538 goto error_free_dyn;
3539 amt = strlen (fnm) + 1;
3540 anm = bfd_alloc (abfd, amt);
3541 if (anm == NULL)
3542 goto error_free_dyn;
3543 memcpy (anm, fnm, amt);
3544 n->name = anm;
3545 n->by = abfd;
3546 n->next = NULL;
3547 for (pn = & runpath;
3548 *pn != NULL;
3549 pn = &(*pn)->next)
3550 ;
3551 *pn = n;
3552 }
3553 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3554 if (!runpath && dyn.d_tag == DT_RPATH)
3555 {
3556 struct bfd_link_needed_list *n, **pn;
3557 char *fnm, *anm;
3558 unsigned int tagv = dyn.d_un.d_val;
3559
3560 amt = sizeof (struct bfd_link_needed_list);
3561 n = bfd_alloc (abfd, amt);
3562 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3563 if (n == NULL || fnm == NULL)
3564 goto error_free_dyn;
3565 amt = strlen (fnm) + 1;
3566 anm = bfd_alloc (abfd, amt);
3567 if (anm == NULL)
3568 {
3569 error_free_dyn:
3570 free (dynbuf);
3571 goto error_return;
3572 }
3573 memcpy (anm, fnm, amt);
3574 n->name = anm;
3575 n->by = abfd;
3576 n->next = NULL;
3577 for (pn = & rpath;
3578 *pn != NULL;
3579 pn = &(*pn)->next)
3580 ;
3581 *pn = n;
3582 }
3583 }
3584
3585 free (dynbuf);
3586 }
3587
3588 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3589 frees all more recently bfd_alloc'd blocks as well. */
3590 if (runpath)
3591 rpath = runpath;
3592
3593 if (rpath)
3594 {
3595 struct bfd_link_needed_list **pn;
66eb6687 3596 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3597 ;
3598 *pn = rpath;
3599 }
3600
3601 /* We do not want to include any of the sections in a dynamic
3602 object in the output file. We hack by simply clobbering the
3603 list of sections in the BFD. This could be handled more
3604 cleanly by, say, a new section flag; the existing
3605 SEC_NEVER_LOAD flag is not the one we want, because that one
3606 still implies that the section takes up space in the output
3607 file. */
3608 bfd_section_list_clear (abfd);
3609
4ad4eba5
AM
3610 /* Find the name to use in a DT_NEEDED entry that refers to this
3611 object. If the object has a DT_SONAME entry, we use it.
3612 Otherwise, if the generic linker stuck something in
3613 elf_dt_name, we use that. Otherwise, we just use the file
3614 name. */
3615 if (soname == NULL || *soname == '\0')
3616 {
3617 soname = elf_dt_name (abfd);
3618 if (soname == NULL || *soname == '\0')
3619 soname = bfd_get_filename (abfd);
3620 }
3621
3622 /* Save the SONAME because sometimes the linker emulation code
3623 will need to know it. */
3624 elf_dt_name (abfd) = soname;
3625
7e9f0867 3626 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3627 if (ret < 0)
3628 goto error_return;
3629
3630 /* If we have already included this dynamic object in the
3631 link, just ignore it. There is no reason to include a
3632 particular dynamic object more than once. */
3633 if (ret > 0)
3634 return TRUE;
3635 }
3636
3637 /* If this is a dynamic object, we always link against the .dynsym
3638 symbol table, not the .symtab symbol table. The dynamic linker
3639 will only see the .dynsym symbol table, so there is no reason to
3640 look at .symtab for a dynamic object. */
3641
3642 if (! dynamic || elf_dynsymtab (abfd) == 0)
3643 hdr = &elf_tdata (abfd)->symtab_hdr;
3644 else
3645 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3646
3647 symcount = hdr->sh_size / bed->s->sizeof_sym;
3648
3649 /* The sh_info field of the symtab header tells us where the
3650 external symbols start. We don't care about the local symbols at
3651 this point. */
3652 if (elf_bad_symtab (abfd))
3653 {
3654 extsymcount = symcount;
3655 extsymoff = 0;
3656 }
3657 else
3658 {
3659 extsymcount = symcount - hdr->sh_info;
3660 extsymoff = hdr->sh_info;
3661 }
3662
3663 sym_hash = NULL;
3664 if (extsymcount != 0)
3665 {
3666 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3667 NULL, NULL, NULL);
3668 if (isymbuf == NULL)
3669 goto error_return;
3670
3671 /* We store a pointer to the hash table entry for each external
3672 symbol. */
3673 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3674 sym_hash = bfd_alloc (abfd, amt);
3675 if (sym_hash == NULL)
3676 goto error_free_sym;
3677 elf_sym_hashes (abfd) = sym_hash;
3678 }
3679
3680 if (dynamic)
3681 {
3682 /* Read in any version definitions. */
fc0e6df6
PB
3683 if (!_bfd_elf_slurp_version_tables (abfd,
3684 info->default_imported_symver))
4ad4eba5
AM
3685 goto error_free_sym;
3686
3687 /* Read in the symbol versions, but don't bother to convert them
3688 to internal format. */
3689 if (elf_dynversym (abfd) != 0)
3690 {
3691 Elf_Internal_Shdr *versymhdr;
3692
3693 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3694 extversym = bfd_malloc (versymhdr->sh_size);
3695 if (extversym == NULL)
3696 goto error_free_sym;
3697 amt = versymhdr->sh_size;
3698 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3699 || bfd_bread (extversym, amt, abfd) != amt)
3700 goto error_free_vers;
3701 }
3702 }
3703
66eb6687
AM
3704 /* If we are loading an as-needed shared lib, save the symbol table
3705 state before we start adding symbols. If the lib turns out
3706 to be unneeded, restore the state. */
3707 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3708 {
3709 unsigned int i;
3710 size_t entsize;
3711
3712 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3713 {
3714 struct bfd_hash_entry *p;
2de92251 3715 struct elf_link_hash_entry *h;
66eb6687
AM
3716
3717 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3718 {
3719 h = (struct elf_link_hash_entry *) p;
3720 entsize += htab->root.table.entsize;
3721 if (h->root.type == bfd_link_hash_warning)
3722 entsize += htab->root.table.entsize;
3723 }
66eb6687
AM
3724 }
3725
3726 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3727 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3728 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3729 if (old_tab == NULL)
3730 goto error_free_vers;
3731
3732 /* Remember the current objalloc pointer, so that all mem for
3733 symbols added can later be reclaimed. */
3734 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3735 if (alloc_mark == NULL)
3736 goto error_free_vers;
3737
5061a885
AM
3738 /* Make a special call to the linker "notice" function to
3739 tell it that we are about to handle an as-needed lib. */
3740 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3741 notice_as_needed))
9af2a943 3742 goto error_free_vers;
5061a885 3743
66eb6687
AM
3744 /* Clone the symbol table and sym hashes. Remember some
3745 pointers into the symbol table, and dynamic symbol count. */
3746 old_hash = (char *) old_tab + tabsize;
3747 old_ent = (char *) old_hash + hashsize;
3748 memcpy (old_tab, htab->root.table.table, tabsize);
3749 memcpy (old_hash, sym_hash, hashsize);
3750 old_undefs = htab->root.undefs;
3751 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3752 old_table = htab->root.table.table;
3753 old_size = htab->root.table.size;
3754 old_count = htab->root.table.count;
66eb6687
AM
3755 old_dynsymcount = htab->dynsymcount;
3756
3757 for (i = 0; i < htab->root.table.size; i++)
3758 {
3759 struct bfd_hash_entry *p;
2de92251 3760 struct elf_link_hash_entry *h;
66eb6687
AM
3761
3762 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3763 {
3764 memcpy (old_ent, p, htab->root.table.entsize);
3765 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3766 h = (struct elf_link_hash_entry *) p;
3767 if (h->root.type == bfd_link_hash_warning)
3768 {
3769 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3770 old_ent = (char *) old_ent + htab->root.table.entsize;
3771 }
66eb6687
AM
3772 }
3773 }
3774 }
4ad4eba5 3775
66eb6687 3776 weaks = NULL;
4ad4eba5
AM
3777 ever = extversym != NULL ? extversym + extsymoff : NULL;
3778 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3779 isym < isymend;
3780 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3781 {
3782 int bind;
3783 bfd_vma value;
af44c138 3784 asection *sec, *new_sec;
4ad4eba5
AM
3785 flagword flags;
3786 const char *name;
3787 struct elf_link_hash_entry *h;
3788 bfd_boolean definition;
3789 bfd_boolean size_change_ok;
3790 bfd_boolean type_change_ok;
3791 bfd_boolean new_weakdef;
3792 bfd_boolean override;
a4d8e49b 3793 bfd_boolean common;
4ad4eba5
AM
3794 unsigned int old_alignment;
3795 bfd *old_bfd;
3796
3797 override = FALSE;
3798
3799 flags = BSF_NO_FLAGS;
3800 sec = NULL;
3801 value = isym->st_value;
3802 *sym_hash = NULL;
a4d8e49b 3803 common = bed->common_definition (isym);
4ad4eba5
AM
3804
3805 bind = ELF_ST_BIND (isym->st_info);
3806 if (bind == STB_LOCAL)
3807 {
3808 /* This should be impossible, since ELF requires that all
3809 global symbols follow all local symbols, and that sh_info
3810 point to the first global symbol. Unfortunately, Irix 5
3811 screws this up. */
3812 continue;
3813 }
3814 else if (bind == STB_GLOBAL)
3815 {
a4d8e49b 3816 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5
AM
3817 flags = BSF_GLOBAL;
3818 }
3819 else if (bind == STB_WEAK)
3820 flags = BSF_WEAK;
3821 else
3822 {
3823 /* Leave it up to the processor backend. */
3824 }
3825
3826 if (isym->st_shndx == SHN_UNDEF)
3827 sec = bfd_und_section_ptr;
cb33740c
AM
3828 else if (isym->st_shndx == SHN_ABS)
3829 sec = bfd_abs_section_ptr;
3830 else if (isym->st_shndx == SHN_COMMON)
3831 {
3832 sec = bfd_com_section_ptr;
3833 /* What ELF calls the size we call the value. What ELF
3834 calls the value we call the alignment. */
3835 value = isym->st_size;
3836 }
3837 else
4ad4eba5
AM
3838 {
3839 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3840 if (sec == NULL)
3841 sec = bfd_abs_section_ptr;
529fcb95
PB
3842 else if (sec->kept_section)
3843 {
e5d08002
L
3844 /* Symbols from discarded section are undefined. We keep
3845 its visibility. */
529fcb95
PB
3846 sec = bfd_und_section_ptr;
3847 isym->st_shndx = SHN_UNDEF;
3848 }
4ad4eba5
AM
3849 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3850 value -= sec->vma;
3851 }
4ad4eba5
AM
3852
3853 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3854 isym->st_name);
3855 if (name == NULL)
3856 goto error_free_vers;
3857
3858 if (isym->st_shndx == SHN_COMMON
6a4a0940
JJ
3859 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3860 && !info->relocatable)
4ad4eba5
AM
3861 {
3862 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3863
3864 if (tcomm == NULL)
3865 {
3496cb2a
L
3866 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3867 (SEC_ALLOC
3868 | SEC_IS_COMMON
3869 | SEC_LINKER_CREATED
3870 | SEC_THREAD_LOCAL));
3871 if (tcomm == NULL)
4ad4eba5
AM
3872 goto error_free_vers;
3873 }
3874 sec = tcomm;
3875 }
66eb6687 3876 else if (bed->elf_add_symbol_hook)
4ad4eba5 3877 {
66eb6687
AM
3878 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3879 &sec, &value))
4ad4eba5
AM
3880 goto error_free_vers;
3881
3882 /* The hook function sets the name to NULL if this symbol
3883 should be skipped for some reason. */
3884 if (name == NULL)
3885 continue;
3886 }
3887
3888 /* Sanity check that all possibilities were handled. */
3889 if (sec == NULL)
3890 {
3891 bfd_set_error (bfd_error_bad_value);
3892 goto error_free_vers;
3893 }
3894
3895 if (bfd_is_und_section (sec)
3896 || bfd_is_com_section (sec))
3897 definition = FALSE;
3898 else
3899 definition = TRUE;
3900
3901 size_change_ok = FALSE;
66eb6687 3902 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
3903 old_alignment = 0;
3904 old_bfd = NULL;
af44c138 3905 new_sec = sec;
4ad4eba5 3906
66eb6687 3907 if (is_elf_hash_table (htab))
4ad4eba5
AM
3908 {
3909 Elf_Internal_Versym iver;
3910 unsigned int vernum = 0;
3911 bfd_boolean skip;
3912
fc0e6df6 3913 if (ever == NULL)
4ad4eba5 3914 {
fc0e6df6
PB
3915 if (info->default_imported_symver)
3916 /* Use the default symbol version created earlier. */
3917 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3918 else
3919 iver.vs_vers = 0;
3920 }
3921 else
3922 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3923
3924 vernum = iver.vs_vers & VERSYM_VERSION;
3925
3926 /* If this is a hidden symbol, or if it is not version
3927 1, we append the version name to the symbol name.
cc86ff91
EB
3928 However, we do not modify a non-hidden absolute symbol
3929 if it is not a function, because it might be the version
3930 symbol itself. FIXME: What if it isn't? */
fc0e6df6 3931 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
3932 || (vernum > 1
3933 && (!bfd_is_abs_section (sec)
3934 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
3935 {
3936 const char *verstr;
3937 size_t namelen, verlen, newlen;
3938 char *newname, *p;
3939
3940 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 3941 {
fc0e6df6
PB
3942 if (vernum > elf_tdata (abfd)->cverdefs)
3943 verstr = NULL;
3944 else if (vernum > 1)
3945 verstr =
3946 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3947 else
3948 verstr = "";
4ad4eba5 3949
fc0e6df6 3950 if (verstr == NULL)
4ad4eba5 3951 {
fc0e6df6
PB
3952 (*_bfd_error_handler)
3953 (_("%B: %s: invalid version %u (max %d)"),
3954 abfd, name, vernum,
3955 elf_tdata (abfd)->cverdefs);
3956 bfd_set_error (bfd_error_bad_value);
3957 goto error_free_vers;
4ad4eba5 3958 }
fc0e6df6
PB
3959 }
3960 else
3961 {
3962 /* We cannot simply test for the number of
3963 entries in the VERNEED section since the
3964 numbers for the needed versions do not start
3965 at 0. */
3966 Elf_Internal_Verneed *t;
3967
3968 verstr = NULL;
3969 for (t = elf_tdata (abfd)->verref;
3970 t != NULL;
3971 t = t->vn_nextref)
4ad4eba5 3972 {
fc0e6df6 3973 Elf_Internal_Vernaux *a;
4ad4eba5 3974
fc0e6df6
PB
3975 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3976 {
3977 if (a->vna_other == vernum)
4ad4eba5 3978 {
fc0e6df6
PB
3979 verstr = a->vna_nodename;
3980 break;
4ad4eba5 3981 }
4ad4eba5 3982 }
fc0e6df6
PB
3983 if (a != NULL)
3984 break;
3985 }
3986 if (verstr == NULL)
3987 {
3988 (*_bfd_error_handler)
3989 (_("%B: %s: invalid needed version %d"),
3990 abfd, name, vernum);
3991 bfd_set_error (bfd_error_bad_value);
3992 goto error_free_vers;
4ad4eba5 3993 }
4ad4eba5 3994 }
fc0e6df6
PB
3995
3996 namelen = strlen (name);
3997 verlen = strlen (verstr);
3998 newlen = namelen + verlen + 2;
3999 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4000 && isym->st_shndx != SHN_UNDEF)
4001 ++newlen;
4002
66eb6687 4003 newname = bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4004 if (newname == NULL)
4005 goto error_free_vers;
4006 memcpy (newname, name, namelen);
4007 p = newname + namelen;
4008 *p++ = ELF_VER_CHR;
4009 /* If this is a defined non-hidden version symbol,
4010 we add another @ to the name. This indicates the
4011 default version of the symbol. */
4012 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4013 && isym->st_shndx != SHN_UNDEF)
4014 *p++ = ELF_VER_CHR;
4015 memcpy (p, verstr, verlen + 1);
4016
4017 name = newname;
4ad4eba5
AM
4018 }
4019
af44c138
L
4020 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4021 &value, &old_alignment,
4ad4eba5
AM
4022 sym_hash, &skip, &override,
4023 &type_change_ok, &size_change_ok))
4024 goto error_free_vers;
4025
4026 if (skip)
4027 continue;
4028
4029 if (override)
4030 definition = FALSE;
4031
4032 h = *sym_hash;
4033 while (h->root.type == bfd_link_hash_indirect
4034 || h->root.type == bfd_link_hash_warning)
4035 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4036
4037 /* Remember the old alignment if this is a common symbol, so
4038 that we don't reduce the alignment later on. We can't
4039 check later, because _bfd_generic_link_add_one_symbol
4040 will set a default for the alignment which we want to
4041 override. We also remember the old bfd where the existing
4042 definition comes from. */
4043 switch (h->root.type)
4044 {
4045 default:
4046 break;
4047
4048 case bfd_link_hash_defined:
4049 case bfd_link_hash_defweak:
4050 old_bfd = h->root.u.def.section->owner;
4051 break;
4052
4053 case bfd_link_hash_common:
4054 old_bfd = h->root.u.c.p->section->owner;
4055 old_alignment = h->root.u.c.p->alignment_power;
4056 break;
4057 }
4058
4059 if (elf_tdata (abfd)->verdef != NULL
4060 && ! override
4061 && vernum > 1
4062 && definition)
4063 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4064 }
4065
4066 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4067 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4068 (struct bfd_link_hash_entry **) sym_hash)))
4069 goto error_free_vers;
4070
4071 h = *sym_hash;
4072 while (h->root.type == bfd_link_hash_indirect
4073 || h->root.type == bfd_link_hash_warning)
4074 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4075 *sym_hash = h;
4076
4077 new_weakdef = FALSE;
4078 if (dynamic
4079 && definition
4080 && (flags & BSF_WEAK) != 0
fcb93ecf 4081 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4082 && is_elf_hash_table (htab)
f6e332e6 4083 && h->u.weakdef == NULL)
4ad4eba5
AM
4084 {
4085 /* Keep a list of all weak defined non function symbols from
4086 a dynamic object, using the weakdef field. Later in this
4087 function we will set the weakdef field to the correct
4088 value. We only put non-function symbols from dynamic
4089 objects on this list, because that happens to be the only
4090 time we need to know the normal symbol corresponding to a
4091 weak symbol, and the information is time consuming to
4092 figure out. If the weakdef field is not already NULL,
4093 then this symbol was already defined by some previous
4094 dynamic object, and we will be using that previous
4095 definition anyhow. */
4096
f6e332e6 4097 h->u.weakdef = weaks;
4ad4eba5
AM
4098 weaks = h;
4099 new_weakdef = TRUE;
4100 }
4101
4102 /* Set the alignment of a common symbol. */
a4d8e49b 4103 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4104 && h->root.type == bfd_link_hash_common)
4105 {
4106 unsigned int align;
4107
a4d8e49b 4108 if (common)
af44c138
L
4109 align = bfd_log2 (isym->st_value);
4110 else
4111 {
4112 /* The new symbol is a common symbol in a shared object.
4113 We need to get the alignment from the section. */
4114 align = new_sec->alignment_power;
4115 }
4ad4eba5
AM
4116 if (align > old_alignment
4117 /* Permit an alignment power of zero if an alignment of one
4118 is specified and no other alignments have been specified. */
4119 || (isym->st_value == 1 && old_alignment == 0))
4120 h->root.u.c.p->alignment_power = align;
4121 else
4122 h->root.u.c.p->alignment_power = old_alignment;
4123 }
4124
66eb6687 4125 if (is_elf_hash_table (htab))
4ad4eba5 4126 {
4ad4eba5 4127 bfd_boolean dynsym;
4ad4eba5
AM
4128
4129 /* Check the alignment when a common symbol is involved. This
4130 can change when a common symbol is overridden by a normal
4131 definition or a common symbol is ignored due to the old
4132 normal definition. We need to make sure the maximum
4133 alignment is maintained. */
a4d8e49b 4134 if ((old_alignment || common)
4ad4eba5
AM
4135 && h->root.type != bfd_link_hash_common)
4136 {
4137 unsigned int common_align;
4138 unsigned int normal_align;
4139 unsigned int symbol_align;
4140 bfd *normal_bfd;
4141 bfd *common_bfd;
4142
4143 symbol_align = ffs (h->root.u.def.value) - 1;
4144 if (h->root.u.def.section->owner != NULL
4145 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4146 {
4147 normal_align = h->root.u.def.section->alignment_power;
4148 if (normal_align > symbol_align)
4149 normal_align = symbol_align;
4150 }
4151 else
4152 normal_align = symbol_align;
4153
4154 if (old_alignment)
4155 {
4156 common_align = old_alignment;
4157 common_bfd = old_bfd;
4158 normal_bfd = abfd;
4159 }
4160 else
4161 {
4162 common_align = bfd_log2 (isym->st_value);
4163 common_bfd = abfd;
4164 normal_bfd = old_bfd;
4165 }
4166
4167 if (normal_align < common_align)
d07676f8
NC
4168 {
4169 /* PR binutils/2735 */
4170 if (normal_bfd == NULL)
4171 (*_bfd_error_handler)
4172 (_("Warning: alignment %u of common symbol `%s' in %B"
4173 " is greater than the alignment (%u) of its section %A"),
4174 common_bfd, h->root.u.def.section,
4175 1 << common_align, name, 1 << normal_align);
4176 else
4177 (*_bfd_error_handler)
4178 (_("Warning: alignment %u of symbol `%s' in %B"
4179 " is smaller than %u in %B"),
4180 normal_bfd, common_bfd,
4181 1 << normal_align, name, 1 << common_align);
4182 }
4ad4eba5
AM
4183 }
4184
83ad0046
L
4185 /* Remember the symbol size if it isn't undefined. */
4186 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4187 && (definition || h->size == 0))
4188 {
83ad0046
L
4189 if (h->size != 0
4190 && h->size != isym->st_size
4191 && ! size_change_ok)
4ad4eba5 4192 (*_bfd_error_handler)
d003868e
AM
4193 (_("Warning: size of symbol `%s' changed"
4194 " from %lu in %B to %lu in %B"),
4195 old_bfd, abfd,
4ad4eba5 4196 name, (unsigned long) h->size,
d003868e 4197 (unsigned long) isym->st_size);
4ad4eba5
AM
4198
4199 h->size = isym->st_size;
4200 }
4201
4202 /* If this is a common symbol, then we always want H->SIZE
4203 to be the size of the common symbol. The code just above
4204 won't fix the size if a common symbol becomes larger. We
4205 don't warn about a size change here, because that is
fcb93ecf
PB
4206 covered by --warn-common. Allow changed between different
4207 function types. */
4ad4eba5
AM
4208 if (h->root.type == bfd_link_hash_common)
4209 h->size = h->root.u.c.size;
4210
4211 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4212 && (definition || h->type == STT_NOTYPE))
4213 {
4214 if (h->type != STT_NOTYPE
4215 && h->type != ELF_ST_TYPE (isym->st_info)
4216 && ! type_change_ok)
4217 (*_bfd_error_handler)
d003868e
AM
4218 (_("Warning: type of symbol `%s' changed"
4219 " from %d to %d in %B"),
4220 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4ad4eba5
AM
4221
4222 h->type = ELF_ST_TYPE (isym->st_info);
4223 }
4224
4225 /* If st_other has a processor-specific meaning, specific
4226 code might be needed here. We never merge the visibility
4227 attribute with the one from a dynamic object. */
4228 if (bed->elf_backend_merge_symbol_attribute)
4229 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4230 dynamic);
4231
b58f81ae
DJ
4232 /* If this symbol has default visibility and the user has requested
4233 we not re-export it, then mark it as hidden. */
4234 if (definition && !dynamic
4235 && (abfd->no_export
4236 || (abfd->my_archive && abfd->my_archive->no_export))
4237 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
66eb6687
AM
4238 isym->st_other = (STV_HIDDEN
4239 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
b58f81ae 4240
8992f0d7 4241 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4ad4eba5
AM
4242 {
4243 unsigned char hvis, symvis, other, nvis;
4244
8992f0d7
TS
4245 /* Only merge the visibility. Leave the remainder of the
4246 st_other field to elf_backend_merge_symbol_attribute. */
4247 other = h->other & ~ELF_ST_VISIBILITY (-1);
4ad4eba5
AM
4248
4249 /* Combine visibilities, using the most constraining one. */
4250 hvis = ELF_ST_VISIBILITY (h->other);
4251 symvis = ELF_ST_VISIBILITY (isym->st_other);
4252 if (! hvis)
4253 nvis = symvis;
4254 else if (! symvis)
4255 nvis = hvis;
4256 else
4257 nvis = hvis < symvis ? hvis : symvis;
4258
4259 h->other = other | nvis;
4260 }
4261
4262 /* Set a flag in the hash table entry indicating the type of
4263 reference or definition we just found. Keep a count of
4264 the number of dynamic symbols we find. A dynamic symbol
4265 is one which is referenced or defined by both a regular
4266 object and a shared object. */
4ad4eba5
AM
4267 dynsym = FALSE;
4268 if (! dynamic)
4269 {
4270 if (! definition)
4271 {
f5385ebf 4272 h->ref_regular = 1;
4ad4eba5 4273 if (bind != STB_WEAK)
f5385ebf 4274 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4275 }
4276 else
f5385ebf 4277 h->def_regular = 1;
4ad4eba5 4278 if (! info->executable
f5385ebf
AM
4279 || h->def_dynamic
4280 || h->ref_dynamic)
4ad4eba5
AM
4281 dynsym = TRUE;
4282 }
4283 else
4284 {
4285 if (! definition)
f5385ebf 4286 h->ref_dynamic = 1;
4ad4eba5 4287 else
f5385ebf
AM
4288 h->def_dynamic = 1;
4289 if (h->def_regular
4290 || h->ref_regular
f6e332e6 4291 || (h->u.weakdef != NULL
4ad4eba5 4292 && ! new_weakdef
f6e332e6 4293 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4294 dynsym = TRUE;
4295 }
4296
b2064611 4297 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4298 {
4299 /* We don't want to make debug symbol dynamic. */
4300 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4301 dynsym = FALSE;
4302 }
4303
4ad4eba5
AM
4304 /* Check to see if we need to add an indirect symbol for
4305 the default name. */
4306 if (definition || h->root.type == bfd_link_hash_common)
4307 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4308 &sec, &value, &dynsym,
4309 override))
4310 goto error_free_vers;
4311
4312 if (definition && !dynamic)
4313 {
4314 char *p = strchr (name, ELF_VER_CHR);
4315 if (p != NULL && p[1] != ELF_VER_CHR)
4316 {
4317 /* Queue non-default versions so that .symver x, x@FOO
4318 aliases can be checked. */
66eb6687 4319 if (!nondeflt_vers)
4ad4eba5 4320 {
66eb6687
AM
4321 amt = ((isymend - isym + 1)
4322 * sizeof (struct elf_link_hash_entry *));
4ad4eba5 4323 nondeflt_vers = bfd_malloc (amt);
14b1c01e
AM
4324 if (!nondeflt_vers)
4325 goto error_free_vers;
4ad4eba5 4326 }
66eb6687 4327 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4328 }
4329 }
4330
4331 if (dynsym && h->dynindx == -1)
4332 {
c152c796 4333 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4334 goto error_free_vers;
f6e332e6 4335 if (h->u.weakdef != NULL
4ad4eba5 4336 && ! new_weakdef
f6e332e6 4337 && h->u.weakdef->dynindx == -1)
4ad4eba5 4338 {
66eb6687 4339 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4340 goto error_free_vers;
4341 }
4342 }
4343 else if (dynsym && h->dynindx != -1)
4344 /* If the symbol already has a dynamic index, but
4345 visibility says it should not be visible, turn it into
4346 a local symbol. */
4347 switch (ELF_ST_VISIBILITY (h->other))
4348 {
4349 case STV_INTERNAL:
4350 case STV_HIDDEN:
4351 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4352 dynsym = FALSE;
4353 break;
4354 }
4355
4356 if (!add_needed
4357 && definition
4358 && dynsym
f5385ebf 4359 && h->ref_regular)
4ad4eba5
AM
4360 {
4361 int ret;
4362 const char *soname = elf_dt_name (abfd);
4363
4364 /* A symbol from a library loaded via DT_NEEDED of some
4365 other library is referenced by a regular object.
e56f61be
L
4366 Add a DT_NEEDED entry for it. Issue an error if
4367 --no-add-needed is used. */
4368 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4369 {
4370 (*_bfd_error_handler)
4371 (_("%s: invalid DSO for symbol `%s' definition"),
d003868e 4372 abfd, name);
e56f61be
L
4373 bfd_set_error (bfd_error_bad_value);
4374 goto error_free_vers;
4375 }
4376
a5db907e
AM
4377 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4378
4ad4eba5 4379 add_needed = TRUE;
7e9f0867 4380 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4381 if (ret < 0)
4382 goto error_free_vers;
4383
4384 BFD_ASSERT (ret == 0);
4385 }
4386 }
4387 }
4388
66eb6687
AM
4389 if (extversym != NULL)
4390 {
4391 free (extversym);
4392 extversym = NULL;
4393 }
4394
4395 if (isymbuf != NULL)
4396 {
4397 free (isymbuf);
4398 isymbuf = NULL;
4399 }
4400
4401 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4402 {
4403 unsigned int i;
4404
4405 /* Restore the symbol table. */
97fed1c9
JJ
4406 if (bed->as_needed_cleanup)
4407 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4408 old_hash = (char *) old_tab + tabsize;
4409 old_ent = (char *) old_hash + hashsize;
4410 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4411 htab->root.table.table = old_table;
4412 htab->root.table.size = old_size;
4413 htab->root.table.count = old_count;
66eb6687
AM
4414 memcpy (htab->root.table.table, old_tab, tabsize);
4415 memcpy (sym_hash, old_hash, hashsize);
4416 htab->root.undefs = old_undefs;
4417 htab->root.undefs_tail = old_undefs_tail;
4418 for (i = 0; i < htab->root.table.size; i++)
4419 {
4420 struct bfd_hash_entry *p;
4421 struct elf_link_hash_entry *h;
4422
4423 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4424 {
4425 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4426 if (h->root.type == bfd_link_hash_warning)
4427 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4428 if (h->dynindx >= old_dynsymcount)
4429 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4430
66eb6687
AM
4431 memcpy (p, old_ent, htab->root.table.entsize);
4432 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4433 h = (struct elf_link_hash_entry *) p;
4434 if (h->root.type == bfd_link_hash_warning)
4435 {
4436 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4437 old_ent = (char *) old_ent + htab->root.table.entsize;
4438 }
66eb6687
AM
4439 }
4440 }
4441
5061a885
AM
4442 /* Make a special call to the linker "notice" function to
4443 tell it that symbols added for crefs may need to be removed. */
4444 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4445 notice_not_needed))
9af2a943 4446 goto error_free_vers;
5061a885 4447
66eb6687
AM
4448 free (old_tab);
4449 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4450 alloc_mark);
4451 if (nondeflt_vers != NULL)
4452 free (nondeflt_vers);
4453 return TRUE;
4454 }
2de92251 4455
66eb6687
AM
4456 if (old_tab != NULL)
4457 {
5061a885
AM
4458 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4459 notice_needed))
9af2a943 4460 goto error_free_vers;
66eb6687
AM
4461 free (old_tab);
4462 old_tab = NULL;
4463 }
4464
4ad4eba5
AM
4465 /* Now that all the symbols from this input file are created, handle
4466 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4467 if (nondeflt_vers != NULL)
4468 {
4469 bfd_size_type cnt, symidx;
4470
4471 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4472 {
4473 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4474 char *shortname, *p;
4475
4476 p = strchr (h->root.root.string, ELF_VER_CHR);
4477 if (p == NULL
4478 || (h->root.type != bfd_link_hash_defined
4479 && h->root.type != bfd_link_hash_defweak))
4480 continue;
4481
4482 amt = p - h->root.root.string;
4483 shortname = bfd_malloc (amt + 1);
14b1c01e
AM
4484 if (!shortname)
4485 goto error_free_vers;
4ad4eba5
AM
4486 memcpy (shortname, h->root.root.string, amt);
4487 shortname[amt] = '\0';
4488
4489 hi = (struct elf_link_hash_entry *)
66eb6687 4490 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4491 FALSE, FALSE, FALSE);
4492 if (hi != NULL
4493 && hi->root.type == h->root.type
4494 && hi->root.u.def.value == h->root.u.def.value
4495 && hi->root.u.def.section == h->root.u.def.section)
4496 {
4497 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4498 hi->root.type = bfd_link_hash_indirect;
4499 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4500 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4501 sym_hash = elf_sym_hashes (abfd);
4502 if (sym_hash)
4503 for (symidx = 0; symidx < extsymcount; ++symidx)
4504 if (sym_hash[symidx] == hi)
4505 {
4506 sym_hash[symidx] = h;
4507 break;
4508 }
4509 }
4510 free (shortname);
4511 }
4512 free (nondeflt_vers);
4513 nondeflt_vers = NULL;
4514 }
4515
4ad4eba5
AM
4516 /* Now set the weakdefs field correctly for all the weak defined
4517 symbols we found. The only way to do this is to search all the
4518 symbols. Since we only need the information for non functions in
4519 dynamic objects, that's the only time we actually put anything on
4520 the list WEAKS. We need this information so that if a regular
4521 object refers to a symbol defined weakly in a dynamic object, the
4522 real symbol in the dynamic object is also put in the dynamic
4523 symbols; we also must arrange for both symbols to point to the
4524 same memory location. We could handle the general case of symbol
4525 aliasing, but a general symbol alias can only be generated in
4526 assembler code, handling it correctly would be very time
4527 consuming, and other ELF linkers don't handle general aliasing
4528 either. */
4529 if (weaks != NULL)
4530 {
4531 struct elf_link_hash_entry **hpp;
4532 struct elf_link_hash_entry **hppend;
4533 struct elf_link_hash_entry **sorted_sym_hash;
4534 struct elf_link_hash_entry *h;
4535 size_t sym_count;
4536
4537 /* Since we have to search the whole symbol list for each weak
4538 defined symbol, search time for N weak defined symbols will be
4539 O(N^2). Binary search will cut it down to O(NlogN). */
4540 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4541 sorted_sym_hash = bfd_malloc (amt);
4542 if (sorted_sym_hash == NULL)
4543 goto error_return;
4544 sym_hash = sorted_sym_hash;
4545 hpp = elf_sym_hashes (abfd);
4546 hppend = hpp + extsymcount;
4547 sym_count = 0;
4548 for (; hpp < hppend; hpp++)
4549 {
4550 h = *hpp;
4551 if (h != NULL
4552 && h->root.type == bfd_link_hash_defined
fcb93ecf 4553 && !bed->is_function_type (h->type))
4ad4eba5
AM
4554 {
4555 *sym_hash = h;
4556 sym_hash++;
4557 sym_count++;
4558 }
4559 }
4560
4561 qsort (sorted_sym_hash, sym_count,
4562 sizeof (struct elf_link_hash_entry *),
4563 elf_sort_symbol);
4564
4565 while (weaks != NULL)
4566 {
4567 struct elf_link_hash_entry *hlook;
4568 asection *slook;
4569 bfd_vma vlook;
4570 long ilook;
4571 size_t i, j, idx;
4572
4573 hlook = weaks;
f6e332e6
AM
4574 weaks = hlook->u.weakdef;
4575 hlook->u.weakdef = NULL;
4ad4eba5
AM
4576
4577 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4578 || hlook->root.type == bfd_link_hash_defweak
4579 || hlook->root.type == bfd_link_hash_common
4580 || hlook->root.type == bfd_link_hash_indirect);
4581 slook = hlook->root.u.def.section;
4582 vlook = hlook->root.u.def.value;
4583
4584 ilook = -1;
4585 i = 0;
4586 j = sym_count;
4587 while (i < j)
4588 {
4589 bfd_signed_vma vdiff;
4590 idx = (i + j) / 2;
4591 h = sorted_sym_hash [idx];
4592 vdiff = vlook - h->root.u.def.value;
4593 if (vdiff < 0)
4594 j = idx;
4595 else if (vdiff > 0)
4596 i = idx + 1;
4597 else
4598 {
a9b881be 4599 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4600 if (sdiff < 0)
4601 j = idx;
4602 else if (sdiff > 0)
4603 i = idx + 1;
4604 else
4605 {
4606 ilook = idx;
4607 break;
4608 }
4609 }
4610 }
4611
4612 /* We didn't find a value/section match. */
4613 if (ilook == -1)
4614 continue;
4615
4616 for (i = ilook; i < sym_count; i++)
4617 {
4618 h = sorted_sym_hash [i];
4619
4620 /* Stop if value or section doesn't match. */
4621 if (h->root.u.def.value != vlook
4622 || h->root.u.def.section != slook)
4623 break;
4624 else if (h != hlook)
4625 {
f6e332e6 4626 hlook->u.weakdef = h;
4ad4eba5
AM
4627
4628 /* If the weak definition is in the list of dynamic
4629 symbols, make sure the real definition is put
4630 there as well. */
4631 if (hlook->dynindx != -1 && h->dynindx == -1)
4632 {
c152c796 4633 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4634 {
4635 err_free_sym_hash:
4636 free (sorted_sym_hash);
4637 goto error_return;
4638 }
4ad4eba5
AM
4639 }
4640
4641 /* If the real definition is in the list of dynamic
4642 symbols, make sure the weak definition is put
4643 there as well. If we don't do this, then the
4644 dynamic loader might not merge the entries for the
4645 real definition and the weak definition. */
4646 if (h->dynindx != -1 && hlook->dynindx == -1)
4647 {
c152c796 4648 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4649 goto err_free_sym_hash;
4ad4eba5
AM
4650 }
4651 break;
4652 }
4653 }
4654 }
4655
4656 free (sorted_sym_hash);
4657 }
4658
66eb6687
AM
4659 if (bed->check_directives)
4660 (*bed->check_directives) (abfd, info);
85fbca6a 4661
4ad4eba5
AM
4662 /* If this object is the same format as the output object, and it is
4663 not a shared library, then let the backend look through the
4664 relocs.
4665
4666 This is required to build global offset table entries and to
4667 arrange for dynamic relocs. It is not required for the
4668 particular common case of linking non PIC code, even when linking
4669 against shared libraries, but unfortunately there is no way of
4670 knowing whether an object file has been compiled PIC or not.
4671 Looking through the relocs is not particularly time consuming.
4672 The problem is that we must either (1) keep the relocs in memory,
4673 which causes the linker to require additional runtime memory or
4674 (2) read the relocs twice from the input file, which wastes time.
4675 This would be a good case for using mmap.
4676
4677 I have no idea how to handle linking PIC code into a file of a
4678 different format. It probably can't be done. */
4ad4eba5 4679 if (! dynamic
66eb6687 4680 && is_elf_hash_table (htab)
13285a1b 4681 && bed->check_relocs != NULL
f13a99db 4682 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4683 {
4684 asection *o;
4685
4686 for (o = abfd->sections; o != NULL; o = o->next)
4687 {
4688 Elf_Internal_Rela *internal_relocs;
4689 bfd_boolean ok;
4690
4691 if ((o->flags & SEC_RELOC) == 0
4692 || o->reloc_count == 0
4693 || ((info->strip == strip_all || info->strip == strip_debugger)
4694 && (o->flags & SEC_DEBUGGING) != 0)
4695 || bfd_is_abs_section (o->output_section))
4696 continue;
4697
4698 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4699 info->keep_memory);
4700 if (internal_relocs == NULL)
4701 goto error_return;
4702
66eb6687 4703 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4704
4705 if (elf_section_data (o)->relocs != internal_relocs)
4706 free (internal_relocs);
4707
4708 if (! ok)
4709 goto error_return;
4710 }
4711 }
4712
4713 /* If this is a non-traditional link, try to optimize the handling
4714 of the .stab/.stabstr sections. */
4715 if (! dynamic
4716 && ! info->traditional_format
66eb6687 4717 && is_elf_hash_table (htab)
4ad4eba5
AM
4718 && (info->strip != strip_all && info->strip != strip_debugger))
4719 {
4720 asection *stabstr;
4721
4722 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4723 if (stabstr != NULL)
4724 {
4725 bfd_size_type string_offset = 0;
4726 asection *stab;
4727
4728 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4729 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4730 && (!stab->name[5] ||
4731 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4732 && (stab->flags & SEC_MERGE) == 0
4733 && !bfd_is_abs_section (stab->output_section))
4734 {
4735 struct bfd_elf_section_data *secdata;
4736
4737 secdata = elf_section_data (stab);
66eb6687
AM
4738 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4739 stabstr, &secdata->sec_info,
4ad4eba5
AM
4740 &string_offset))
4741 goto error_return;
4742 if (secdata->sec_info)
4743 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4744 }
4745 }
4746 }
4747
66eb6687 4748 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4749 {
4750 /* Add this bfd to the loaded list. */
4751 struct elf_link_loaded_list *n;
4752
4753 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4754 if (n == NULL)
4755 goto error_return;
4756 n->abfd = abfd;
66eb6687
AM
4757 n->next = htab->loaded;
4758 htab->loaded = n;
4ad4eba5
AM
4759 }
4760
4761 return TRUE;
4762
4763 error_free_vers:
66eb6687
AM
4764 if (old_tab != NULL)
4765 free (old_tab);
4ad4eba5
AM
4766 if (nondeflt_vers != NULL)
4767 free (nondeflt_vers);
4768 if (extversym != NULL)
4769 free (extversym);
4770 error_free_sym:
4771 if (isymbuf != NULL)
4772 free (isymbuf);
4773 error_return:
4774 return FALSE;
4775}
4776
8387904d
AM
4777/* Return the linker hash table entry of a symbol that might be
4778 satisfied by an archive symbol. Return -1 on error. */
4779
4780struct elf_link_hash_entry *
4781_bfd_elf_archive_symbol_lookup (bfd *abfd,
4782 struct bfd_link_info *info,
4783 const char *name)
4784{
4785 struct elf_link_hash_entry *h;
4786 char *p, *copy;
4787 size_t len, first;
4788
4789 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4790 if (h != NULL)
4791 return h;
4792
4793 /* If this is a default version (the name contains @@), look up the
4794 symbol again with only one `@' as well as without the version.
4795 The effect is that references to the symbol with and without the
4796 version will be matched by the default symbol in the archive. */
4797
4798 p = strchr (name, ELF_VER_CHR);
4799 if (p == NULL || p[1] != ELF_VER_CHR)
4800 return h;
4801
4802 /* First check with only one `@'. */
4803 len = strlen (name);
4804 copy = bfd_alloc (abfd, len);
4805 if (copy == NULL)
4806 return (struct elf_link_hash_entry *) 0 - 1;
4807
4808 first = p - name + 1;
4809 memcpy (copy, name, first);
4810 memcpy (copy + first, name + first + 1, len - first);
4811
4812 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4813 if (h == NULL)
4814 {
4815 /* We also need to check references to the symbol without the
4816 version. */
4817 copy[first - 1] = '\0';
4818 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4819 FALSE, FALSE, FALSE);
4820 }
4821
4822 bfd_release (abfd, copy);
4823 return h;
4824}
4825
0ad989f9
L
4826/* Add symbols from an ELF archive file to the linker hash table. We
4827 don't use _bfd_generic_link_add_archive_symbols because of a
4828 problem which arises on UnixWare. The UnixWare libc.so is an
4829 archive which includes an entry libc.so.1 which defines a bunch of
4830 symbols. The libc.so archive also includes a number of other
4831 object files, which also define symbols, some of which are the same
4832 as those defined in libc.so.1. Correct linking requires that we
4833 consider each object file in turn, and include it if it defines any
4834 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4835 this; it looks through the list of undefined symbols, and includes
4836 any object file which defines them. When this algorithm is used on
4837 UnixWare, it winds up pulling in libc.so.1 early and defining a
4838 bunch of symbols. This means that some of the other objects in the
4839 archive are not included in the link, which is incorrect since they
4840 precede libc.so.1 in the archive.
4841
4842 Fortunately, ELF archive handling is simpler than that done by
4843 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4844 oddities. In ELF, if we find a symbol in the archive map, and the
4845 symbol is currently undefined, we know that we must pull in that
4846 object file.
4847
4848 Unfortunately, we do have to make multiple passes over the symbol
4849 table until nothing further is resolved. */
4850
4ad4eba5
AM
4851static bfd_boolean
4852elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4853{
4854 symindex c;
4855 bfd_boolean *defined = NULL;
4856 bfd_boolean *included = NULL;
4857 carsym *symdefs;
4858 bfd_boolean loop;
4859 bfd_size_type amt;
8387904d
AM
4860 const struct elf_backend_data *bed;
4861 struct elf_link_hash_entry * (*archive_symbol_lookup)
4862 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4863
4864 if (! bfd_has_map (abfd))
4865 {
4866 /* An empty archive is a special case. */
4867 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4868 return TRUE;
4869 bfd_set_error (bfd_error_no_armap);
4870 return FALSE;
4871 }
4872
4873 /* Keep track of all symbols we know to be already defined, and all
4874 files we know to be already included. This is to speed up the
4875 second and subsequent passes. */
4876 c = bfd_ardata (abfd)->symdef_count;
4877 if (c == 0)
4878 return TRUE;
4879 amt = c;
4880 amt *= sizeof (bfd_boolean);
4881 defined = bfd_zmalloc (amt);
4882 included = bfd_zmalloc (amt);
4883 if (defined == NULL || included == NULL)
4884 goto error_return;
4885
4886 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4887 bed = get_elf_backend_data (abfd);
4888 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4889
4890 do
4891 {
4892 file_ptr last;
4893 symindex i;
4894 carsym *symdef;
4895 carsym *symdefend;
4896
4897 loop = FALSE;
4898 last = -1;
4899
4900 symdef = symdefs;
4901 symdefend = symdef + c;
4902 for (i = 0; symdef < symdefend; symdef++, i++)
4903 {
4904 struct elf_link_hash_entry *h;
4905 bfd *element;
4906 struct bfd_link_hash_entry *undefs_tail;
4907 symindex mark;
4908
4909 if (defined[i] || included[i])
4910 continue;
4911 if (symdef->file_offset == last)
4912 {
4913 included[i] = TRUE;
4914 continue;
4915 }
4916
8387904d
AM
4917 h = archive_symbol_lookup (abfd, info, symdef->name);
4918 if (h == (struct elf_link_hash_entry *) 0 - 1)
4919 goto error_return;
0ad989f9
L
4920
4921 if (h == NULL)
4922 continue;
4923
4924 if (h->root.type == bfd_link_hash_common)
4925 {
4926 /* We currently have a common symbol. The archive map contains
4927 a reference to this symbol, so we may want to include it. We
4928 only want to include it however, if this archive element
4929 contains a definition of the symbol, not just another common
4930 declaration of it.
4931
4932 Unfortunately some archivers (including GNU ar) will put
4933 declarations of common symbols into their archive maps, as
4934 well as real definitions, so we cannot just go by the archive
4935 map alone. Instead we must read in the element's symbol
4936 table and check that to see what kind of symbol definition
4937 this is. */
4938 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4939 continue;
4940 }
4941 else if (h->root.type != bfd_link_hash_undefined)
4942 {
4943 if (h->root.type != bfd_link_hash_undefweak)
4944 defined[i] = TRUE;
4945 continue;
4946 }
4947
4948 /* We need to include this archive member. */
4949 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4950 if (element == NULL)
4951 goto error_return;
4952
4953 if (! bfd_check_format (element, bfd_object))
4954 goto error_return;
4955
4956 /* Doublecheck that we have not included this object
4957 already--it should be impossible, but there may be
4958 something wrong with the archive. */
4959 if (element->archive_pass != 0)
4960 {
4961 bfd_set_error (bfd_error_bad_value);
4962 goto error_return;
4963 }
4964 element->archive_pass = 1;
4965
4966 undefs_tail = info->hash->undefs_tail;
4967
4968 if (! (*info->callbacks->add_archive_element) (info, element,
4969 symdef->name))
4970 goto error_return;
4971 if (! bfd_link_add_symbols (element, info))
4972 goto error_return;
4973
4974 /* If there are any new undefined symbols, we need to make
4975 another pass through the archive in order to see whether
4976 they can be defined. FIXME: This isn't perfect, because
4977 common symbols wind up on undefs_tail and because an
4978 undefined symbol which is defined later on in this pass
4979 does not require another pass. This isn't a bug, but it
4980 does make the code less efficient than it could be. */
4981 if (undefs_tail != info->hash->undefs_tail)
4982 loop = TRUE;
4983
4984 /* Look backward to mark all symbols from this object file
4985 which we have already seen in this pass. */
4986 mark = i;
4987 do
4988 {
4989 included[mark] = TRUE;
4990 if (mark == 0)
4991 break;
4992 --mark;
4993 }
4994 while (symdefs[mark].file_offset == symdef->file_offset);
4995
4996 /* We mark subsequent symbols from this object file as we go
4997 on through the loop. */
4998 last = symdef->file_offset;
4999 }
5000 }
5001 while (loop);
5002
5003 free (defined);
5004 free (included);
5005
5006 return TRUE;
5007
5008 error_return:
5009 if (defined != NULL)
5010 free (defined);
5011 if (included != NULL)
5012 free (included);
5013 return FALSE;
5014}
4ad4eba5
AM
5015
5016/* Given an ELF BFD, add symbols to the global hash table as
5017 appropriate. */
5018
5019bfd_boolean
5020bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5021{
5022 switch (bfd_get_format (abfd))
5023 {
5024 case bfd_object:
5025 return elf_link_add_object_symbols (abfd, info);
5026 case bfd_archive:
5027 return elf_link_add_archive_symbols (abfd, info);
5028 default:
5029 bfd_set_error (bfd_error_wrong_format);
5030 return FALSE;
5031 }
5032}
5a580b3a 5033\f
14b1c01e
AM
5034struct hash_codes_info
5035{
5036 unsigned long *hashcodes;
5037 bfd_boolean error;
5038};
a0c8462f 5039
5a580b3a
AM
5040/* This function will be called though elf_link_hash_traverse to store
5041 all hash value of the exported symbols in an array. */
5042
5043static bfd_boolean
5044elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5045{
14b1c01e 5046 struct hash_codes_info *inf = data;
5a580b3a
AM
5047 const char *name;
5048 char *p;
5049 unsigned long ha;
5050 char *alc = NULL;
5051
5052 if (h->root.type == bfd_link_hash_warning)
5053 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5054
5055 /* Ignore indirect symbols. These are added by the versioning code. */
5056 if (h->dynindx == -1)
5057 return TRUE;
5058
5059 name = h->root.root.string;
5060 p = strchr (name, ELF_VER_CHR);
5061 if (p != NULL)
5062 {
5063 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5064 if (alc == NULL)
5065 {
5066 inf->error = TRUE;
5067 return FALSE;
5068 }
5a580b3a
AM
5069 memcpy (alc, name, p - name);
5070 alc[p - name] = '\0';
5071 name = alc;
5072 }
5073
5074 /* Compute the hash value. */
5075 ha = bfd_elf_hash (name);
5076
5077 /* Store the found hash value in the array given as the argument. */
14b1c01e 5078 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5079
5080 /* And store it in the struct so that we can put it in the hash table
5081 later. */
f6e332e6 5082 h->u.elf_hash_value = ha;
5a580b3a
AM
5083
5084 if (alc != NULL)
5085 free (alc);
5086
5087 return TRUE;
5088}
5089
fdc90cb4
JJ
5090struct collect_gnu_hash_codes
5091{
5092 bfd *output_bfd;
5093 const struct elf_backend_data *bed;
5094 unsigned long int nsyms;
5095 unsigned long int maskbits;
5096 unsigned long int *hashcodes;
5097 unsigned long int *hashval;
5098 unsigned long int *indx;
5099 unsigned long int *counts;
5100 bfd_vma *bitmask;
5101 bfd_byte *contents;
5102 long int min_dynindx;
5103 unsigned long int bucketcount;
5104 unsigned long int symindx;
5105 long int local_indx;
5106 long int shift1, shift2;
5107 unsigned long int mask;
14b1c01e 5108 bfd_boolean error;
fdc90cb4
JJ
5109};
5110
5111/* This function will be called though elf_link_hash_traverse to store
5112 all hash value of the exported symbols in an array. */
5113
5114static bfd_boolean
5115elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5116{
5117 struct collect_gnu_hash_codes *s = data;
5118 const char *name;
5119 char *p;
5120 unsigned long ha;
5121 char *alc = NULL;
5122
5123 if (h->root.type == bfd_link_hash_warning)
5124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5125
5126 /* Ignore indirect symbols. These are added by the versioning code. */
5127 if (h->dynindx == -1)
5128 return TRUE;
5129
5130 /* Ignore also local symbols and undefined symbols. */
5131 if (! (*s->bed->elf_hash_symbol) (h))
5132 return TRUE;
5133
5134 name = h->root.root.string;
5135 p = strchr (name, ELF_VER_CHR);
5136 if (p != NULL)
5137 {
5138 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5139 if (alc == NULL)
5140 {
5141 s->error = TRUE;
5142 return FALSE;
5143 }
fdc90cb4
JJ
5144 memcpy (alc, name, p - name);
5145 alc[p - name] = '\0';
5146 name = alc;
5147 }
5148
5149 /* Compute the hash value. */
5150 ha = bfd_elf_gnu_hash (name);
5151
5152 /* Store the found hash value in the array for compute_bucket_count,
5153 and also for .dynsym reordering purposes. */
5154 s->hashcodes[s->nsyms] = ha;
5155 s->hashval[h->dynindx] = ha;
5156 ++s->nsyms;
5157 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5158 s->min_dynindx = h->dynindx;
5159
5160 if (alc != NULL)
5161 free (alc);
5162
5163 return TRUE;
5164}
5165
5166/* This function will be called though elf_link_hash_traverse to do
5167 final dynaminc symbol renumbering. */
5168
5169static bfd_boolean
5170elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5171{
5172 struct collect_gnu_hash_codes *s = data;
5173 unsigned long int bucket;
5174 unsigned long int val;
5175
5176 if (h->root.type == bfd_link_hash_warning)
5177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5178
5179 /* Ignore indirect symbols. */
5180 if (h->dynindx == -1)
5181 return TRUE;
5182
5183 /* Ignore also local symbols and undefined symbols. */
5184 if (! (*s->bed->elf_hash_symbol) (h))
5185 {
5186 if (h->dynindx >= s->min_dynindx)
5187 h->dynindx = s->local_indx++;
5188 return TRUE;
5189 }
5190
5191 bucket = s->hashval[h->dynindx] % s->bucketcount;
5192 val = (s->hashval[h->dynindx] >> s->shift1)
5193 & ((s->maskbits >> s->shift1) - 1);
5194 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5195 s->bitmask[val]
5196 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5197 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5198 if (s->counts[bucket] == 1)
5199 /* Last element terminates the chain. */
5200 val |= 1;
5201 bfd_put_32 (s->output_bfd, val,
5202 s->contents + (s->indx[bucket] - s->symindx) * 4);
5203 --s->counts[bucket];
5204 h->dynindx = s->indx[bucket]++;
5205 return TRUE;
5206}
5207
5208/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5209
5210bfd_boolean
5211_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5212{
5213 return !(h->forced_local
5214 || h->root.type == bfd_link_hash_undefined
5215 || h->root.type == bfd_link_hash_undefweak
5216 || ((h->root.type == bfd_link_hash_defined
5217 || h->root.type == bfd_link_hash_defweak)
5218 && h->root.u.def.section->output_section == NULL));
5219}
5220
5a580b3a
AM
5221/* Array used to determine the number of hash table buckets to use
5222 based on the number of symbols there are. If there are fewer than
5223 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5224 fewer than 37 we use 17 buckets, and so forth. We never use more
5225 than 32771 buckets. */
5226
5227static const size_t elf_buckets[] =
5228{
5229 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5230 16411, 32771, 0
5231};
5232
5233/* Compute bucket count for hashing table. We do not use a static set
5234 of possible tables sizes anymore. Instead we determine for all
5235 possible reasonable sizes of the table the outcome (i.e., the
5236 number of collisions etc) and choose the best solution. The
5237 weighting functions are not too simple to allow the table to grow
5238 without bounds. Instead one of the weighting factors is the size.
5239 Therefore the result is always a good payoff between few collisions
5240 (= short chain lengths) and table size. */
5241static size_t
d40f3da9
AM
5242compute_bucket_count (struct bfd_link_info *info,
5243 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5244 unsigned long int nsyms,
5245 int gnu_hash)
5a580b3a 5246{
5a580b3a 5247 size_t best_size = 0;
5a580b3a 5248 unsigned long int i;
5a580b3a 5249
5a580b3a
AM
5250 /* We have a problem here. The following code to optimize the table
5251 size requires an integer type with more the 32 bits. If
5252 BFD_HOST_U_64_BIT is set we know about such a type. */
5253#ifdef BFD_HOST_U_64_BIT
5254 if (info->optimize)
5255 {
5a580b3a
AM
5256 size_t minsize;
5257 size_t maxsize;
5258 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5259 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5260 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5261 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5262 unsigned long int *counts;
d40f3da9 5263 bfd_size_type amt;
5a580b3a
AM
5264
5265 /* Possible optimization parameters: if we have NSYMS symbols we say
5266 that the hashing table must at least have NSYMS/4 and at most
5267 2*NSYMS buckets. */
5268 minsize = nsyms / 4;
5269 if (minsize == 0)
5270 minsize = 1;
5271 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5272 if (gnu_hash)
5273 {
5274 if (minsize < 2)
5275 minsize = 2;
5276 if ((best_size & 31) == 0)
5277 ++best_size;
5278 }
5a580b3a
AM
5279
5280 /* Create array where we count the collisions in. We must use bfd_malloc
5281 since the size could be large. */
5282 amt = maxsize;
5283 amt *= sizeof (unsigned long int);
5284 counts = bfd_malloc (amt);
5285 if (counts == NULL)
fdc90cb4 5286 return 0;
5a580b3a
AM
5287
5288 /* Compute the "optimal" size for the hash table. The criteria is a
5289 minimal chain length. The minor criteria is (of course) the size
5290 of the table. */
5291 for (i = minsize; i < maxsize; ++i)
5292 {
5293 /* Walk through the array of hashcodes and count the collisions. */
5294 BFD_HOST_U_64_BIT max;
5295 unsigned long int j;
5296 unsigned long int fact;
5297
fdc90cb4
JJ
5298 if (gnu_hash && (i & 31) == 0)
5299 continue;
5300
5a580b3a
AM
5301 memset (counts, '\0', i * sizeof (unsigned long int));
5302
5303 /* Determine how often each hash bucket is used. */
5304 for (j = 0; j < nsyms; ++j)
5305 ++counts[hashcodes[j] % i];
5306
5307 /* For the weight function we need some information about the
5308 pagesize on the target. This is information need not be 100%
5309 accurate. Since this information is not available (so far) we
5310 define it here to a reasonable default value. If it is crucial
5311 to have a better value some day simply define this value. */
5312# ifndef BFD_TARGET_PAGESIZE
5313# define BFD_TARGET_PAGESIZE (4096)
5314# endif
5315
fdc90cb4
JJ
5316 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5317 and the chains. */
5318 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5319
5320# if 1
5321 /* Variant 1: optimize for short chains. We add the squares
5322 of all the chain lengths (which favors many small chain
5323 over a few long chains). */
5324 for (j = 0; j < i; ++j)
5325 max += counts[j] * counts[j];
5326
5327 /* This adds penalties for the overall size of the table. */
fdc90cb4 5328 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5329 max *= fact * fact;
5330# else
5331 /* Variant 2: Optimize a lot more for small table. Here we
5332 also add squares of the size but we also add penalties for
5333 empty slots (the +1 term). */
5334 for (j = 0; j < i; ++j)
5335 max += (1 + counts[j]) * (1 + counts[j]);
5336
5337 /* The overall size of the table is considered, but not as
5338 strong as in variant 1, where it is squared. */
fdc90cb4 5339 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5340 max *= fact;
5341# endif
5342
5343 /* Compare with current best results. */
5344 if (max < best_chlen)
5345 {
5346 best_chlen = max;
5347 best_size = i;
5348 }
5349 }
5350
5351 free (counts);
5352 }
5353 else
5354#endif /* defined (BFD_HOST_U_64_BIT) */
5355 {
5356 /* This is the fallback solution if no 64bit type is available or if we
5357 are not supposed to spend much time on optimizations. We select the
5358 bucket count using a fixed set of numbers. */
5359 for (i = 0; elf_buckets[i] != 0; i++)
5360 {
5361 best_size = elf_buckets[i];
fdc90cb4 5362 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5363 break;
5364 }
fdc90cb4
JJ
5365 if (gnu_hash && best_size < 2)
5366 best_size = 2;
5a580b3a
AM
5367 }
5368
5a580b3a
AM
5369 return best_size;
5370}
5371
5372/* Set up the sizes and contents of the ELF dynamic sections. This is
5373 called by the ELF linker emulation before_allocation routine. We
5374 must set the sizes of the sections before the linker sets the
5375 addresses of the various sections. */
5376
5377bfd_boolean
5378bfd_elf_size_dynamic_sections (bfd *output_bfd,
5379 const char *soname,
5380 const char *rpath,
5381 const char *filter_shlib,
5382 const char * const *auxiliary_filters,
5383 struct bfd_link_info *info,
5384 asection **sinterpptr,
5385 struct bfd_elf_version_tree *verdefs)
5386{
5387 bfd_size_type soname_indx;
5388 bfd *dynobj;
5389 const struct elf_backend_data *bed;
5390 struct elf_assign_sym_version_info asvinfo;
5391
5392 *sinterpptr = NULL;
5393
5394 soname_indx = (bfd_size_type) -1;
5395
5396 if (!is_elf_hash_table (info->hash))
5397 return TRUE;
5398
6bfdb61b 5399 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5400 if (info->execstack)
5401 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5402 else if (info->noexecstack)
5403 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5404 else
5405 {
5406 bfd *inputobj;
5407 asection *notesec = NULL;
5408 int exec = 0;
5409
5410 for (inputobj = info->input_bfds;
5411 inputobj;
5412 inputobj = inputobj->link_next)
5413 {
5414 asection *s;
5415
a94b9d2d 5416 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5a580b3a
AM
5417 continue;
5418 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5419 if (s)
5420 {
5421 if (s->flags & SEC_CODE)
5422 exec = PF_X;
5423 notesec = s;
5424 }
6bfdb61b 5425 else if (bed->default_execstack)
5a580b3a
AM
5426 exec = PF_X;
5427 }
5428 if (notesec)
5429 {
5430 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5431 if (exec && info->relocatable
5432 && notesec->output_section != bfd_abs_section_ptr)
5433 notesec->output_section->flags |= SEC_CODE;
5434 }
5435 }
5436
5437 /* Any syms created from now on start with -1 in
5438 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5439 elf_hash_table (info)->init_got_refcount
5440 = elf_hash_table (info)->init_got_offset;
5441 elf_hash_table (info)->init_plt_refcount
5442 = elf_hash_table (info)->init_plt_offset;
5a580b3a
AM
5443
5444 /* The backend may have to create some sections regardless of whether
5445 we're dynamic or not. */
5a580b3a
AM
5446 if (bed->elf_backend_always_size_sections
5447 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5448 return FALSE;
5449
eb3d5f3b
JB
5450 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5451 return FALSE;
5452
5a580b3a
AM
5453 dynobj = elf_hash_table (info)->dynobj;
5454
5455 /* If there were no dynamic objects in the link, there is nothing to
5456 do here. */
5457 if (dynobj == NULL)
5458 return TRUE;
5459
5a580b3a
AM
5460 if (elf_hash_table (info)->dynamic_sections_created)
5461 {
5462 struct elf_info_failed eif;
5463 struct elf_link_hash_entry *h;
5464 asection *dynstr;
5465 struct bfd_elf_version_tree *t;
5466 struct bfd_elf_version_expr *d;
046183de 5467 asection *s;
5a580b3a
AM
5468 bfd_boolean all_defined;
5469
5470 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5471 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5472
5473 if (soname != NULL)
5474 {
5475 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5476 soname, TRUE);
5477 if (soname_indx == (bfd_size_type) -1
5478 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5479 return FALSE;
5480 }
5481
5482 if (info->symbolic)
5483 {
5484 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5485 return FALSE;
5486 info->flags |= DF_SYMBOLIC;
5487 }
5488
5489 if (rpath != NULL)
5490 {
5491 bfd_size_type indx;
5492
5493 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5494 TRUE);
5495 if (indx == (bfd_size_type) -1
5496 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5497 return FALSE;
5498
5499 if (info->new_dtags)
5500 {
5501 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5502 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5503 return FALSE;
5504 }
5505 }
5506
5507 if (filter_shlib != NULL)
5508 {
5509 bfd_size_type indx;
5510
5511 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5512 filter_shlib, TRUE);
5513 if (indx == (bfd_size_type) -1
5514 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5515 return FALSE;
5516 }
5517
5518 if (auxiliary_filters != NULL)
5519 {
5520 const char * const *p;
5521
5522 for (p = auxiliary_filters; *p != NULL; p++)
5523 {
5524 bfd_size_type indx;
5525
5526 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5527 *p, TRUE);
5528 if (indx == (bfd_size_type) -1
5529 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5530 return FALSE;
5531 }
5532 }
5533
5534 eif.info = info;
5535 eif.verdefs = verdefs;
5536 eif.failed = FALSE;
5537
5538 /* If we are supposed to export all symbols into the dynamic symbol
5539 table (this is not the normal case), then do so. */
55255dae
L
5540 if (info->export_dynamic
5541 || (info->executable && info->dynamic))
5a580b3a
AM
5542 {
5543 elf_link_hash_traverse (elf_hash_table (info),
5544 _bfd_elf_export_symbol,
5545 &eif);
5546 if (eif.failed)
5547 return FALSE;
5548 }
5549
5550 /* Make all global versions with definition. */
5551 for (t = verdefs; t != NULL; t = t->next)
5552 for (d = t->globals.list; d != NULL; d = d->next)
5553 if (!d->symver && d->symbol)
5554 {
5555 const char *verstr, *name;
5556 size_t namelen, verlen, newlen;
5557 char *newname, *p;
5558 struct elf_link_hash_entry *newh;
5559
5560 name = d->symbol;
5561 namelen = strlen (name);
5562 verstr = t->name;
5563 verlen = strlen (verstr);
5564 newlen = namelen + verlen + 3;
5565
5566 newname = bfd_malloc (newlen);
5567 if (newname == NULL)
5568 return FALSE;
5569 memcpy (newname, name, namelen);
5570
5571 /* Check the hidden versioned definition. */
5572 p = newname + namelen;
5573 *p++ = ELF_VER_CHR;
5574 memcpy (p, verstr, verlen + 1);
5575 newh = elf_link_hash_lookup (elf_hash_table (info),
5576 newname, FALSE, FALSE,
5577 FALSE);
5578 if (newh == NULL
5579 || (newh->root.type != bfd_link_hash_defined
5580 && newh->root.type != bfd_link_hash_defweak))
5581 {
5582 /* Check the default versioned definition. */
5583 *p++ = ELF_VER_CHR;
5584 memcpy (p, verstr, verlen + 1);
5585 newh = elf_link_hash_lookup (elf_hash_table (info),
5586 newname, FALSE, FALSE,
5587 FALSE);
5588 }
5589 free (newname);
5590
5591 /* Mark this version if there is a definition and it is
5592 not defined in a shared object. */
5593 if (newh != NULL
f5385ebf 5594 && !newh->def_dynamic
5a580b3a
AM
5595 && (newh->root.type == bfd_link_hash_defined
5596 || newh->root.type == bfd_link_hash_defweak))
5597 d->symver = 1;
5598 }
5599
5600 /* Attach all the symbols to their version information. */
5601 asvinfo.output_bfd = output_bfd;
5602 asvinfo.info = info;
5603 asvinfo.verdefs = verdefs;
5604 asvinfo.failed = FALSE;
5605
5606 elf_link_hash_traverse (elf_hash_table (info),
5607 _bfd_elf_link_assign_sym_version,
5608 &asvinfo);
5609 if (asvinfo.failed)
5610 return FALSE;
5611
5612 if (!info->allow_undefined_version)
5613 {
5614 /* Check if all global versions have a definition. */
5615 all_defined = TRUE;
5616 for (t = verdefs; t != NULL; t = t->next)
5617 for (d = t->globals.list; d != NULL; d = d->next)
5618 if (!d->symver && !d->script)
5619 {
5620 (*_bfd_error_handler)
5621 (_("%s: undefined version: %s"),
5622 d->pattern, t->name);
5623 all_defined = FALSE;
5624 }
5625
5626 if (!all_defined)
5627 {
5628 bfd_set_error (bfd_error_bad_value);
5629 return FALSE;
5630 }
5631 }
5632
5633 /* Find all symbols which were defined in a dynamic object and make
5634 the backend pick a reasonable value for them. */
5635 elf_link_hash_traverse (elf_hash_table (info),
5636 _bfd_elf_adjust_dynamic_symbol,
5637 &eif);
5638 if (eif.failed)
5639 return FALSE;
5640
5641 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5642 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5643 now so that we know the final size of the .dynamic section. */
5644
5645 /* If there are initialization and/or finalization functions to
5646 call then add the corresponding DT_INIT/DT_FINI entries. */
5647 h = (info->init_function
5648 ? elf_link_hash_lookup (elf_hash_table (info),
5649 info->init_function, FALSE,
5650 FALSE, FALSE)
5651 : NULL);
5652 if (h != NULL
f5385ebf
AM
5653 && (h->ref_regular
5654 || h->def_regular))
5a580b3a
AM
5655 {
5656 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5657 return FALSE;
5658 }
5659 h = (info->fini_function
5660 ? elf_link_hash_lookup (elf_hash_table (info),
5661 info->fini_function, FALSE,
5662 FALSE, FALSE)
5663 : NULL);
5664 if (h != NULL
f5385ebf
AM
5665 && (h->ref_regular
5666 || h->def_regular))
5a580b3a
AM
5667 {
5668 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5669 return FALSE;
5670 }
5671
046183de
AM
5672 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5673 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5674 {
5675 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5676 if (! info->executable)
5677 {
5678 bfd *sub;
5679 asection *o;
5680
5681 for (sub = info->input_bfds; sub != NULL;
5682 sub = sub->link_next)
3fcd97f1
JJ
5683 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5684 for (o = sub->sections; o != NULL; o = o->next)
5685 if (elf_section_data (o)->this_hdr.sh_type
5686 == SHT_PREINIT_ARRAY)
5687 {
5688 (*_bfd_error_handler)
5689 (_("%B: .preinit_array section is not allowed in DSO"),
5690 sub);
5691 break;
5692 }
5a580b3a
AM
5693
5694 bfd_set_error (bfd_error_nonrepresentable_section);
5695 return FALSE;
5696 }
5697
5698 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5699 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5700 return FALSE;
5701 }
046183de
AM
5702 s = bfd_get_section_by_name (output_bfd, ".init_array");
5703 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5704 {
5705 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5706 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5707 return FALSE;
5708 }
046183de
AM
5709 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5710 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5711 {
5712 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5713 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5714 return FALSE;
5715 }
5716
5717 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5718 /* If .dynstr is excluded from the link, we don't want any of
5719 these tags. Strictly, we should be checking each section
5720 individually; This quick check covers for the case where
5721 someone does a /DISCARD/ : { *(*) }. */
5722 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5723 {
5724 bfd_size_type strsize;
5725
5726 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5727 if ((info->emit_hash
5728 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5729 || (info->emit_gnu_hash
5730 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5731 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5732 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5733 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5734 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5735 bed->s->sizeof_sym))
5736 return FALSE;
5737 }
5738 }
5739
5740 /* The backend must work out the sizes of all the other dynamic
5741 sections. */
5742 if (bed->elf_backend_size_dynamic_sections
5743 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5744 return FALSE;
5745
5746 if (elf_hash_table (info)->dynamic_sections_created)
5747 {
554220db 5748 unsigned long section_sym_count;
5a580b3a 5749 asection *s;
5a580b3a
AM
5750
5751 /* Set up the version definition section. */
5752 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5753 BFD_ASSERT (s != NULL);
5754
5755 /* We may have created additional version definitions if we are
5756 just linking a regular application. */
5757 verdefs = asvinfo.verdefs;
5758
5759 /* Skip anonymous version tag. */
5760 if (verdefs != NULL && verdefs->vernum == 0)
5761 verdefs = verdefs->next;
5762
3e3b46e5 5763 if (verdefs == NULL && !info->create_default_symver)
8423293d 5764 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5765 else
5766 {
5767 unsigned int cdefs;
5768 bfd_size_type size;
5769 struct bfd_elf_version_tree *t;
5770 bfd_byte *p;
5771 Elf_Internal_Verdef def;
5772 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5773 struct bfd_link_hash_entry *bh;
5774 struct elf_link_hash_entry *h;
5775 const char *name;
5a580b3a
AM
5776
5777 cdefs = 0;
5778 size = 0;
5779
5780 /* Make space for the base version. */
5781 size += sizeof (Elf_External_Verdef);
5782 size += sizeof (Elf_External_Verdaux);
5783 ++cdefs;
5784
3e3b46e5
PB
5785 /* Make space for the default version. */
5786 if (info->create_default_symver)
5787 {
5788 size += sizeof (Elf_External_Verdef);
5789 ++cdefs;
5790 }
5791
5a580b3a
AM
5792 for (t = verdefs; t != NULL; t = t->next)
5793 {
5794 struct bfd_elf_version_deps *n;
5795
5796 size += sizeof (Elf_External_Verdef);
5797 size += sizeof (Elf_External_Verdaux);
5798 ++cdefs;
5799
5800 for (n = t->deps; n != NULL; n = n->next)
5801 size += sizeof (Elf_External_Verdaux);
5802 }
5803
eea6121a
AM
5804 s->size = size;
5805 s->contents = bfd_alloc (output_bfd, s->size);
5806 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5807 return FALSE;
5808
5809 /* Fill in the version definition section. */
5810
5811 p = s->contents;
5812
5813 def.vd_version = VER_DEF_CURRENT;
5814 def.vd_flags = VER_FLG_BASE;
5815 def.vd_ndx = 1;
5816 def.vd_cnt = 1;
3e3b46e5
PB
5817 if (info->create_default_symver)
5818 {
5819 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5820 def.vd_next = sizeof (Elf_External_Verdef);
5821 }
5822 else
5823 {
5824 def.vd_aux = sizeof (Elf_External_Verdef);
5825 def.vd_next = (sizeof (Elf_External_Verdef)
5826 + sizeof (Elf_External_Verdaux));
5827 }
5a580b3a
AM
5828
5829 if (soname_indx != (bfd_size_type) -1)
5830 {
5831 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5832 soname_indx);
5833 def.vd_hash = bfd_elf_hash (soname);
5834 defaux.vda_name = soname_indx;
3e3b46e5 5835 name = soname;
5a580b3a
AM
5836 }
5837 else
5838 {
5a580b3a
AM
5839 bfd_size_type indx;
5840
06084812 5841 name = lbasename (output_bfd->filename);
5a580b3a
AM
5842 def.vd_hash = bfd_elf_hash (name);
5843 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5844 name, FALSE);
5845 if (indx == (bfd_size_type) -1)
5846 return FALSE;
5847 defaux.vda_name = indx;
5848 }
5849 defaux.vda_next = 0;
5850
5851 _bfd_elf_swap_verdef_out (output_bfd, &def,
5852 (Elf_External_Verdef *) p);
5853 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
5854 if (info->create_default_symver)
5855 {
5856 /* Add a symbol representing this version. */
5857 bh = NULL;
5858 if (! (_bfd_generic_link_add_one_symbol
5859 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5860 0, NULL, FALSE,
5861 get_elf_backend_data (dynobj)->collect, &bh)))
5862 return FALSE;
5863 h = (struct elf_link_hash_entry *) bh;
5864 h->non_elf = 0;
5865 h->def_regular = 1;
5866 h->type = STT_OBJECT;
5867 h->verinfo.vertree = NULL;
5868
5869 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5870 return FALSE;
5871
5872 /* Create a duplicate of the base version with the same
5873 aux block, but different flags. */
5874 def.vd_flags = 0;
5875 def.vd_ndx = 2;
5876 def.vd_aux = sizeof (Elf_External_Verdef);
5877 if (verdefs)
5878 def.vd_next = (sizeof (Elf_External_Verdef)
5879 + sizeof (Elf_External_Verdaux));
5880 else
5881 def.vd_next = 0;
5882 _bfd_elf_swap_verdef_out (output_bfd, &def,
5883 (Elf_External_Verdef *) p);
5884 p += sizeof (Elf_External_Verdef);
5885 }
5a580b3a
AM
5886 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5887 (Elf_External_Verdaux *) p);
5888 p += sizeof (Elf_External_Verdaux);
5889
5890 for (t = verdefs; t != NULL; t = t->next)
5891 {
5892 unsigned int cdeps;
5893 struct bfd_elf_version_deps *n;
5a580b3a
AM
5894
5895 cdeps = 0;
5896 for (n = t->deps; n != NULL; n = n->next)
5897 ++cdeps;
5898
5899 /* Add a symbol representing this version. */
5900 bh = NULL;
5901 if (! (_bfd_generic_link_add_one_symbol
5902 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5903 0, NULL, FALSE,
5904 get_elf_backend_data (dynobj)->collect, &bh)))
5905 return FALSE;
5906 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5907 h->non_elf = 0;
5908 h->def_regular = 1;
5a580b3a
AM
5909 h->type = STT_OBJECT;
5910 h->verinfo.vertree = t;
5911
c152c796 5912 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
5913 return FALSE;
5914
5915 def.vd_version = VER_DEF_CURRENT;
5916 def.vd_flags = 0;
5917 if (t->globals.list == NULL
5918 && t->locals.list == NULL
5919 && ! t->used)
5920 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 5921 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
5922 def.vd_cnt = cdeps + 1;
5923 def.vd_hash = bfd_elf_hash (t->name);
5924 def.vd_aux = sizeof (Elf_External_Verdef);
5925 def.vd_next = 0;
5926 if (t->next != NULL)
5927 def.vd_next = (sizeof (Elf_External_Verdef)
5928 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5929
5930 _bfd_elf_swap_verdef_out (output_bfd, &def,
5931 (Elf_External_Verdef *) p);
5932 p += sizeof (Elf_External_Verdef);
5933
5934 defaux.vda_name = h->dynstr_index;
5935 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5936 h->dynstr_index);
5937 defaux.vda_next = 0;
5938 if (t->deps != NULL)
5939 defaux.vda_next = sizeof (Elf_External_Verdaux);
5940 t->name_indx = defaux.vda_name;
5941
5942 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5943 (Elf_External_Verdaux *) p);
5944 p += sizeof (Elf_External_Verdaux);
5945
5946 for (n = t->deps; n != NULL; n = n->next)
5947 {
5948 if (n->version_needed == NULL)
5949 {
5950 /* This can happen if there was an error in the
5951 version script. */
5952 defaux.vda_name = 0;
5953 }
5954 else
5955 {
5956 defaux.vda_name = n->version_needed->name_indx;
5957 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5958 defaux.vda_name);
5959 }
5960 if (n->next == NULL)
5961 defaux.vda_next = 0;
5962 else
5963 defaux.vda_next = sizeof (Elf_External_Verdaux);
5964
5965 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5966 (Elf_External_Verdaux *) p);
5967 p += sizeof (Elf_External_Verdaux);
5968 }
5969 }
5970
5971 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5972 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5973 return FALSE;
5974
5975 elf_tdata (output_bfd)->cverdefs = cdefs;
5976 }
5977
5978 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5979 {
5980 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5981 return FALSE;
5982 }
5983 else if (info->flags & DF_BIND_NOW)
5984 {
5985 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5986 return FALSE;
5987 }
5988
5989 if (info->flags_1)
5990 {
5991 if (info->executable)
5992 info->flags_1 &= ~ (DF_1_INITFIRST
5993 | DF_1_NODELETE
5994 | DF_1_NOOPEN);
5995 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5996 return FALSE;
5997 }
5998
5999 /* Work out the size of the version reference section. */
6000
6001 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6002 BFD_ASSERT (s != NULL);
6003 {
6004 struct elf_find_verdep_info sinfo;
6005
6006 sinfo.output_bfd = output_bfd;
6007 sinfo.info = info;
6008 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6009 if (sinfo.vers == 0)
6010 sinfo.vers = 1;
6011 sinfo.failed = FALSE;
6012
6013 elf_link_hash_traverse (elf_hash_table (info),
6014 _bfd_elf_link_find_version_dependencies,
6015 &sinfo);
14b1c01e
AM
6016 if (sinfo.failed)
6017 return FALSE;
5a580b3a
AM
6018
6019 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6020 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6021 else
6022 {
6023 Elf_Internal_Verneed *t;
6024 unsigned int size;
6025 unsigned int crefs;
6026 bfd_byte *p;
6027
6028 /* Build the version definition section. */
6029 size = 0;
6030 crefs = 0;
6031 for (t = elf_tdata (output_bfd)->verref;
6032 t != NULL;
6033 t = t->vn_nextref)
6034 {
6035 Elf_Internal_Vernaux *a;
6036
6037 size += sizeof (Elf_External_Verneed);
6038 ++crefs;
6039 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6040 size += sizeof (Elf_External_Vernaux);
6041 }
6042
eea6121a
AM
6043 s->size = size;
6044 s->contents = bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6045 if (s->contents == NULL)
6046 return FALSE;
6047
6048 p = s->contents;
6049 for (t = elf_tdata (output_bfd)->verref;
6050 t != NULL;
6051 t = t->vn_nextref)
6052 {
6053 unsigned int caux;
6054 Elf_Internal_Vernaux *a;
6055 bfd_size_type indx;
6056
6057 caux = 0;
6058 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6059 ++caux;
6060
6061 t->vn_version = VER_NEED_CURRENT;
6062 t->vn_cnt = caux;
6063 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6064 elf_dt_name (t->vn_bfd) != NULL
6065 ? elf_dt_name (t->vn_bfd)
06084812 6066 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6067 FALSE);
6068 if (indx == (bfd_size_type) -1)
6069 return FALSE;
6070 t->vn_file = indx;
6071 t->vn_aux = sizeof (Elf_External_Verneed);
6072 if (t->vn_nextref == NULL)
6073 t->vn_next = 0;
6074 else
6075 t->vn_next = (sizeof (Elf_External_Verneed)
6076 + caux * sizeof (Elf_External_Vernaux));
6077
6078 _bfd_elf_swap_verneed_out (output_bfd, t,
6079 (Elf_External_Verneed *) p);
6080 p += sizeof (Elf_External_Verneed);
6081
6082 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6083 {
6084 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6085 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6086 a->vna_nodename, FALSE);
6087 if (indx == (bfd_size_type) -1)
6088 return FALSE;
6089 a->vna_name = indx;
6090 if (a->vna_nextptr == NULL)
6091 a->vna_next = 0;
6092 else
6093 a->vna_next = sizeof (Elf_External_Vernaux);
6094
6095 _bfd_elf_swap_vernaux_out (output_bfd, a,
6096 (Elf_External_Vernaux *) p);
6097 p += sizeof (Elf_External_Vernaux);
6098 }
6099 }
6100
6101 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6102 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6103 return FALSE;
6104
6105 elf_tdata (output_bfd)->cverrefs = crefs;
6106 }
6107 }
6108
8423293d
AM
6109 if ((elf_tdata (output_bfd)->cverrefs == 0
6110 && elf_tdata (output_bfd)->cverdefs == 0)
6111 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6112 &section_sym_count) == 0)
6113 {
6114 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6115 s->flags |= SEC_EXCLUDE;
6116 }
6117 }
6118 return TRUE;
6119}
6120
74541ad4
AM
6121/* Find the first non-excluded output section. We'll use its
6122 section symbol for some emitted relocs. */
6123void
6124_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6125{
6126 asection *s;
6127
6128 for (s = output_bfd->sections; s != NULL; s = s->next)
6129 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6130 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6131 {
6132 elf_hash_table (info)->text_index_section = s;
6133 break;
6134 }
6135}
6136
6137/* Find two non-excluded output sections, one for code, one for data.
6138 We'll use their section symbols for some emitted relocs. */
6139void
6140_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6141{
6142 asection *s;
6143
266b05cf
DJ
6144 /* Data first, since setting text_index_section changes
6145 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6146 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6147 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6148 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6149 {
266b05cf 6150 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6151 break;
6152 }
6153
6154 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6155 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6156 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6157 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6158 {
266b05cf 6159 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6160 break;
6161 }
6162
6163 if (elf_hash_table (info)->text_index_section == NULL)
6164 elf_hash_table (info)->text_index_section
6165 = elf_hash_table (info)->data_index_section;
6166}
6167
8423293d
AM
6168bfd_boolean
6169bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6170{
74541ad4
AM
6171 const struct elf_backend_data *bed;
6172
8423293d
AM
6173 if (!is_elf_hash_table (info->hash))
6174 return TRUE;
6175
74541ad4
AM
6176 bed = get_elf_backend_data (output_bfd);
6177 (*bed->elf_backend_init_index_section) (output_bfd, info);
6178
8423293d
AM
6179 if (elf_hash_table (info)->dynamic_sections_created)
6180 {
6181 bfd *dynobj;
8423293d
AM
6182 asection *s;
6183 bfd_size_type dynsymcount;
6184 unsigned long section_sym_count;
8423293d
AM
6185 unsigned int dtagcount;
6186
6187 dynobj = elf_hash_table (info)->dynobj;
6188
5a580b3a
AM
6189 /* Assign dynsym indicies. In a shared library we generate a
6190 section symbol for each output section, which come first.
6191 Next come all of the back-end allocated local dynamic syms,
6192 followed by the rest of the global symbols. */
6193
554220db
AM
6194 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6195 &section_sym_count);
5a580b3a
AM
6196
6197 /* Work out the size of the symbol version section. */
6198 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6199 BFD_ASSERT (s != NULL);
8423293d
AM
6200 if (dynsymcount != 0
6201 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6202 {
eea6121a
AM
6203 s->size = dynsymcount * sizeof (Elf_External_Versym);
6204 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6205 if (s->contents == NULL)
6206 return FALSE;
6207
6208 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6209 return FALSE;
6210 }
6211
6212 /* Set the size of the .dynsym and .hash sections. We counted
6213 the number of dynamic symbols in elf_link_add_object_symbols.
6214 We will build the contents of .dynsym and .hash when we build
6215 the final symbol table, because until then we do not know the
6216 correct value to give the symbols. We built the .dynstr
6217 section as we went along in elf_link_add_object_symbols. */
6218 s = bfd_get_section_by_name (dynobj, ".dynsym");
6219 BFD_ASSERT (s != NULL);
eea6121a 6220 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6221
6222 if (dynsymcount != 0)
6223 {
554220db
AM
6224 s->contents = bfd_alloc (output_bfd, s->size);
6225 if (s->contents == NULL)
6226 return FALSE;
5a580b3a 6227
554220db
AM
6228 /* The first entry in .dynsym is a dummy symbol.
6229 Clear all the section syms, in case we don't output them all. */
6230 ++section_sym_count;
6231 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6232 }
6233
fdc90cb4
JJ
6234 elf_hash_table (info)->bucketcount = 0;
6235
5a580b3a
AM
6236 /* Compute the size of the hashing table. As a side effect this
6237 computes the hash values for all the names we export. */
fdc90cb4
JJ
6238 if (info->emit_hash)
6239 {
6240 unsigned long int *hashcodes;
14b1c01e 6241 struct hash_codes_info hashinf;
fdc90cb4
JJ
6242 bfd_size_type amt;
6243 unsigned long int nsyms;
6244 size_t bucketcount;
6245 size_t hash_entry_size;
6246
6247 /* Compute the hash values for all exported symbols. At the same
6248 time store the values in an array so that we could use them for
6249 optimizations. */
6250 amt = dynsymcount * sizeof (unsigned long int);
6251 hashcodes = bfd_malloc (amt);
6252 if (hashcodes == NULL)
6253 return FALSE;
14b1c01e
AM
6254 hashinf.hashcodes = hashcodes;
6255 hashinf.error = FALSE;
5a580b3a 6256
fdc90cb4
JJ
6257 /* Put all hash values in HASHCODES. */
6258 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6259 elf_collect_hash_codes, &hashinf);
6260 if (hashinf.error)
4dd07732
AM
6261 {
6262 free (hashcodes);
6263 return FALSE;
6264 }
5a580b3a 6265
14b1c01e 6266 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6267 bucketcount
6268 = compute_bucket_count (info, hashcodes, nsyms, 0);
6269 free (hashcodes);
6270
6271 if (bucketcount == 0)
6272 return FALSE;
5a580b3a 6273
fdc90cb4
JJ
6274 elf_hash_table (info)->bucketcount = bucketcount;
6275
6276 s = bfd_get_section_by_name (dynobj, ".hash");
6277 BFD_ASSERT (s != NULL);
6278 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6279 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6280 s->contents = bfd_zalloc (output_bfd, s->size);
6281 if (s->contents == NULL)
6282 return FALSE;
6283
6284 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6285 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6286 s->contents + hash_entry_size);
6287 }
6288
6289 if (info->emit_gnu_hash)
6290 {
6291 size_t i, cnt;
6292 unsigned char *contents;
6293 struct collect_gnu_hash_codes cinfo;
6294 bfd_size_type amt;
6295 size_t bucketcount;
6296
6297 memset (&cinfo, 0, sizeof (cinfo));
6298
6299 /* Compute the hash values for all exported symbols. At the same
6300 time store the values in an array so that we could use them for
6301 optimizations. */
6302 amt = dynsymcount * 2 * sizeof (unsigned long int);
6303 cinfo.hashcodes = bfd_malloc (amt);
6304 if (cinfo.hashcodes == NULL)
6305 return FALSE;
6306
6307 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6308 cinfo.min_dynindx = -1;
6309 cinfo.output_bfd = output_bfd;
6310 cinfo.bed = bed;
6311
6312 /* Put all hash values in HASHCODES. */
6313 elf_link_hash_traverse (elf_hash_table (info),
6314 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6315 if (cinfo.error)
4dd07732
AM
6316 {
6317 free (cinfo.hashcodes);
6318 return FALSE;
6319 }
fdc90cb4
JJ
6320
6321 bucketcount
6322 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6323
6324 if (bucketcount == 0)
6325 {
6326 free (cinfo.hashcodes);
6327 return FALSE;
6328 }
6329
6330 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6331 BFD_ASSERT (s != NULL);
6332
6333 if (cinfo.nsyms == 0)
6334 {
6335 /* Empty .gnu.hash section is special. */
6336 BFD_ASSERT (cinfo.min_dynindx == -1);
6337 free (cinfo.hashcodes);
6338 s->size = 5 * 4 + bed->s->arch_size / 8;
6339 contents = bfd_zalloc (output_bfd, s->size);
6340 if (contents == NULL)
6341 return FALSE;
6342 s->contents = contents;
6343 /* 1 empty bucket. */
6344 bfd_put_32 (output_bfd, 1, contents);
6345 /* SYMIDX above the special symbol 0. */
6346 bfd_put_32 (output_bfd, 1, contents + 4);
6347 /* Just one word for bitmask. */
6348 bfd_put_32 (output_bfd, 1, contents + 8);
6349 /* Only hash fn bloom filter. */
6350 bfd_put_32 (output_bfd, 0, contents + 12);
6351 /* No hashes are valid - empty bitmask. */
6352 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6353 /* No hashes in the only bucket. */
6354 bfd_put_32 (output_bfd, 0,
6355 contents + 16 + bed->s->arch_size / 8);
6356 }
6357 else
6358 {
fdc90cb4 6359 unsigned long int maskwords, maskbitslog2;
0b33793d 6360 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4
JJ
6361
6362 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6363 if (maskbitslog2 < 3)
6364 maskbitslog2 = 5;
6365 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6366 maskbitslog2 = maskbitslog2 + 3;
6367 else
6368 maskbitslog2 = maskbitslog2 + 2;
6369 if (bed->s->arch_size == 64)
6370 {
6371 if (maskbitslog2 == 5)
6372 maskbitslog2 = 6;
6373 cinfo.shift1 = 6;
6374 }
6375 else
6376 cinfo.shift1 = 5;
6377 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6378 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6379 cinfo.maskbits = 1 << maskbitslog2;
6380 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6381 amt = bucketcount * sizeof (unsigned long int) * 2;
6382 amt += maskwords * sizeof (bfd_vma);
6383 cinfo.bitmask = bfd_malloc (amt);
6384 if (cinfo.bitmask == NULL)
6385 {
6386 free (cinfo.hashcodes);
6387 return FALSE;
6388 }
6389
6390 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6391 cinfo.indx = cinfo.counts + bucketcount;
6392 cinfo.symindx = dynsymcount - cinfo.nsyms;
6393 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6394
6395 /* Determine how often each hash bucket is used. */
6396 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6397 for (i = 0; i < cinfo.nsyms; ++i)
6398 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6399
6400 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6401 if (cinfo.counts[i] != 0)
6402 {
6403 cinfo.indx[i] = cnt;
6404 cnt += cinfo.counts[i];
6405 }
6406 BFD_ASSERT (cnt == dynsymcount);
6407 cinfo.bucketcount = bucketcount;
6408 cinfo.local_indx = cinfo.min_dynindx;
6409
6410 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6411 s->size += cinfo.maskbits / 8;
6412 contents = bfd_zalloc (output_bfd, s->size);
6413 if (contents == NULL)
6414 {
6415 free (cinfo.bitmask);
6416 free (cinfo.hashcodes);
6417 return FALSE;
6418 }
6419
6420 s->contents = contents;
6421 bfd_put_32 (output_bfd, bucketcount, contents);
6422 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6423 bfd_put_32 (output_bfd, maskwords, contents + 8);
6424 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6425 contents += 16 + cinfo.maskbits / 8;
6426
6427 for (i = 0; i < bucketcount; ++i)
6428 {
6429 if (cinfo.counts[i] == 0)
6430 bfd_put_32 (output_bfd, 0, contents);
6431 else
6432 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6433 contents += 4;
6434 }
6435
6436 cinfo.contents = contents;
6437
6438 /* Renumber dynamic symbols, populate .gnu.hash section. */
6439 elf_link_hash_traverse (elf_hash_table (info),
6440 elf_renumber_gnu_hash_syms, &cinfo);
6441
6442 contents = s->contents + 16;
6443 for (i = 0; i < maskwords; ++i)
6444 {
6445 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6446 contents);
6447 contents += bed->s->arch_size / 8;
6448 }
6449
6450 free (cinfo.bitmask);
6451 free (cinfo.hashcodes);
6452 }
6453 }
5a580b3a
AM
6454
6455 s = bfd_get_section_by_name (dynobj, ".dynstr");
6456 BFD_ASSERT (s != NULL);
6457
4ad4eba5 6458 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6459
eea6121a 6460 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6461
6462 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6463 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6464 return FALSE;
6465 }
6466
6467 return TRUE;
6468}
4d269e42
AM
6469\f
6470/* Indicate that we are only retrieving symbol values from this
6471 section. */
6472
6473void
6474_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6475{
6476 if (is_elf_hash_table (info->hash))
6477 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6478 _bfd_generic_link_just_syms (sec, info);
6479}
6480
6481/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6482
6483static void
6484merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6485 asection *sec)
6486{
6487 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6488 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6489}
6490
6491/* Finish SHF_MERGE section merging. */
6492
6493bfd_boolean
6494_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6495{
6496 bfd *ibfd;
6497 asection *sec;
6498
6499 if (!is_elf_hash_table (info->hash))
6500 return FALSE;
6501
6502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6503 if ((ibfd->flags & DYNAMIC) == 0)
6504 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6505 if ((sec->flags & SEC_MERGE) != 0
6506 && !bfd_is_abs_section (sec->output_section))
6507 {
6508 struct bfd_elf_section_data *secdata;
6509
6510 secdata = elf_section_data (sec);
6511 if (! _bfd_add_merge_section (abfd,
6512 &elf_hash_table (info)->merge_info,
6513 sec, &secdata->sec_info))
6514 return FALSE;
6515 else if (secdata->sec_info)
6516 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6517 }
6518
6519 if (elf_hash_table (info)->merge_info != NULL)
6520 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6521 merge_sections_remove_hook);
6522 return TRUE;
6523}
6524
6525/* Create an entry in an ELF linker hash table. */
6526
6527struct bfd_hash_entry *
6528_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6529 struct bfd_hash_table *table,
6530 const char *string)
6531{
6532 /* Allocate the structure if it has not already been allocated by a
6533 subclass. */
6534 if (entry == NULL)
6535 {
6536 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6537 if (entry == NULL)
6538 return entry;
6539 }
6540
6541 /* Call the allocation method of the superclass. */
6542 entry = _bfd_link_hash_newfunc (entry, table, string);
6543 if (entry != NULL)
6544 {
6545 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6546 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6547
6548 /* Set local fields. */
6549 ret->indx = -1;
6550 ret->dynindx = -1;
6551 ret->got = htab->init_got_refcount;
6552 ret->plt = htab->init_plt_refcount;
6553 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6554 - offsetof (struct elf_link_hash_entry, size)));
6555 /* Assume that we have been called by a non-ELF symbol reader.
6556 This flag is then reset by the code which reads an ELF input
6557 file. This ensures that a symbol created by a non-ELF symbol
6558 reader will have the flag set correctly. */
6559 ret->non_elf = 1;
6560 }
6561
6562 return entry;
6563}
6564
6565/* Copy data from an indirect symbol to its direct symbol, hiding the
6566 old indirect symbol. Also used for copying flags to a weakdef. */
6567
6568void
6569_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6570 struct elf_link_hash_entry *dir,
6571 struct elf_link_hash_entry *ind)
6572{
6573 struct elf_link_hash_table *htab;
6574
6575 /* Copy down any references that we may have already seen to the
6576 symbol which just became indirect. */
6577
6578 dir->ref_dynamic |= ind->ref_dynamic;
6579 dir->ref_regular |= ind->ref_regular;
6580 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6581 dir->non_got_ref |= ind->non_got_ref;
6582 dir->needs_plt |= ind->needs_plt;
6583 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6584
6585 if (ind->root.type != bfd_link_hash_indirect)
6586 return;
6587
6588 /* Copy over the global and procedure linkage table refcount entries.
6589 These may have been already set up by a check_relocs routine. */
6590 htab = elf_hash_table (info);
6591 if (ind->got.refcount > htab->init_got_refcount.refcount)
6592 {
6593 if (dir->got.refcount < 0)
6594 dir->got.refcount = 0;
6595 dir->got.refcount += ind->got.refcount;
6596 ind->got.refcount = htab->init_got_refcount.refcount;
6597 }
6598
6599 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6600 {
6601 if (dir->plt.refcount < 0)
6602 dir->plt.refcount = 0;
6603 dir->plt.refcount += ind->plt.refcount;
6604 ind->plt.refcount = htab->init_plt_refcount.refcount;
6605 }
6606
6607 if (ind->dynindx != -1)
6608 {
6609 if (dir->dynindx != -1)
6610 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6611 dir->dynindx = ind->dynindx;
6612 dir->dynstr_index = ind->dynstr_index;
6613 ind->dynindx = -1;
6614 ind->dynstr_index = 0;
6615 }
6616}
6617
6618void
6619_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6620 struct elf_link_hash_entry *h,
6621 bfd_boolean force_local)
6622{
6623 h->plt = elf_hash_table (info)->init_plt_offset;
6624 h->needs_plt = 0;
6625 if (force_local)
6626 {
6627 h->forced_local = 1;
6628 if (h->dynindx != -1)
6629 {
6630 h->dynindx = -1;
6631 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6632 h->dynstr_index);
6633 }
6634 }
6635}
6636
6637/* Initialize an ELF linker hash table. */
6638
6639bfd_boolean
6640_bfd_elf_link_hash_table_init
6641 (struct elf_link_hash_table *table,
6642 bfd *abfd,
6643 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6644 struct bfd_hash_table *,
6645 const char *),
6646 unsigned int entsize)
6647{
6648 bfd_boolean ret;
6649 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6650
6651 memset (table, 0, sizeof * table);
6652 table->init_got_refcount.refcount = can_refcount - 1;
6653 table->init_plt_refcount.refcount = can_refcount - 1;
6654 table->init_got_offset.offset = -(bfd_vma) 1;
6655 table->init_plt_offset.offset = -(bfd_vma) 1;
6656 /* The first dynamic symbol is a dummy. */
6657 table->dynsymcount = 1;
6658
6659 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6660 table->root.type = bfd_link_elf_hash_table;
6661
6662 return ret;
6663}
6664
6665/* Create an ELF linker hash table. */
6666
6667struct bfd_link_hash_table *
6668_bfd_elf_link_hash_table_create (bfd *abfd)
6669{
6670 struct elf_link_hash_table *ret;
6671 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6672
6673 ret = bfd_malloc (amt);
6674 if (ret == NULL)
6675 return NULL;
6676
6677 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6678 sizeof (struct elf_link_hash_entry)))
6679 {
6680 free (ret);
6681 return NULL;
6682 }
6683
6684 return &ret->root;
6685}
6686
6687/* This is a hook for the ELF emulation code in the generic linker to
6688 tell the backend linker what file name to use for the DT_NEEDED
6689 entry for a dynamic object. */
6690
6691void
6692bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6693{
6694 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6695 && bfd_get_format (abfd) == bfd_object)
6696 elf_dt_name (abfd) = name;
6697}
6698
6699int
6700bfd_elf_get_dyn_lib_class (bfd *abfd)
6701{
6702 int lib_class;
6703 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6704 && bfd_get_format (abfd) == bfd_object)
6705 lib_class = elf_dyn_lib_class (abfd);
6706 else
6707 lib_class = 0;
6708 return lib_class;
6709}
6710
6711void
6712bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6713{
6714 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6715 && bfd_get_format (abfd) == bfd_object)
6716 elf_dyn_lib_class (abfd) = lib_class;
6717}
6718
6719/* Get the list of DT_NEEDED entries for a link. This is a hook for
6720 the linker ELF emulation code. */
6721
6722struct bfd_link_needed_list *
6723bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6724 struct bfd_link_info *info)
6725{
6726 if (! is_elf_hash_table (info->hash))
6727 return NULL;
6728 return elf_hash_table (info)->needed;
6729}
6730
6731/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6732 hook for the linker ELF emulation code. */
6733
6734struct bfd_link_needed_list *
6735bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6736 struct bfd_link_info *info)
6737{
6738 if (! is_elf_hash_table (info->hash))
6739 return NULL;
6740 return elf_hash_table (info)->runpath;
6741}
6742
6743/* Get the name actually used for a dynamic object for a link. This
6744 is the SONAME entry if there is one. Otherwise, it is the string
6745 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6746
6747const char *
6748bfd_elf_get_dt_soname (bfd *abfd)
6749{
6750 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6751 && bfd_get_format (abfd) == bfd_object)
6752 return elf_dt_name (abfd);
6753 return NULL;
6754}
6755
6756/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6757 the ELF linker emulation code. */
6758
6759bfd_boolean
6760bfd_elf_get_bfd_needed_list (bfd *abfd,
6761 struct bfd_link_needed_list **pneeded)
6762{
6763 asection *s;
6764 bfd_byte *dynbuf = NULL;
cb33740c 6765 unsigned int elfsec;
4d269e42
AM
6766 unsigned long shlink;
6767 bfd_byte *extdyn, *extdynend;
6768 size_t extdynsize;
6769 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6770
6771 *pneeded = NULL;
6772
6773 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6774 || bfd_get_format (abfd) != bfd_object)
6775 return TRUE;
6776
6777 s = bfd_get_section_by_name (abfd, ".dynamic");
6778 if (s == NULL || s->size == 0)
6779 return TRUE;
6780
6781 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6782 goto error_return;
6783
6784 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6785 if (elfsec == SHN_BAD)
4d269e42
AM
6786 goto error_return;
6787
6788 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6789
4d269e42
AM
6790 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6791 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6792
6793 extdyn = dynbuf;
6794 extdynend = extdyn + s->size;
6795 for (; extdyn < extdynend; extdyn += extdynsize)
6796 {
6797 Elf_Internal_Dyn dyn;
6798
6799 (*swap_dyn_in) (abfd, extdyn, &dyn);
6800
6801 if (dyn.d_tag == DT_NULL)
6802 break;
6803
6804 if (dyn.d_tag == DT_NEEDED)
6805 {
6806 const char *string;
6807 struct bfd_link_needed_list *l;
6808 unsigned int tagv = dyn.d_un.d_val;
6809 bfd_size_type amt;
6810
6811 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6812 if (string == NULL)
6813 goto error_return;
6814
6815 amt = sizeof *l;
6816 l = bfd_alloc (abfd, amt);
6817 if (l == NULL)
6818 goto error_return;
6819
6820 l->by = abfd;
6821 l->name = string;
6822 l->next = *pneeded;
6823 *pneeded = l;
6824 }
6825 }
6826
6827 free (dynbuf);
6828
6829 return TRUE;
6830
6831 error_return:
6832 if (dynbuf != NULL)
6833 free (dynbuf);
6834 return FALSE;
6835}
6836
6837struct elf_symbuf_symbol
6838{
6839 unsigned long st_name; /* Symbol name, index in string tbl */
6840 unsigned char st_info; /* Type and binding attributes */
6841 unsigned char st_other; /* Visibilty, and target specific */
6842};
6843
6844struct elf_symbuf_head
6845{
6846 struct elf_symbuf_symbol *ssym;
6847 bfd_size_type count;
6848 unsigned int st_shndx;
6849};
6850
6851struct elf_symbol
6852{
6853 union
6854 {
6855 Elf_Internal_Sym *isym;
6856 struct elf_symbuf_symbol *ssym;
6857 } u;
6858 const char *name;
6859};
6860
6861/* Sort references to symbols by ascending section number. */
6862
6863static int
6864elf_sort_elf_symbol (const void *arg1, const void *arg2)
6865{
6866 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6867 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6868
6869 return s1->st_shndx - s2->st_shndx;
6870}
6871
6872static int
6873elf_sym_name_compare (const void *arg1, const void *arg2)
6874{
6875 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6876 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6877 return strcmp (s1->name, s2->name);
6878}
6879
6880static struct elf_symbuf_head *
6881elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6882{
14b1c01e 6883 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
6884 struct elf_symbuf_symbol *ssym;
6885 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 6886 bfd_size_type i, shndx_count, total_size;
4d269e42 6887
14b1c01e 6888 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
6889 if (indbuf == NULL)
6890 return NULL;
6891
6892 for (ind = indbuf, i = 0; i < symcount; i++)
6893 if (isymbuf[i].st_shndx != SHN_UNDEF)
6894 *ind++ = &isymbuf[i];
6895 indbufend = ind;
6896
6897 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6898 elf_sort_elf_symbol);
6899
6900 shndx_count = 0;
6901 if (indbufend > indbuf)
6902 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6903 if (ind[0]->st_shndx != ind[1]->st_shndx)
6904 shndx_count++;
6905
3ae181ee
L
6906 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6907 + (indbufend - indbuf) * sizeof (*ssym));
6908 ssymbuf = bfd_malloc (total_size);
4d269e42
AM
6909 if (ssymbuf == NULL)
6910 {
6911 free (indbuf);
6912 return NULL;
6913 }
6914
3ae181ee 6915 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
6916 ssymbuf->ssym = NULL;
6917 ssymbuf->count = shndx_count;
6918 ssymbuf->st_shndx = 0;
6919 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6920 {
6921 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6922 {
6923 ssymhead++;
6924 ssymhead->ssym = ssym;
6925 ssymhead->count = 0;
6926 ssymhead->st_shndx = (*ind)->st_shndx;
6927 }
6928 ssym->st_name = (*ind)->st_name;
6929 ssym->st_info = (*ind)->st_info;
6930 ssym->st_other = (*ind)->st_other;
6931 ssymhead->count++;
6932 }
3ae181ee
L
6933 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6934 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6935 == total_size));
4d269e42
AM
6936
6937 free (indbuf);
6938 return ssymbuf;
6939}
6940
6941/* Check if 2 sections define the same set of local and global
6942 symbols. */
6943
8f317e31 6944static bfd_boolean
4d269e42
AM
6945bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6946 struct bfd_link_info *info)
6947{
6948 bfd *bfd1, *bfd2;
6949 const struct elf_backend_data *bed1, *bed2;
6950 Elf_Internal_Shdr *hdr1, *hdr2;
6951 bfd_size_type symcount1, symcount2;
6952 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6953 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6954 Elf_Internal_Sym *isym, *isymend;
6955 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6956 bfd_size_type count1, count2, i;
cb33740c 6957 unsigned int shndx1, shndx2;
4d269e42
AM
6958 bfd_boolean result;
6959
6960 bfd1 = sec1->owner;
6961 bfd2 = sec2->owner;
6962
4d269e42
AM
6963 /* Both sections have to be in ELF. */
6964 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6965 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6966 return FALSE;
6967
6968 if (elf_section_type (sec1) != elf_section_type (sec2))
6969 return FALSE;
6970
4d269e42
AM
6971 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6972 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 6973 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
6974 return FALSE;
6975
6976 bed1 = get_elf_backend_data (bfd1);
6977 bed2 = get_elf_backend_data (bfd2);
6978 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
6979 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
6980 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
6981 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
6982
6983 if (symcount1 == 0 || symcount2 == 0)
6984 return FALSE;
6985
6986 result = FALSE;
6987 isymbuf1 = NULL;
6988 isymbuf2 = NULL;
6989 ssymbuf1 = elf_tdata (bfd1)->symbuf;
6990 ssymbuf2 = elf_tdata (bfd2)->symbuf;
6991
6992 if (ssymbuf1 == NULL)
6993 {
6994 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
6995 NULL, NULL, NULL);
6996 if (isymbuf1 == NULL)
6997 goto done;
6998
6999 if (!info->reduce_memory_overheads)
7000 elf_tdata (bfd1)->symbuf = ssymbuf1
7001 = elf_create_symbuf (symcount1, isymbuf1);
7002 }
7003
7004 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7005 {
7006 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7007 NULL, NULL, NULL);
7008 if (isymbuf2 == NULL)
7009 goto done;
7010
7011 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7012 elf_tdata (bfd2)->symbuf = ssymbuf2
7013 = elf_create_symbuf (symcount2, isymbuf2);
7014 }
7015
7016 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7017 {
7018 /* Optimized faster version. */
7019 bfd_size_type lo, hi, mid;
7020 struct elf_symbol *symp;
7021 struct elf_symbuf_symbol *ssym, *ssymend;
7022
7023 lo = 0;
7024 hi = ssymbuf1->count;
7025 ssymbuf1++;
7026 count1 = 0;
7027 while (lo < hi)
7028 {
7029 mid = (lo + hi) / 2;
cb33740c 7030 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7031 hi = mid;
cb33740c 7032 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7033 lo = mid + 1;
7034 else
7035 {
7036 count1 = ssymbuf1[mid].count;
7037 ssymbuf1 += mid;
7038 break;
7039 }
7040 }
7041
7042 lo = 0;
7043 hi = ssymbuf2->count;
7044 ssymbuf2++;
7045 count2 = 0;
7046 while (lo < hi)
7047 {
7048 mid = (lo + hi) / 2;
cb33740c 7049 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7050 hi = mid;
cb33740c 7051 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7052 lo = mid + 1;
7053 else
7054 {
7055 count2 = ssymbuf2[mid].count;
7056 ssymbuf2 += mid;
7057 break;
7058 }
7059 }
7060
7061 if (count1 == 0 || count2 == 0 || count1 != count2)
7062 goto done;
7063
7064 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7065 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7066 if (symtable1 == NULL || symtable2 == NULL)
7067 goto done;
7068
7069 symp = symtable1;
7070 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7071 ssym < ssymend; ssym++, symp++)
7072 {
7073 symp->u.ssym = ssym;
7074 symp->name = bfd_elf_string_from_elf_section (bfd1,
7075 hdr1->sh_link,
7076 ssym->st_name);
7077 }
7078
7079 symp = symtable2;
7080 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7081 ssym < ssymend; ssym++, symp++)
7082 {
7083 symp->u.ssym = ssym;
7084 symp->name = bfd_elf_string_from_elf_section (bfd2,
7085 hdr2->sh_link,
7086 ssym->st_name);
7087 }
7088
7089 /* Sort symbol by name. */
7090 qsort (symtable1, count1, sizeof (struct elf_symbol),
7091 elf_sym_name_compare);
7092 qsort (symtable2, count1, sizeof (struct elf_symbol),
7093 elf_sym_name_compare);
7094
7095 for (i = 0; i < count1; i++)
7096 /* Two symbols must have the same binding, type and name. */
7097 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7098 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7099 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7100 goto done;
7101
7102 result = TRUE;
7103 goto done;
7104 }
7105
7106 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7107 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7108 if (symtable1 == NULL || symtable2 == NULL)
7109 goto done;
7110
7111 /* Count definitions in the section. */
7112 count1 = 0;
7113 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7114 if (isym->st_shndx == shndx1)
4d269e42
AM
7115 symtable1[count1++].u.isym = isym;
7116
7117 count2 = 0;
7118 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7119 if (isym->st_shndx == shndx2)
4d269e42
AM
7120 symtable2[count2++].u.isym = isym;
7121
7122 if (count1 == 0 || count2 == 0 || count1 != count2)
7123 goto done;
7124
7125 for (i = 0; i < count1; i++)
7126 symtable1[i].name
7127 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7128 symtable1[i].u.isym->st_name);
7129
7130 for (i = 0; i < count2; i++)
7131 symtable2[i].name
7132 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7133 symtable2[i].u.isym->st_name);
7134
7135 /* Sort symbol by name. */
7136 qsort (symtable1, count1, sizeof (struct elf_symbol),
7137 elf_sym_name_compare);
7138 qsort (symtable2, count1, sizeof (struct elf_symbol),
7139 elf_sym_name_compare);
7140
7141 for (i = 0; i < count1; i++)
7142 /* Two symbols must have the same binding, type and name. */
7143 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7144 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7145 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7146 goto done;
7147
7148 result = TRUE;
7149
7150done:
7151 if (symtable1)
7152 free (symtable1);
7153 if (symtable2)
7154 free (symtable2);
7155 if (isymbuf1)
7156 free (isymbuf1);
7157 if (isymbuf2)
7158 free (isymbuf2);
7159
7160 return result;
7161}
7162
7163/* Return TRUE if 2 section types are compatible. */
7164
7165bfd_boolean
7166_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7167 bfd *bbfd, const asection *bsec)
7168{
7169 if (asec == NULL
7170 || bsec == NULL
7171 || abfd->xvec->flavour != bfd_target_elf_flavour
7172 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7173 return TRUE;
7174
7175 return elf_section_type (asec) == elf_section_type (bsec);
7176}
7177\f
c152c796
AM
7178/* Final phase of ELF linker. */
7179
7180/* A structure we use to avoid passing large numbers of arguments. */
7181
7182struct elf_final_link_info
7183{
7184 /* General link information. */
7185 struct bfd_link_info *info;
7186 /* Output BFD. */
7187 bfd *output_bfd;
7188 /* Symbol string table. */
7189 struct bfd_strtab_hash *symstrtab;
7190 /* .dynsym section. */
7191 asection *dynsym_sec;
7192 /* .hash section. */
7193 asection *hash_sec;
7194 /* symbol version section (.gnu.version). */
7195 asection *symver_sec;
7196 /* Buffer large enough to hold contents of any section. */
7197 bfd_byte *contents;
7198 /* Buffer large enough to hold external relocs of any section. */
7199 void *external_relocs;
7200 /* Buffer large enough to hold internal relocs of any section. */
7201 Elf_Internal_Rela *internal_relocs;
7202 /* Buffer large enough to hold external local symbols of any input
7203 BFD. */
7204 bfd_byte *external_syms;
7205 /* And a buffer for symbol section indices. */
7206 Elf_External_Sym_Shndx *locsym_shndx;
7207 /* Buffer large enough to hold internal local symbols of any input
7208 BFD. */
7209 Elf_Internal_Sym *internal_syms;
7210 /* Array large enough to hold a symbol index for each local symbol
7211 of any input BFD. */
7212 long *indices;
7213 /* Array large enough to hold a section pointer for each local
7214 symbol of any input BFD. */
7215 asection **sections;
7216 /* Buffer to hold swapped out symbols. */
7217 bfd_byte *symbuf;
7218 /* And one for symbol section indices. */
7219 Elf_External_Sym_Shndx *symshndxbuf;
7220 /* Number of swapped out symbols in buffer. */
7221 size_t symbuf_count;
7222 /* Number of symbols which fit in symbuf. */
7223 size_t symbuf_size;
7224 /* And same for symshndxbuf. */
7225 size_t shndxbuf_size;
7226};
7227
7228/* This struct is used to pass information to elf_link_output_extsym. */
7229
7230struct elf_outext_info
7231{
7232 bfd_boolean failed;
7233 bfd_boolean localsyms;
7234 struct elf_final_link_info *finfo;
7235};
7236
d9352518
DB
7237
7238/* Support for evaluating a complex relocation.
7239
7240 Complex relocations are generalized, self-describing relocations. The
7241 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7242 relocations themselves.
d9352518
DB
7243
7244 The relocations are use a reserved elf-wide relocation type code (R_RELC
7245 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7246 information (start bit, end bit, word width, etc) into the addend. This
7247 information is extracted from CGEN-generated operand tables within gas.
7248
7249 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7250 internal) representing prefix-notation expressions, including but not
7251 limited to those sorts of expressions normally encoded as addends in the
7252 addend field. The symbol mangling format is:
7253
7254 <node> := <literal>
7255 | <unary-operator> ':' <node>
7256 | <binary-operator> ':' <node> ':' <node>
7257 ;
7258
7259 <literal> := 's' <digits=N> ':' <N character symbol name>
7260 | 'S' <digits=N> ':' <N character section name>
7261 | '#' <hexdigits>
7262 ;
7263
7264 <binary-operator> := as in C
7265 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7266
7267static void
a0c8462f
AM
7268set_symbol_value (bfd *bfd_with_globals,
7269 Elf_Internal_Sym *isymbuf,
7270 size_t locsymcount,
7271 size_t symidx,
7272 bfd_vma val)
d9352518 7273{
8977835c
AM
7274 struct elf_link_hash_entry **sym_hashes;
7275 struct elf_link_hash_entry *h;
7276 size_t extsymoff = locsymcount;
d9352518 7277
8977835c 7278 if (symidx < locsymcount)
d9352518 7279 {
8977835c
AM
7280 Elf_Internal_Sym *sym;
7281
7282 sym = isymbuf + symidx;
7283 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7284 {
7285 /* It is a local symbol: move it to the
7286 "absolute" section and give it a value. */
7287 sym->st_shndx = SHN_ABS;
7288 sym->st_value = val;
7289 return;
7290 }
7291 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7292 extsymoff = 0;
d9352518 7293 }
8977835c
AM
7294
7295 /* It is a global symbol: set its link type
7296 to "defined" and give it a value. */
7297
7298 sym_hashes = elf_sym_hashes (bfd_with_globals);
7299 h = sym_hashes [symidx - extsymoff];
7300 while (h->root.type == bfd_link_hash_indirect
7301 || h->root.type == bfd_link_hash_warning)
7302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7303 h->root.type = bfd_link_hash_defined;
7304 h->root.u.def.value = val;
7305 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7306}
7307
a0c8462f
AM
7308static bfd_boolean
7309resolve_symbol (const char *name,
7310 bfd *input_bfd,
7311 struct elf_final_link_info *finfo,
7312 bfd_vma *result,
7313 Elf_Internal_Sym *isymbuf,
7314 size_t locsymcount)
d9352518 7315{
a0c8462f
AM
7316 Elf_Internal_Sym *sym;
7317 struct bfd_link_hash_entry *global_entry;
7318 const char *candidate = NULL;
7319 Elf_Internal_Shdr *symtab_hdr;
7320 size_t i;
7321
d9352518
DB
7322 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7323
7324 for (i = 0; i < locsymcount; ++ i)
7325 {
8977835c 7326 sym = isymbuf + i;
d9352518
DB
7327
7328 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7329 continue;
7330
7331 candidate = bfd_elf_string_from_elf_section (input_bfd,
7332 symtab_hdr->sh_link,
7333 sym->st_name);
7334#ifdef DEBUG
0f02bbd9
AM
7335 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7336 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7337#endif
7338 if (candidate && strcmp (candidate, name) == 0)
7339 {
0f02bbd9 7340 asection *sec = finfo->sections [i];
d9352518 7341
0f02bbd9
AM
7342 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7343 *result += sec->output_offset + sec->output_section->vma;
d9352518 7344#ifdef DEBUG
0f02bbd9
AM
7345 printf ("Found symbol with value %8.8lx\n",
7346 (unsigned long) *result);
d9352518
DB
7347#endif
7348 return TRUE;
7349 }
7350 }
7351
7352 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7353 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7354 FALSE, FALSE, TRUE);
d9352518
DB
7355 if (!global_entry)
7356 return FALSE;
a0c8462f 7357
d9352518
DB
7358 if (global_entry->type == bfd_link_hash_defined
7359 || global_entry->type == bfd_link_hash_defweak)
7360 {
a0c8462f
AM
7361 *result = (global_entry->u.def.value
7362 + global_entry->u.def.section->output_section->vma
7363 + global_entry->u.def.section->output_offset);
d9352518 7364#ifdef DEBUG
0f02bbd9
AM
7365 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7366 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7367#endif
7368 return TRUE;
a0c8462f 7369 }
d9352518 7370
d9352518
DB
7371 return FALSE;
7372}
7373
7374static bfd_boolean
a0c8462f
AM
7375resolve_section (const char *name,
7376 asection *sections,
7377 bfd_vma *result)
d9352518 7378{
a0c8462f
AM
7379 asection *curr;
7380 unsigned int len;
d9352518 7381
a0c8462f 7382 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7383 if (strcmp (curr->name, name) == 0)
7384 {
7385 *result = curr->vma;
7386 return TRUE;
7387 }
7388
7389 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7390 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7391 {
7392 len = strlen (curr->name);
a0c8462f 7393 if (len > strlen (name))
d9352518
DB
7394 continue;
7395
7396 if (strncmp (curr->name, name, len) == 0)
7397 {
7398 if (strncmp (".end", name + len, 4) == 0)
7399 {
7400 *result = curr->vma + curr->size;
7401 return TRUE;
7402 }
7403
7404 /* Insert more pseudo-section names here, if you like. */
7405 }
7406 }
a0c8462f 7407
d9352518
DB
7408 return FALSE;
7409}
7410
7411static void
a0c8462f 7412undefined_reference (const char *reftype, const char *name)
d9352518 7413{
a0c8462f
AM
7414 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7415 reftype, name);
d9352518
DB
7416}
7417
7418static bfd_boolean
a0c8462f
AM
7419eval_symbol (bfd_vma *result,
7420 const char **symp,
7421 bfd *input_bfd,
7422 struct elf_final_link_info *finfo,
7423 bfd_vma dot,
7424 Elf_Internal_Sym *isymbuf,
7425 size_t locsymcount,
7426 int signed_p)
d9352518 7427{
4b93929b
NC
7428 size_t len;
7429 size_t symlen;
a0c8462f
AM
7430 bfd_vma a;
7431 bfd_vma b;
4b93929b 7432 char symbuf[4096];
0f02bbd9 7433 const char *sym = *symp;
a0c8462f
AM
7434 const char *symend;
7435 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7436
7437 len = strlen (sym);
7438 symend = sym + len;
7439
4b93929b 7440 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7441 {
7442 bfd_set_error (bfd_error_invalid_operation);
7443 return FALSE;
7444 }
a0c8462f 7445
d9352518
DB
7446 switch (* sym)
7447 {
7448 case '.':
0f02bbd9
AM
7449 *result = dot;
7450 *symp = sym + 1;
d9352518
DB
7451 return TRUE;
7452
7453 case '#':
0f02bbd9
AM
7454 ++sym;
7455 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7456 return TRUE;
7457
7458 case 'S':
7459 symbol_is_section = TRUE;
a0c8462f 7460 case 's':
0f02bbd9
AM
7461 ++sym;
7462 symlen = strtol (sym, (char **) symp, 10);
7463 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7464
4b93929b 7465 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7466 {
7467 bfd_set_error (bfd_error_invalid_operation);
7468 return FALSE;
7469 }
7470
7471 memcpy (symbuf, sym, symlen);
a0c8462f 7472 symbuf[symlen] = '\0';
0f02bbd9 7473 *symp = sym + symlen;
a0c8462f
AM
7474
7475 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7476 the symbol as a section, or vice-versa. so we're pretty liberal in our
7477 interpretation here; section means "try section first", not "must be a
7478 section", and likewise with symbol. */
7479
a0c8462f 7480 if (symbol_is_section)
d9352518 7481 {
8977835c
AM
7482 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7483 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7484 isymbuf, locsymcount))
d9352518
DB
7485 {
7486 undefined_reference ("section", symbuf);
7487 return FALSE;
7488 }
a0c8462f
AM
7489 }
7490 else
d9352518 7491 {
8977835c
AM
7492 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7493 isymbuf, locsymcount)
7494 && !resolve_section (symbuf, finfo->output_bfd->sections,
7495 result))
d9352518
DB
7496 {
7497 undefined_reference ("symbol", symbuf);
7498 return FALSE;
7499 }
7500 }
7501
7502 return TRUE;
a0c8462f 7503
d9352518
DB
7504 /* All that remains are operators. */
7505
7506#define UNARY_OP(op) \
7507 if (strncmp (sym, #op, strlen (#op)) == 0) \
7508 { \
7509 sym += strlen (#op); \
a0c8462f
AM
7510 if (*sym == ':') \
7511 ++sym; \
0f02bbd9
AM
7512 *symp = sym; \
7513 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7514 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7515 return FALSE; \
7516 if (signed_p) \
0f02bbd9 7517 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7518 else \
7519 *result = op a; \
d9352518
DB
7520 return TRUE; \
7521 }
7522
7523#define BINARY_OP(op) \
7524 if (strncmp (sym, #op, strlen (#op)) == 0) \
7525 { \
7526 sym += strlen (#op); \
a0c8462f
AM
7527 if (*sym == ':') \
7528 ++sym; \
0f02bbd9
AM
7529 *symp = sym; \
7530 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7531 isymbuf, locsymcount, signed_p)) \
a0c8462f 7532 return FALSE; \
0f02bbd9
AM
7533 ++*symp; \
7534 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7535 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7536 return FALSE; \
7537 if (signed_p) \
0f02bbd9 7538 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7539 else \
7540 *result = a op b; \
d9352518
DB
7541 return TRUE; \
7542 }
7543
7544 default:
7545 UNARY_OP (0-);
7546 BINARY_OP (<<);
7547 BINARY_OP (>>);
7548 BINARY_OP (==);
7549 BINARY_OP (!=);
7550 BINARY_OP (<=);
7551 BINARY_OP (>=);
7552 BINARY_OP (&&);
7553 BINARY_OP (||);
7554 UNARY_OP (~);
7555 UNARY_OP (!);
7556 BINARY_OP (*);
7557 BINARY_OP (/);
7558 BINARY_OP (%);
7559 BINARY_OP (^);
7560 BINARY_OP (|);
7561 BINARY_OP (&);
7562 BINARY_OP (+);
7563 BINARY_OP (-);
7564 BINARY_OP (<);
7565 BINARY_OP (>);
7566#undef UNARY_OP
7567#undef BINARY_OP
7568 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7569 bfd_set_error (bfd_error_invalid_operation);
7570 return FALSE;
7571 }
7572}
7573
d9352518 7574static void
a0c8462f
AM
7575put_value (bfd_vma size,
7576 unsigned long chunksz,
7577 bfd *input_bfd,
7578 bfd_vma x,
7579 bfd_byte *location)
d9352518
DB
7580{
7581 location += (size - chunksz);
7582
a0c8462f 7583 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7584 {
7585 switch (chunksz)
7586 {
7587 default:
7588 case 0:
7589 abort ();
7590 case 1:
7591 bfd_put_8 (input_bfd, x, location);
7592 break;
7593 case 2:
7594 bfd_put_16 (input_bfd, x, location);
7595 break;
7596 case 4:
7597 bfd_put_32 (input_bfd, x, location);
7598 break;
7599 case 8:
7600#ifdef BFD64
7601 bfd_put_64 (input_bfd, x, location);
7602#else
7603 abort ();
7604#endif
7605 break;
7606 }
7607 }
7608}
7609
a0c8462f
AM
7610static bfd_vma
7611get_value (bfd_vma size,
7612 unsigned long chunksz,
7613 bfd *input_bfd,
7614 bfd_byte *location)
d9352518
DB
7615{
7616 bfd_vma x = 0;
7617
a0c8462f 7618 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7619 {
7620 switch (chunksz)
7621 {
7622 default:
7623 case 0:
7624 abort ();
7625 case 1:
7626 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7627 break;
7628 case 2:
7629 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7630 break;
7631 case 4:
7632 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7633 break;
7634 case 8:
7635#ifdef BFD64
7636 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7637#else
7638 abort ();
7639#endif
7640 break;
7641 }
7642 }
7643 return x;
7644}
7645
a0c8462f
AM
7646static void
7647decode_complex_addend (unsigned long *start, /* in bits */
7648 unsigned long *oplen, /* in bits */
7649 unsigned long *len, /* in bits */
7650 unsigned long *wordsz, /* in bytes */
7651 unsigned long *chunksz, /* in bytes */
7652 unsigned long *lsb0_p,
7653 unsigned long *signed_p,
7654 unsigned long *trunc_p,
7655 unsigned long encoded)
d9352518
DB
7656{
7657 * start = encoded & 0x3F;
7658 * len = (encoded >> 6) & 0x3F;
7659 * oplen = (encoded >> 12) & 0x3F;
7660 * wordsz = (encoded >> 18) & 0xF;
7661 * chunksz = (encoded >> 22) & 0xF;
7662 * lsb0_p = (encoded >> 27) & 1;
7663 * signed_p = (encoded >> 28) & 1;
7664 * trunc_p = (encoded >> 29) & 1;
7665}
7666
cdfeee4f 7667bfd_reloc_status_type
0f02bbd9 7668bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7669 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7670 bfd_byte *contents,
7671 Elf_Internal_Rela *rel,
7672 bfd_vma relocation)
d9352518 7673{
0f02bbd9
AM
7674 bfd_vma shift, x, mask;
7675 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7676 bfd_reloc_status_type r;
d9352518
DB
7677
7678 /* Perform this reloc, since it is complex.
7679 (this is not to say that it necessarily refers to a complex
7680 symbol; merely that it is a self-describing CGEN based reloc.
7681 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7682 word size, etc) encoded within it.). */
d9352518 7683
a0c8462f
AM
7684 decode_complex_addend (&start, &oplen, &len, &wordsz,
7685 &chunksz, &lsb0_p, &signed_p,
7686 &trunc_p, rel->r_addend);
d9352518
DB
7687
7688 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7689
7690 if (lsb0_p)
7691 shift = (start + 1) - len;
7692 else
7693 shift = (8 * wordsz) - (start + len);
7694
a0c8462f 7695 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7696
7697#ifdef DEBUG
7698 printf ("Doing complex reloc: "
7699 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7700 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7701 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7702 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7703 oplen, x, mask, relocation);
7704#endif
7705
cdfeee4f 7706 r = bfd_reloc_ok;
d9352518 7707 if (! trunc_p)
cdfeee4f
AM
7708 /* Now do an overflow check. */
7709 r = bfd_check_overflow ((signed_p
7710 ? complain_overflow_signed
7711 : complain_overflow_unsigned),
7712 len, 0, (8 * wordsz),
7713 relocation);
a0c8462f 7714
d9352518
DB
7715 /* Do the deed. */
7716 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7717
7718#ifdef DEBUG
7719 printf (" relocation: %8.8lx\n"
7720 " shifted mask: %8.8lx\n"
7721 " shifted/masked reloc: %8.8lx\n"
7722 " result: %8.8lx\n",
a0c8462f 7723 relocation, (mask << shift),
d9352518
DB
7724 ((relocation & mask) << shift), x);
7725#endif
7726 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7727 return r;
d9352518
DB
7728}
7729
c152c796
AM
7730/* When performing a relocatable link, the input relocations are
7731 preserved. But, if they reference global symbols, the indices
7732 referenced must be updated. Update all the relocations in
7733 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7734
7735static void
7736elf_link_adjust_relocs (bfd *abfd,
7737 Elf_Internal_Shdr *rel_hdr,
7738 unsigned int count,
7739 struct elf_link_hash_entry **rel_hash)
7740{
7741 unsigned int i;
7742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7743 bfd_byte *erela;
7744 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7745 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7746 bfd_vma r_type_mask;
7747 int r_sym_shift;
7748
7749 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7750 {
7751 swap_in = bed->s->swap_reloc_in;
7752 swap_out = bed->s->swap_reloc_out;
7753 }
7754 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7755 {
7756 swap_in = bed->s->swap_reloca_in;
7757 swap_out = bed->s->swap_reloca_out;
7758 }
7759 else
7760 abort ();
7761
7762 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7763 abort ();
7764
7765 if (bed->s->arch_size == 32)
7766 {
7767 r_type_mask = 0xff;
7768 r_sym_shift = 8;
7769 }
7770 else
7771 {
7772 r_type_mask = 0xffffffff;
7773 r_sym_shift = 32;
7774 }
7775
7776 erela = rel_hdr->contents;
7777 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7778 {
7779 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7780 unsigned int j;
7781
7782 if (*rel_hash == NULL)
7783 continue;
7784
7785 BFD_ASSERT ((*rel_hash)->indx >= 0);
7786
7787 (*swap_in) (abfd, erela, irela);
7788 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7789 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7790 | (irela[j].r_info & r_type_mask));
7791 (*swap_out) (abfd, irela, erela);
7792 }
7793}
7794
7795struct elf_link_sort_rela
7796{
7797 union {
7798 bfd_vma offset;
7799 bfd_vma sym_mask;
7800 } u;
7801 enum elf_reloc_type_class type;
7802 /* We use this as an array of size int_rels_per_ext_rel. */
7803 Elf_Internal_Rela rela[1];
7804};
7805
7806static int
7807elf_link_sort_cmp1 (const void *A, const void *B)
7808{
7809 const struct elf_link_sort_rela *a = A;
7810 const struct elf_link_sort_rela *b = B;
7811 int relativea, relativeb;
7812
7813 relativea = a->type == reloc_class_relative;
7814 relativeb = b->type == reloc_class_relative;
7815
7816 if (relativea < relativeb)
7817 return 1;
7818 if (relativea > relativeb)
7819 return -1;
7820 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7821 return -1;
7822 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7823 return 1;
7824 if (a->rela->r_offset < b->rela->r_offset)
7825 return -1;
7826 if (a->rela->r_offset > b->rela->r_offset)
7827 return 1;
7828 return 0;
7829}
7830
7831static int
7832elf_link_sort_cmp2 (const void *A, const void *B)
7833{
7834 const struct elf_link_sort_rela *a = A;
7835 const struct elf_link_sort_rela *b = B;
7836 int copya, copyb;
7837
7838 if (a->u.offset < b->u.offset)
7839 return -1;
7840 if (a->u.offset > b->u.offset)
7841 return 1;
7842 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7843 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7844 if (copya < copyb)
7845 return -1;
7846 if (copya > copyb)
7847 return 1;
7848 if (a->rela->r_offset < b->rela->r_offset)
7849 return -1;
7850 if (a->rela->r_offset > b->rela->r_offset)
7851 return 1;
7852 return 0;
7853}
7854
7855static size_t
7856elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7857{
3410fea8 7858 asection *dynamic_relocs;
fc66a176
L
7859 asection *rela_dyn;
7860 asection *rel_dyn;
c152c796
AM
7861 bfd_size_type count, size;
7862 size_t i, ret, sort_elt, ext_size;
7863 bfd_byte *sort, *s_non_relative, *p;
7864 struct elf_link_sort_rela *sq;
7865 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7866 int i2e = bed->s->int_rels_per_ext_rel;
7867 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7868 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7869 struct bfd_link_order *lo;
7870 bfd_vma r_sym_mask;
3410fea8 7871 bfd_boolean use_rela;
c152c796 7872
3410fea8
NC
7873 /* Find a dynamic reloc section. */
7874 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7875 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7876 if (rela_dyn != NULL && rela_dyn->size > 0
7877 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 7878 {
3410fea8
NC
7879 bfd_boolean use_rela_initialised = FALSE;
7880
7881 /* This is just here to stop gcc from complaining.
7882 It's initialization checking code is not perfect. */
7883 use_rela = TRUE;
7884
7885 /* Both sections are present. Examine the sizes
7886 of the indirect sections to help us choose. */
7887 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7888 if (lo->type == bfd_indirect_link_order)
7889 {
7890 asection *o = lo->u.indirect.section;
7891
7892 if ((o->size % bed->s->sizeof_rela) == 0)
7893 {
7894 if ((o->size % bed->s->sizeof_rel) == 0)
7895 /* Section size is divisible by both rel and rela sizes.
7896 It is of no help to us. */
7897 ;
7898 else
7899 {
7900 /* Section size is only divisible by rela. */
7901 if (use_rela_initialised && (use_rela == FALSE))
7902 {
7903 _bfd_error_handler
7904 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7905 bfd_set_error (bfd_error_invalid_operation);
7906 return 0;
7907 }
7908 else
7909 {
7910 use_rela = TRUE;
7911 use_rela_initialised = TRUE;
7912 }
7913 }
7914 }
7915 else if ((o->size % bed->s->sizeof_rel) == 0)
7916 {
7917 /* Section size is only divisible by rel. */
7918 if (use_rela_initialised && (use_rela == TRUE))
7919 {
7920 _bfd_error_handler
7921 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7922 bfd_set_error (bfd_error_invalid_operation);
7923 return 0;
7924 }
7925 else
7926 {
7927 use_rela = FALSE;
7928 use_rela_initialised = TRUE;
7929 }
7930 }
7931 else
7932 {
7933 /* The section size is not divisible by either - something is wrong. */
7934 _bfd_error_handler
7935 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7936 bfd_set_error (bfd_error_invalid_operation);
7937 return 0;
7938 }
7939 }
7940
7941 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7942 if (lo->type == bfd_indirect_link_order)
7943 {
7944 asection *o = lo->u.indirect.section;
7945
7946 if ((o->size % bed->s->sizeof_rela) == 0)
7947 {
7948 if ((o->size % bed->s->sizeof_rel) == 0)
7949 /* Section size is divisible by both rel and rela sizes.
7950 It is of no help to us. */
7951 ;
7952 else
7953 {
7954 /* Section size is only divisible by rela. */
7955 if (use_rela_initialised && (use_rela == FALSE))
7956 {
7957 _bfd_error_handler
7958 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7959 bfd_set_error (bfd_error_invalid_operation);
7960 return 0;
7961 }
7962 else
7963 {
7964 use_rela = TRUE;
7965 use_rela_initialised = TRUE;
7966 }
7967 }
7968 }
7969 else if ((o->size % bed->s->sizeof_rel) == 0)
7970 {
7971 /* Section size is only divisible by rel. */
7972 if (use_rela_initialised && (use_rela == TRUE))
7973 {
7974 _bfd_error_handler
7975 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7976 bfd_set_error (bfd_error_invalid_operation);
7977 return 0;
7978 }
7979 else
7980 {
7981 use_rela = FALSE;
7982 use_rela_initialised = TRUE;
7983 }
7984 }
7985 else
7986 {
7987 /* The section size is not divisible by either - something is wrong. */
7988 _bfd_error_handler
7989 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7990 bfd_set_error (bfd_error_invalid_operation);
7991 return 0;
7992 }
7993 }
7994
7995 if (! use_rela_initialised)
7996 /* Make a guess. */
7997 use_rela = TRUE;
c152c796 7998 }
fc66a176
L
7999 else if (rela_dyn != NULL && rela_dyn->size > 0)
8000 use_rela = TRUE;
8001 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8002 use_rela = FALSE;
c152c796 8003 else
fc66a176 8004 return 0;
3410fea8
NC
8005
8006 if (use_rela)
c152c796 8007 {
3410fea8 8008 dynamic_relocs = rela_dyn;
c152c796
AM
8009 ext_size = bed->s->sizeof_rela;
8010 swap_in = bed->s->swap_reloca_in;
8011 swap_out = bed->s->swap_reloca_out;
8012 }
3410fea8
NC
8013 else
8014 {
8015 dynamic_relocs = rel_dyn;
8016 ext_size = bed->s->sizeof_rel;
8017 swap_in = bed->s->swap_reloc_in;
8018 swap_out = bed->s->swap_reloc_out;
8019 }
c152c796
AM
8020
8021 size = 0;
3410fea8 8022 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8023 if (lo->type == bfd_indirect_link_order)
3410fea8 8024 size += lo->u.indirect.section->size;
c152c796 8025
3410fea8 8026 if (size != dynamic_relocs->size)
c152c796
AM
8027 return 0;
8028
8029 sort_elt = (sizeof (struct elf_link_sort_rela)
8030 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8031
8032 count = dynamic_relocs->size / ext_size;
c152c796 8033 sort = bfd_zmalloc (sort_elt * count);
3410fea8 8034
c152c796
AM
8035 if (sort == NULL)
8036 {
8037 (*info->callbacks->warning)
8038 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8039 return 0;
8040 }
8041
8042 if (bed->s->arch_size == 32)
8043 r_sym_mask = ~(bfd_vma) 0xff;
8044 else
8045 r_sym_mask = ~(bfd_vma) 0xffffffff;
8046
3410fea8 8047 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8048 if (lo->type == bfd_indirect_link_order)
8049 {
8050 bfd_byte *erel, *erelend;
8051 asection *o = lo->u.indirect.section;
8052
1da212d6
AM
8053 if (o->contents == NULL && o->size != 0)
8054 {
8055 /* This is a reloc section that is being handled as a normal
8056 section. See bfd_section_from_shdr. We can't combine
8057 relocs in this case. */
8058 free (sort);
8059 return 0;
8060 }
c152c796 8061 erel = o->contents;
eea6121a 8062 erelend = o->contents + o->size;
c152c796 8063 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8064
c152c796
AM
8065 while (erel < erelend)
8066 {
8067 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8068
c152c796
AM
8069 (*swap_in) (abfd, erel, s->rela);
8070 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8071 s->u.sym_mask = r_sym_mask;
8072 p += sort_elt;
8073 erel += ext_size;
8074 }
8075 }
8076
8077 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8078
8079 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8080 {
8081 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8082 if (s->type != reloc_class_relative)
8083 break;
8084 }
8085 ret = i;
8086 s_non_relative = p;
8087
8088 sq = (struct elf_link_sort_rela *) s_non_relative;
8089 for (; i < count; i++, p += sort_elt)
8090 {
8091 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8092 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8093 sq = sp;
8094 sp->u.offset = sq->rela->r_offset;
8095 }
8096
8097 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8098
3410fea8 8099 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8100 if (lo->type == bfd_indirect_link_order)
8101 {
8102 bfd_byte *erel, *erelend;
8103 asection *o = lo->u.indirect.section;
8104
8105 erel = o->contents;
eea6121a 8106 erelend = o->contents + o->size;
c152c796
AM
8107 p = sort + o->output_offset / ext_size * sort_elt;
8108 while (erel < erelend)
8109 {
8110 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8111 (*swap_out) (abfd, s->rela, erel);
8112 p += sort_elt;
8113 erel += ext_size;
8114 }
8115 }
8116
8117 free (sort);
3410fea8 8118 *psec = dynamic_relocs;
c152c796
AM
8119 return ret;
8120}
8121
8122/* Flush the output symbols to the file. */
8123
8124static bfd_boolean
8125elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8126 const struct elf_backend_data *bed)
8127{
8128 if (finfo->symbuf_count > 0)
8129 {
8130 Elf_Internal_Shdr *hdr;
8131 file_ptr pos;
8132 bfd_size_type amt;
8133
8134 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8135 pos = hdr->sh_offset + hdr->sh_size;
8136 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8137 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8138 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8139 return FALSE;
8140
8141 hdr->sh_size += amt;
8142 finfo->symbuf_count = 0;
8143 }
8144
8145 return TRUE;
8146}
8147
8148/* Add a symbol to the output symbol table. */
8149
8150static bfd_boolean
8151elf_link_output_sym (struct elf_final_link_info *finfo,
8152 const char *name,
8153 Elf_Internal_Sym *elfsym,
8154 asection *input_sec,
8155 struct elf_link_hash_entry *h)
8156{
8157 bfd_byte *dest;
8158 Elf_External_Sym_Shndx *destshndx;
8159 bfd_boolean (*output_symbol_hook)
8160 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8161 struct elf_link_hash_entry *);
8162 const struct elf_backend_data *bed;
8163
8164 bed = get_elf_backend_data (finfo->output_bfd);
8165 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8166 if (output_symbol_hook != NULL)
8167 {
8168 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8169 return FALSE;
8170 }
8171
8172 if (name == NULL || *name == '\0')
8173 elfsym->st_name = 0;
8174 else if (input_sec->flags & SEC_EXCLUDE)
8175 elfsym->st_name = 0;
8176 else
8177 {
8178 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8179 name, TRUE, FALSE);
8180 if (elfsym->st_name == (unsigned long) -1)
8181 return FALSE;
8182 }
8183
8184 if (finfo->symbuf_count >= finfo->symbuf_size)
8185 {
8186 if (! elf_link_flush_output_syms (finfo, bed))
8187 return FALSE;
8188 }
8189
8190 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8191 destshndx = finfo->symshndxbuf;
8192 if (destshndx != NULL)
8193 {
8194 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8195 {
8196 bfd_size_type amt;
8197
8198 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
515ef31d 8199 destshndx = bfd_realloc (destshndx, amt * 2);
c152c796
AM
8200 if (destshndx == NULL)
8201 return FALSE;
515ef31d 8202 finfo->symshndxbuf = destshndx;
c152c796
AM
8203 memset ((char *) destshndx + amt, 0, amt);
8204 finfo->shndxbuf_size *= 2;
8205 }
8206 destshndx += bfd_get_symcount (finfo->output_bfd);
8207 }
8208
8209 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8210 finfo->symbuf_count += 1;
8211 bfd_get_symcount (finfo->output_bfd) += 1;
8212
8213 return TRUE;
8214}
8215
c0d5a53d
L
8216/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8217
8218static bfd_boolean
8219check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8220{
4fbb74a6
AM
8221 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8222 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8223 {
8224 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8225 beyond 64k. */
c0d5a53d
L
8226 (*_bfd_error_handler)
8227 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8228 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8229 bfd_set_error (bfd_error_nonrepresentable_section);
8230 return FALSE;
8231 }
8232 return TRUE;
8233}
8234
c152c796
AM
8235/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8236 allowing an unsatisfied unversioned symbol in the DSO to match a
8237 versioned symbol that would normally require an explicit version.
8238 We also handle the case that a DSO references a hidden symbol
8239 which may be satisfied by a versioned symbol in another DSO. */
8240
8241static bfd_boolean
8242elf_link_check_versioned_symbol (struct bfd_link_info *info,
8243 const struct elf_backend_data *bed,
8244 struct elf_link_hash_entry *h)
8245{
8246 bfd *abfd;
8247 struct elf_link_loaded_list *loaded;
8248
8249 if (!is_elf_hash_table (info->hash))
8250 return FALSE;
8251
8252 switch (h->root.type)
8253 {
8254 default:
8255 abfd = NULL;
8256 break;
8257
8258 case bfd_link_hash_undefined:
8259 case bfd_link_hash_undefweak:
8260 abfd = h->root.u.undef.abfd;
8261 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8262 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8263 return FALSE;
8264 break;
8265
8266 case bfd_link_hash_defined:
8267 case bfd_link_hash_defweak:
8268 abfd = h->root.u.def.section->owner;
8269 break;
8270
8271 case bfd_link_hash_common:
8272 abfd = h->root.u.c.p->section->owner;
8273 break;
8274 }
8275 BFD_ASSERT (abfd != NULL);
8276
8277 for (loaded = elf_hash_table (info)->loaded;
8278 loaded != NULL;
8279 loaded = loaded->next)
8280 {
8281 bfd *input;
8282 Elf_Internal_Shdr *hdr;
8283 bfd_size_type symcount;
8284 bfd_size_type extsymcount;
8285 bfd_size_type extsymoff;
8286 Elf_Internal_Shdr *versymhdr;
8287 Elf_Internal_Sym *isym;
8288 Elf_Internal_Sym *isymend;
8289 Elf_Internal_Sym *isymbuf;
8290 Elf_External_Versym *ever;
8291 Elf_External_Versym *extversym;
8292
8293 input = loaded->abfd;
8294
8295 /* We check each DSO for a possible hidden versioned definition. */
8296 if (input == abfd
8297 || (input->flags & DYNAMIC) == 0
8298 || elf_dynversym (input) == 0)
8299 continue;
8300
8301 hdr = &elf_tdata (input)->dynsymtab_hdr;
8302
8303 symcount = hdr->sh_size / bed->s->sizeof_sym;
8304 if (elf_bad_symtab (input))
8305 {
8306 extsymcount = symcount;
8307 extsymoff = 0;
8308 }
8309 else
8310 {
8311 extsymcount = symcount - hdr->sh_info;
8312 extsymoff = hdr->sh_info;
8313 }
8314
8315 if (extsymcount == 0)
8316 continue;
8317
8318 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8319 NULL, NULL, NULL);
8320 if (isymbuf == NULL)
8321 return FALSE;
8322
8323 /* Read in any version definitions. */
8324 versymhdr = &elf_tdata (input)->dynversym_hdr;
8325 extversym = bfd_malloc (versymhdr->sh_size);
8326 if (extversym == NULL)
8327 goto error_ret;
8328
8329 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8330 || (bfd_bread (extversym, versymhdr->sh_size, input)
8331 != versymhdr->sh_size))
8332 {
8333 free (extversym);
8334 error_ret:
8335 free (isymbuf);
8336 return FALSE;
8337 }
8338
8339 ever = extversym + extsymoff;
8340 isymend = isymbuf + extsymcount;
8341 for (isym = isymbuf; isym < isymend; isym++, ever++)
8342 {
8343 const char *name;
8344 Elf_Internal_Versym iver;
8345 unsigned short version_index;
8346
8347 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8348 || isym->st_shndx == SHN_UNDEF)
8349 continue;
8350
8351 name = bfd_elf_string_from_elf_section (input,
8352 hdr->sh_link,
8353 isym->st_name);
8354 if (strcmp (name, h->root.root.string) != 0)
8355 continue;
8356
8357 _bfd_elf_swap_versym_in (input, ever, &iver);
8358
8359 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8360 {
8361 /* If we have a non-hidden versioned sym, then it should
8362 have provided a definition for the undefined sym. */
8363 abort ();
8364 }
8365
8366 version_index = iver.vs_vers & VERSYM_VERSION;
8367 if (version_index == 1 || version_index == 2)
8368 {
8369 /* This is the base or first version. We can use it. */
8370 free (extversym);
8371 free (isymbuf);
8372 return TRUE;
8373 }
8374 }
8375
8376 free (extversym);
8377 free (isymbuf);
8378 }
8379
8380 return FALSE;
8381}
8382
8383/* Add an external symbol to the symbol table. This is called from
8384 the hash table traversal routine. When generating a shared object,
8385 we go through the symbol table twice. The first time we output
8386 anything that might have been forced to local scope in a version
8387 script. The second time we output the symbols that are still
8388 global symbols. */
8389
8390static bfd_boolean
8391elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8392{
8393 struct elf_outext_info *eoinfo = data;
8394 struct elf_final_link_info *finfo = eoinfo->finfo;
8395 bfd_boolean strip;
8396 Elf_Internal_Sym sym;
8397 asection *input_sec;
8398 const struct elf_backend_data *bed;
8399
8400 if (h->root.type == bfd_link_hash_warning)
8401 {
8402 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8403 if (h->root.type == bfd_link_hash_new)
8404 return TRUE;
8405 }
8406
8407 /* Decide whether to output this symbol in this pass. */
8408 if (eoinfo->localsyms)
8409 {
f5385ebf 8410 if (!h->forced_local)
c152c796
AM
8411 return TRUE;
8412 }
8413 else
8414 {
f5385ebf 8415 if (h->forced_local)
c152c796
AM
8416 return TRUE;
8417 }
8418
8419 bed = get_elf_backend_data (finfo->output_bfd);
8420
12ac1cf5 8421 if (h->root.type == bfd_link_hash_undefined)
c152c796 8422 {
12ac1cf5
NC
8423 /* If we have an undefined symbol reference here then it must have
8424 come from a shared library that is being linked in. (Undefined
8425 references in regular files have already been handled). */
8426 bfd_boolean ignore_undef = FALSE;
8427
8428 /* Some symbols may be special in that the fact that they're
8429 undefined can be safely ignored - let backend determine that. */
8430 if (bed->elf_backend_ignore_undef_symbol)
8431 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8432
8433 /* If we are reporting errors for this situation then do so now. */
8434 if (ignore_undef == FALSE
8435 && h->ref_dynamic
8436 && ! h->ref_regular
8437 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8438 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8439 {
12ac1cf5
NC
8440 if (! (finfo->info->callbacks->undefined_symbol
8441 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8442 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8443 {
8444 eoinfo->failed = TRUE;
8445 return FALSE;
8446 }
c152c796
AM
8447 }
8448 }
8449
8450 /* We should also warn if a forced local symbol is referenced from
8451 shared libraries. */
8452 if (! finfo->info->relocatable
8453 && (! finfo->info->shared)
f5385ebf
AM
8454 && h->forced_local
8455 && h->ref_dynamic
8456 && !h->dynamic_def
8457 && !h->dynamic_weak
c152c796
AM
8458 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8459 {
8460 (*_bfd_error_handler)
d003868e 8461 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
cfca085c
L
8462 finfo->output_bfd,
8463 h->root.u.def.section == bfd_abs_section_ptr
8464 ? finfo->output_bfd : h->root.u.def.section->owner,
c152c796
AM
8465 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8466 ? "internal"
8467 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
d003868e
AM
8468 ? "hidden" : "local",
8469 h->root.root.string);
c152c796
AM
8470 eoinfo->failed = TRUE;
8471 return FALSE;
8472 }
8473
8474 /* We don't want to output symbols that have never been mentioned by
8475 a regular file, or that we have been told to strip. However, if
8476 h->indx is set to -2, the symbol is used by a reloc and we must
8477 output it. */
8478 if (h->indx == -2)
8479 strip = FALSE;
f5385ebf 8480 else if ((h->def_dynamic
77cfaee6
AM
8481 || h->ref_dynamic
8482 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8483 && !h->def_regular
8484 && !h->ref_regular)
c152c796
AM
8485 strip = TRUE;
8486 else if (finfo->info->strip == strip_all)
8487 strip = TRUE;
8488 else if (finfo->info->strip == strip_some
8489 && bfd_hash_lookup (finfo->info->keep_hash,
8490 h->root.root.string, FALSE, FALSE) == NULL)
8491 strip = TRUE;
8492 else if (finfo->info->strip_discarded
8493 && (h->root.type == bfd_link_hash_defined
8494 || h->root.type == bfd_link_hash_defweak)
8495 && elf_discarded_section (h->root.u.def.section))
8496 strip = TRUE;
8497 else
8498 strip = FALSE;
8499
8500 /* If we're stripping it, and it's not a dynamic symbol, there's
8501 nothing else to do unless it is a forced local symbol. */
8502 if (strip
8503 && h->dynindx == -1
f5385ebf 8504 && !h->forced_local)
c152c796
AM
8505 return TRUE;
8506
8507 sym.st_value = 0;
8508 sym.st_size = h->size;
8509 sym.st_other = h->other;
f5385ebf 8510 if (h->forced_local)
c152c796
AM
8511 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8512 else if (h->root.type == bfd_link_hash_undefweak
8513 || h->root.type == bfd_link_hash_defweak)
8514 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8515 else
8516 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8517
8518 switch (h->root.type)
8519 {
8520 default:
8521 case bfd_link_hash_new:
8522 case bfd_link_hash_warning:
8523 abort ();
8524 return FALSE;
8525
8526 case bfd_link_hash_undefined:
8527 case bfd_link_hash_undefweak:
8528 input_sec = bfd_und_section_ptr;
8529 sym.st_shndx = SHN_UNDEF;
8530 break;
8531
8532 case bfd_link_hash_defined:
8533 case bfd_link_hash_defweak:
8534 {
8535 input_sec = h->root.u.def.section;
8536 if (input_sec->output_section != NULL)
8537 {
8538 sym.st_shndx =
8539 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8540 input_sec->output_section);
8541 if (sym.st_shndx == SHN_BAD)
8542 {
8543 (*_bfd_error_handler)
d003868e
AM
8544 (_("%B: could not find output section %A for input section %A"),
8545 finfo->output_bfd, input_sec->output_section, input_sec);
c152c796
AM
8546 eoinfo->failed = TRUE;
8547 return FALSE;
8548 }
8549
8550 /* ELF symbols in relocatable files are section relative,
8551 but in nonrelocatable files they are virtual
8552 addresses. */
8553 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8554 if (! finfo->info->relocatable)
8555 {
8556 sym.st_value += input_sec->output_section->vma;
8557 if (h->type == STT_TLS)
8558 {
430a16a5
NC
8559 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8560 if (tls_sec != NULL)
8561 sym.st_value -= tls_sec->vma;
8562 else
8563 {
8564 /* The TLS section may have been garbage collected. */
8565 BFD_ASSERT (finfo->info->gc_sections
8566 && !input_sec->gc_mark);
8567 }
c152c796
AM
8568 }
8569 }
8570 }
8571 else
8572 {
8573 BFD_ASSERT (input_sec->owner == NULL
8574 || (input_sec->owner->flags & DYNAMIC) != 0);
8575 sym.st_shndx = SHN_UNDEF;
8576 input_sec = bfd_und_section_ptr;
8577 }
8578 }
8579 break;
8580
8581 case bfd_link_hash_common:
8582 input_sec = h->root.u.c.p->section;
a4d8e49b 8583 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8584 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8585 break;
8586
8587 case bfd_link_hash_indirect:
8588 /* These symbols are created by symbol versioning. They point
8589 to the decorated version of the name. For example, if the
8590 symbol foo@@GNU_1.2 is the default, which should be used when
8591 foo is used with no version, then we add an indirect symbol
8592 foo which points to foo@@GNU_1.2. We ignore these symbols,
8593 since the indirected symbol is already in the hash table. */
8594 return TRUE;
8595 }
8596
8597 /* Give the processor backend a chance to tweak the symbol value,
8598 and also to finish up anything that needs to be done for this
8599 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8600 forced local syms when non-shared is due to a historical quirk. */
8601 if ((h->dynindx != -1
f5385ebf 8602 || h->forced_local)
c152c796
AM
8603 && ((finfo->info->shared
8604 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8605 || h->root.type != bfd_link_hash_undefweak))
f5385ebf 8606 || !h->forced_local)
c152c796
AM
8607 && elf_hash_table (finfo->info)->dynamic_sections_created)
8608 {
8609 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8610 (finfo->output_bfd, finfo->info, h, &sym)))
8611 {
8612 eoinfo->failed = TRUE;
8613 return FALSE;
8614 }
8615 }
8616
8617 /* If we are marking the symbol as undefined, and there are no
8618 non-weak references to this symbol from a regular object, then
8619 mark the symbol as weak undefined; if there are non-weak
8620 references, mark the symbol as strong. We can't do this earlier,
8621 because it might not be marked as undefined until the
8622 finish_dynamic_symbol routine gets through with it. */
8623 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8624 && h->ref_regular
c152c796
AM
8625 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8626 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8627 {
8628 int bindtype;
8629
f5385ebf 8630 if (h->ref_regular_nonweak)
c152c796
AM
8631 bindtype = STB_GLOBAL;
8632 else
8633 bindtype = STB_WEAK;
8634 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8635 }
8636
bda987c2
CD
8637 /* If this is a symbol defined in a dynamic library, don't use the
8638 symbol size from the dynamic library. Relinking an executable
8639 against a new library may introduce gratuitous changes in the
8640 executable's symbols if we keep the size. */
8641 if (sym.st_shndx == SHN_UNDEF
8642 && !h->def_regular
8643 && h->def_dynamic)
8644 sym.st_size = 0;
8645
c152c796
AM
8646 /* If a non-weak symbol with non-default visibility is not defined
8647 locally, it is a fatal error. */
8648 if (! finfo->info->relocatable
8649 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8650 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8651 && h->root.type == bfd_link_hash_undefined
f5385ebf 8652 && !h->def_regular)
c152c796
AM
8653 {
8654 (*_bfd_error_handler)
d003868e
AM
8655 (_("%B: %s symbol `%s' isn't defined"),
8656 finfo->output_bfd,
8657 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8658 ? "protected"
8659 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8660 ? "internal" : "hidden",
8661 h->root.root.string);
c152c796
AM
8662 eoinfo->failed = TRUE;
8663 return FALSE;
8664 }
8665
8666 /* If this symbol should be put in the .dynsym section, then put it
8667 there now. We already know the symbol index. We also fill in
8668 the entry in the .hash section. */
8669 if (h->dynindx != -1
8670 && elf_hash_table (finfo->info)->dynamic_sections_created)
8671 {
c152c796
AM
8672 bfd_byte *esym;
8673
8674 sym.st_name = h->dynstr_index;
8675 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8676 if (! check_dynsym (finfo->output_bfd, &sym))
8677 {
8678 eoinfo->failed = TRUE;
8679 return FALSE;
8680 }
c152c796
AM
8681 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8682
fdc90cb4
JJ
8683 if (finfo->hash_sec != NULL)
8684 {
8685 size_t hash_entry_size;
8686 bfd_byte *bucketpos;
8687 bfd_vma chain;
41198d0c
L
8688 size_t bucketcount;
8689 size_t bucket;
8690
8691 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8692 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8693
8694 hash_entry_size
8695 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8696 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8697 + (bucket + 2) * hash_entry_size);
8698 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8699 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8700 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8701 ((bfd_byte *) finfo->hash_sec->contents
8702 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8703 }
c152c796
AM
8704
8705 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8706 {
8707 Elf_Internal_Versym iversym;
8708 Elf_External_Versym *eversym;
8709
f5385ebf 8710 if (!h->def_regular)
c152c796
AM
8711 {
8712 if (h->verinfo.verdef == NULL)
8713 iversym.vs_vers = 0;
8714 else
8715 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8716 }
8717 else
8718 {
8719 if (h->verinfo.vertree == NULL)
8720 iversym.vs_vers = 1;
8721 else
8722 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8723 if (finfo->info->create_default_symver)
8724 iversym.vs_vers++;
c152c796
AM
8725 }
8726
f5385ebf 8727 if (h->hidden)
c152c796
AM
8728 iversym.vs_vers |= VERSYM_HIDDEN;
8729
8730 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8731 eversym += h->dynindx;
8732 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8733 }
8734 }
8735
8736 /* If we're stripping it, then it was just a dynamic symbol, and
8737 there's nothing else to do. */
8738 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8739 return TRUE;
8740
8741 h->indx = bfd_get_symcount (finfo->output_bfd);
8742
8743 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8744 {
8745 eoinfo->failed = TRUE;
8746 return FALSE;
8747 }
8748
8749 return TRUE;
8750}
8751
cdd3575c
AM
8752/* Return TRUE if special handling is done for relocs in SEC against
8753 symbols defined in discarded sections. */
8754
c152c796
AM
8755static bfd_boolean
8756elf_section_ignore_discarded_relocs (asection *sec)
8757{
8758 const struct elf_backend_data *bed;
8759
cdd3575c
AM
8760 switch (sec->sec_info_type)
8761 {
8762 case ELF_INFO_TYPE_STABS:
8763 case ELF_INFO_TYPE_EH_FRAME:
8764 return TRUE;
8765 default:
8766 break;
8767 }
c152c796
AM
8768
8769 bed = get_elf_backend_data (sec->owner);
8770 if (bed->elf_backend_ignore_discarded_relocs != NULL
8771 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8772 return TRUE;
8773
8774 return FALSE;
8775}
8776
9e66c942
AM
8777/* Return a mask saying how ld should treat relocations in SEC against
8778 symbols defined in discarded sections. If this function returns
8779 COMPLAIN set, ld will issue a warning message. If this function
8780 returns PRETEND set, and the discarded section was link-once and the
8781 same size as the kept link-once section, ld will pretend that the
8782 symbol was actually defined in the kept section. Otherwise ld will
8783 zero the reloc (at least that is the intent, but some cooperation by
8784 the target dependent code is needed, particularly for REL targets). */
8785
8a696751
AM
8786unsigned int
8787_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 8788{
9e66c942 8789 if (sec->flags & SEC_DEBUGGING)
69d54b1b 8790 return PRETEND;
cdd3575c
AM
8791
8792 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 8793 return 0;
cdd3575c
AM
8794
8795 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 8796 return 0;
cdd3575c 8797
9e66c942 8798 return COMPLAIN | PRETEND;
cdd3575c
AM
8799}
8800
3d7f7666
L
8801/* Find a match between a section and a member of a section group. */
8802
8803static asection *
c0f00686
L
8804match_group_member (asection *sec, asection *group,
8805 struct bfd_link_info *info)
3d7f7666
L
8806{
8807 asection *first = elf_next_in_group (group);
8808 asection *s = first;
8809
8810 while (s != NULL)
8811 {
c0f00686 8812 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
8813 return s;
8814
83180ade 8815 s = elf_next_in_group (s);
3d7f7666
L
8816 if (s == first)
8817 break;
8818 }
8819
8820 return NULL;
8821}
8822
01b3c8ab 8823/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
8824 to replace it. Return the replacement if it is OK. Otherwise return
8825 NULL. */
01b3c8ab
L
8826
8827asection *
c0f00686 8828_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
8829{
8830 asection *kept;
8831
8832 kept = sec->kept_section;
8833 if (kept != NULL)
8834 {
c2370991 8835 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 8836 kept = match_group_member (sec, kept, info);
1dd2625f
BW
8837 if (kept != NULL
8838 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8839 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 8840 kept = NULL;
c2370991 8841 sec->kept_section = kept;
01b3c8ab
L
8842 }
8843 return kept;
8844}
8845
c152c796
AM
8846/* Link an input file into the linker output file. This function
8847 handles all the sections and relocations of the input file at once.
8848 This is so that we only have to read the local symbols once, and
8849 don't have to keep them in memory. */
8850
8851static bfd_boolean
8852elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8853{
ece5ef60 8854 int (*relocate_section)
c152c796
AM
8855 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8856 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8857 bfd *output_bfd;
8858 Elf_Internal_Shdr *symtab_hdr;
8859 size_t locsymcount;
8860 size_t extsymoff;
8861 Elf_Internal_Sym *isymbuf;
8862 Elf_Internal_Sym *isym;
8863 Elf_Internal_Sym *isymend;
8864 long *pindex;
8865 asection **ppsection;
8866 asection *o;
8867 const struct elf_backend_data *bed;
c152c796
AM
8868 struct elf_link_hash_entry **sym_hashes;
8869
8870 output_bfd = finfo->output_bfd;
8871 bed = get_elf_backend_data (output_bfd);
8872 relocate_section = bed->elf_backend_relocate_section;
8873
8874 /* If this is a dynamic object, we don't want to do anything here:
8875 we don't want the local symbols, and we don't want the section
8876 contents. */
8877 if ((input_bfd->flags & DYNAMIC) != 0)
8878 return TRUE;
8879
c152c796
AM
8880 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8881 if (elf_bad_symtab (input_bfd))
8882 {
8883 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8884 extsymoff = 0;
8885 }
8886 else
8887 {
8888 locsymcount = symtab_hdr->sh_info;
8889 extsymoff = symtab_hdr->sh_info;
8890 }
8891
8892 /* Read the local symbols. */
8893 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8894 if (isymbuf == NULL && locsymcount != 0)
8895 {
8896 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8897 finfo->internal_syms,
8898 finfo->external_syms,
8899 finfo->locsym_shndx);
8900 if (isymbuf == NULL)
8901 return FALSE;
8902 }
8903
8904 /* Find local symbol sections and adjust values of symbols in
8905 SEC_MERGE sections. Write out those local symbols we know are
8906 going into the output file. */
8907 isymend = isymbuf + locsymcount;
8908 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8909 isym < isymend;
8910 isym++, pindex++, ppsection++)
8911 {
8912 asection *isec;
8913 const char *name;
8914 Elf_Internal_Sym osym;
8915
8916 *pindex = -1;
8917
8918 if (elf_bad_symtab (input_bfd))
8919 {
8920 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8921 {
8922 *ppsection = NULL;
8923 continue;
8924 }
8925 }
8926
8927 if (isym->st_shndx == SHN_UNDEF)
8928 isec = bfd_und_section_ptr;
c152c796
AM
8929 else if (isym->st_shndx == SHN_ABS)
8930 isec = bfd_abs_section_ptr;
8931 else if (isym->st_shndx == SHN_COMMON)
8932 isec = bfd_com_section_ptr;
8933 else
8934 {
cb33740c
AM
8935 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8936 if (isec == NULL)
8937 {
8938 /* Don't attempt to output symbols with st_shnx in the
8939 reserved range other than SHN_ABS and SHN_COMMON. */
8940 *ppsection = NULL;
8941 continue;
8942 }
8943 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8944 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8945 isym->st_value =
8946 _bfd_merged_section_offset (output_bfd, &isec,
8947 elf_section_data (isec)->sec_info,
8948 isym->st_value);
c152c796
AM
8949 }
8950
8951 *ppsection = isec;
8952
8953 /* Don't output the first, undefined, symbol. */
8954 if (ppsection == finfo->sections)
8955 continue;
8956
8957 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8958 {
8959 /* We never output section symbols. Instead, we use the
8960 section symbol of the corresponding section in the output
8961 file. */
8962 continue;
8963 }
8964
8965 /* If we are stripping all symbols, we don't want to output this
8966 one. */
8967 if (finfo->info->strip == strip_all)
8968 continue;
8969
8970 /* If we are discarding all local symbols, we don't want to
8971 output this one. If we are generating a relocatable output
8972 file, then some of the local symbols may be required by
8973 relocs; we output them below as we discover that they are
8974 needed. */
8975 if (finfo->info->discard == discard_all)
8976 continue;
8977
8978 /* If this symbol is defined in a section which we are
f02571c5
AM
8979 discarding, we don't need to keep it. */
8980 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
8981 && isym->st_shndx < SHN_LORESERVE
8982 && bfd_section_removed_from_list (output_bfd,
8983 isec->output_section))
e75a280b
L
8984 continue;
8985
c152c796
AM
8986 /* Get the name of the symbol. */
8987 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
8988 isym->st_name);
8989 if (name == NULL)
8990 return FALSE;
8991
8992 /* See if we are discarding symbols with this name. */
8993 if ((finfo->info->strip == strip_some
8994 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
8995 == NULL))
8996 || (((finfo->info->discard == discard_sec_merge
8997 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
8998 || finfo->info->discard == discard_l)
8999 && bfd_is_local_label_name (input_bfd, name)))
9000 continue;
9001
9002 /* If we get here, we are going to output this symbol. */
9003
9004 osym = *isym;
9005
9006 /* Adjust the section index for the output file. */
9007 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9008 isec->output_section);
9009 if (osym.st_shndx == SHN_BAD)
9010 return FALSE;
9011
9012 *pindex = bfd_get_symcount (output_bfd);
9013
9014 /* ELF symbols in relocatable files are section relative, but
9015 in executable files they are virtual addresses. Note that
9016 this code assumes that all ELF sections have an associated
9017 BFD section with a reasonable value for output_offset; below
9018 we assume that they also have a reasonable value for
9019 output_section. Any special sections must be set up to meet
9020 these requirements. */
9021 osym.st_value += isec->output_offset;
9022 if (! finfo->info->relocatable)
9023 {
9024 osym.st_value += isec->output_section->vma;
9025 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9026 {
9027 /* STT_TLS symbols are relative to PT_TLS segment base. */
9028 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9029 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9030 }
9031 }
9032
9033 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9034 return FALSE;
9035 }
9036
9037 /* Relocate the contents of each section. */
9038 sym_hashes = elf_sym_hashes (input_bfd);
9039 for (o = input_bfd->sections; o != NULL; o = o->next)
9040 {
9041 bfd_byte *contents;
9042
9043 if (! o->linker_mark)
9044 {
9045 /* This section was omitted from the link. */
9046 continue;
9047 }
9048
9049 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9050 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9051 continue;
9052
9053 if ((o->flags & SEC_LINKER_CREATED) != 0)
9054 {
9055 /* Section was created by _bfd_elf_link_create_dynamic_sections
9056 or somesuch. */
9057 continue;
9058 }
9059
9060 /* Get the contents of the section. They have been cached by a
9061 relaxation routine. Note that o is a section in an input
9062 file, so the contents field will not have been set by any of
9063 the routines which work on output files. */
9064 if (elf_section_data (o)->this_hdr.contents != NULL)
9065 contents = elf_section_data (o)->this_hdr.contents;
9066 else
9067 {
eea6121a
AM
9068 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9069
c152c796 9070 contents = finfo->contents;
eea6121a 9071 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
c152c796
AM
9072 return FALSE;
9073 }
9074
9075 if ((o->flags & SEC_RELOC) != 0)
9076 {
9077 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9078 Elf_Internal_Rela *rel, *relend;
c152c796
AM
9079 bfd_vma r_type_mask;
9080 int r_sym_shift;
0f02bbd9 9081 int action_discarded;
ece5ef60 9082 int ret;
c152c796
AM
9083
9084 /* Get the swapped relocs. */
9085 internal_relocs
9086 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9087 finfo->internal_relocs, FALSE);
9088 if (internal_relocs == NULL
9089 && o->reloc_count > 0)
9090 return FALSE;
9091
9092 if (bed->s->arch_size == 32)
9093 {
9094 r_type_mask = 0xff;
9095 r_sym_shift = 8;
9096 }
9097 else
9098 {
9099 r_type_mask = 0xffffffff;
9100 r_sym_shift = 32;
9101 }
9102
0f02bbd9 9103 action_discarded = -1;
c152c796 9104 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9105 action_discarded = (*bed->action_discarded) (o);
9106
9107 /* Run through the relocs evaluating complex reloc symbols and
9108 looking for relocs against symbols from discarded sections
9109 or section symbols from removed link-once sections.
9110 Complain about relocs against discarded sections. Zero
9111 relocs against removed link-once sections. */
9112
9113 rel = internal_relocs;
9114 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9115 for ( ; rel < relend; rel++)
c152c796 9116 {
0f02bbd9
AM
9117 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9118 unsigned int s_type;
9119 asection **ps, *sec;
9120 struct elf_link_hash_entry *h = NULL;
9121 const char *sym_name;
c152c796 9122
0f02bbd9
AM
9123 if (r_symndx == STN_UNDEF)
9124 continue;
c152c796 9125
0f02bbd9
AM
9126 if (r_symndx >= locsymcount
9127 || (elf_bad_symtab (input_bfd)
9128 && finfo->sections[r_symndx] == NULL))
9129 {
9130 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9131
0f02bbd9
AM
9132 /* Badly formatted input files can contain relocs that
9133 reference non-existant symbols. Check here so that
9134 we do not seg fault. */
9135 if (h == NULL)
c152c796 9136 {
0f02bbd9 9137 char buffer [32];
dce669a1 9138
0f02bbd9
AM
9139 sprintf_vma (buffer, rel->r_info);
9140 (*_bfd_error_handler)
9141 (_("error: %B contains a reloc (0x%s) for section %A "
9142 "that references a non-existent global symbol"),
9143 input_bfd, o, buffer);
9144 bfd_set_error (bfd_error_bad_value);
9145 return FALSE;
9146 }
3b36f7e6 9147
0f02bbd9
AM
9148 while (h->root.type == bfd_link_hash_indirect
9149 || h->root.type == bfd_link_hash_warning)
9150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9151
0f02bbd9 9152 s_type = h->type;
cdd3575c 9153
0f02bbd9
AM
9154 ps = NULL;
9155 if (h->root.type == bfd_link_hash_defined
9156 || h->root.type == bfd_link_hash_defweak)
9157 ps = &h->root.u.def.section;
9158
9159 sym_name = h->root.root.string;
9160 }
9161 else
9162 {
9163 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9164
9165 s_type = ELF_ST_TYPE (sym->st_info);
9166 ps = &finfo->sections[r_symndx];
9167 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9168 sym, *ps);
9169 }
c152c796 9170
0f02bbd9
AM
9171 if (s_type == STT_RELC || s_type == STT_SRELC)
9172 {
9173 bfd_vma val;
9174 bfd_vma dot = (rel->r_offset
9175 + o->output_offset + o->output_section->vma);
9176#ifdef DEBUG
9177 printf ("Encountered a complex symbol!");
9178 printf (" (input_bfd %s, section %s, reloc %ld\n",
9179 input_bfd->filename, o->name, rel - internal_relocs);
9180 printf (" symbol: idx %8.8lx, name %s\n",
9181 r_symndx, sym_name);
9182 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9183 (unsigned long) rel->r_info,
9184 (unsigned long) rel->r_offset);
9185#endif
9186 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9187 isymbuf, locsymcount, s_type == STT_SRELC))
9188 return FALSE;
9189
9190 /* Symbol evaluated OK. Update to absolute value. */
9191 set_symbol_value (input_bfd, isymbuf, locsymcount,
9192 r_symndx, val);
9193 continue;
9194 }
9195
9196 if (action_discarded != -1 && ps != NULL)
9197 {
cdd3575c
AM
9198 /* Complain if the definition comes from a
9199 discarded section. */
9200 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9201 {
87e5235d 9202 BFD_ASSERT (r_symndx != 0);
0f02bbd9 9203 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9204 (*finfo->info->callbacks->einfo)
9205 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9206 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9207 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9208
87e5235d 9209 /* Try to do the best we can to support buggy old
e0ae6d6f 9210 versions of gcc. Pretend that the symbol is
87e5235d
AM
9211 really defined in the kept linkonce section.
9212 FIXME: This is quite broken. Modifying the
9213 symbol here means we will be changing all later
e0ae6d6f 9214 uses of the symbol, not just in this section. */
0f02bbd9 9215 if (action_discarded & PRETEND)
87e5235d 9216 {
01b3c8ab
L
9217 asection *kept;
9218
c0f00686
L
9219 kept = _bfd_elf_check_kept_section (sec,
9220 finfo->info);
01b3c8ab 9221 if (kept != NULL)
87e5235d
AM
9222 {
9223 *ps = kept;
9224 continue;
9225 }
9226 }
c152c796
AM
9227 }
9228 }
9229 }
9230
9231 /* Relocate the section by invoking a back end routine.
9232
9233 The back end routine is responsible for adjusting the
9234 section contents as necessary, and (if using Rela relocs
9235 and generating a relocatable output file) adjusting the
9236 reloc addend as necessary.
9237
9238 The back end routine does not have to worry about setting
9239 the reloc address or the reloc symbol index.
9240
9241 The back end routine is given a pointer to the swapped in
9242 internal symbols, and can access the hash table entries
9243 for the external symbols via elf_sym_hashes (input_bfd).
9244
9245 When generating relocatable output, the back end routine
9246 must handle STB_LOCAL/STT_SECTION symbols specially. The
9247 output symbol is going to be a section symbol
9248 corresponding to the output section, which will require
9249 the addend to be adjusted. */
9250
ece5ef60 9251 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9252 input_bfd, o, contents,
9253 internal_relocs,
9254 isymbuf,
ece5ef60
AM
9255 finfo->sections);
9256 if (!ret)
c152c796
AM
9257 return FALSE;
9258
ece5ef60
AM
9259 if (ret == 2
9260 || finfo->info->relocatable
9261 || finfo->info->emitrelocations)
c152c796
AM
9262 {
9263 Elf_Internal_Rela *irela;
9264 Elf_Internal_Rela *irelaend;
9265 bfd_vma last_offset;
9266 struct elf_link_hash_entry **rel_hash;
eac338cf 9267 struct elf_link_hash_entry **rel_hash_list;
c152c796
AM
9268 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9269 unsigned int next_erel;
c152c796
AM
9270 bfd_boolean rela_normal;
9271
9272 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9273 rela_normal = (bed->rela_normal
9274 && (input_rel_hdr->sh_entsize
9275 == bed->s->sizeof_rela));
9276
9277 /* Adjust the reloc addresses and symbol indices. */
9278
9279 irela = internal_relocs;
9280 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9281 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9282 + elf_section_data (o->output_section)->rel_count
9283 + elf_section_data (o->output_section)->rel_count2);
eac338cf 9284 rel_hash_list = rel_hash;
c152c796
AM
9285 last_offset = o->output_offset;
9286 if (!finfo->info->relocatable)
9287 last_offset += o->output_section->vma;
9288 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9289 {
9290 unsigned long r_symndx;
9291 asection *sec;
9292 Elf_Internal_Sym sym;
9293
9294 if (next_erel == bed->s->int_rels_per_ext_rel)
9295 {
9296 rel_hash++;
9297 next_erel = 0;
9298 }
9299
9300 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9301 finfo->info, o,
9302 irela->r_offset);
9303 if (irela->r_offset >= (bfd_vma) -2)
9304 {
9305 /* This is a reloc for a deleted entry or somesuch.
9306 Turn it into an R_*_NONE reloc, at the same
9307 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9308 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9309 being ordered. */
9310 irela->r_offset = last_offset;
9311 irela->r_info = 0;
9312 irela->r_addend = 0;
9313 continue;
9314 }
9315
9316 irela->r_offset += o->output_offset;
9317
9318 /* Relocs in an executable have to be virtual addresses. */
9319 if (!finfo->info->relocatable)
9320 irela->r_offset += o->output_section->vma;
9321
9322 last_offset = irela->r_offset;
9323
9324 r_symndx = irela->r_info >> r_sym_shift;
9325 if (r_symndx == STN_UNDEF)
9326 continue;
9327
9328 if (r_symndx >= locsymcount
9329 || (elf_bad_symtab (input_bfd)
9330 && finfo->sections[r_symndx] == NULL))
9331 {
9332 struct elf_link_hash_entry *rh;
9333 unsigned long indx;
9334
9335 /* This is a reloc against a global symbol. We
9336 have not yet output all the local symbols, so
9337 we do not know the symbol index of any global
9338 symbol. We set the rel_hash entry for this
9339 reloc to point to the global hash table entry
9340 for this symbol. The symbol index is then
ee75fd95 9341 set at the end of bfd_elf_final_link. */
c152c796
AM
9342 indx = r_symndx - extsymoff;
9343 rh = elf_sym_hashes (input_bfd)[indx];
9344 while (rh->root.type == bfd_link_hash_indirect
9345 || rh->root.type == bfd_link_hash_warning)
9346 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9347
9348 /* Setting the index to -2 tells
9349 elf_link_output_extsym that this symbol is
9350 used by a reloc. */
9351 BFD_ASSERT (rh->indx < 0);
9352 rh->indx = -2;
9353
9354 *rel_hash = rh;
9355
9356 continue;
9357 }
9358
9359 /* This is a reloc against a local symbol. */
9360
9361 *rel_hash = NULL;
9362 sym = isymbuf[r_symndx];
9363 sec = finfo->sections[r_symndx];
9364 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9365 {
9366 /* I suppose the backend ought to fill in the
9367 section of any STT_SECTION symbol against a
6a8d1586
AM
9368 processor specific section. */
9369 r_symndx = 0;
9370 if (bfd_is_abs_section (sec))
9371 ;
c152c796
AM
9372 else if (sec == NULL || sec->owner == NULL)
9373 {
9374 bfd_set_error (bfd_error_bad_value);
9375 return FALSE;
9376 }
9377 else
9378 {
6a8d1586
AM
9379 asection *osec = sec->output_section;
9380
9381 /* If we have discarded a section, the output
9382 section will be the absolute section. In
ab96bf03
AM
9383 case of discarded SEC_MERGE sections, use
9384 the kept section. relocate_section should
9385 have already handled discarded linkonce
9386 sections. */
6a8d1586
AM
9387 if (bfd_is_abs_section (osec)
9388 && sec->kept_section != NULL
9389 && sec->kept_section->output_section != NULL)
9390 {
9391 osec = sec->kept_section->output_section;
9392 irela->r_addend -= osec->vma;
9393 }
9394
9395 if (!bfd_is_abs_section (osec))
9396 {
9397 r_symndx = osec->target_index;
74541ad4
AM
9398 if (r_symndx == 0)
9399 {
9400 struct elf_link_hash_table *htab;
9401 asection *oi;
9402
9403 htab = elf_hash_table (finfo->info);
9404 oi = htab->text_index_section;
9405 if ((osec->flags & SEC_READONLY) == 0
9406 && htab->data_index_section != NULL)
9407 oi = htab->data_index_section;
9408
9409 if (oi != NULL)
9410 {
9411 irela->r_addend += osec->vma - oi->vma;
9412 r_symndx = oi->target_index;
9413 }
9414 }
9415
6a8d1586
AM
9416 BFD_ASSERT (r_symndx != 0);
9417 }
c152c796
AM
9418 }
9419
9420 /* Adjust the addend according to where the
9421 section winds up in the output section. */
9422 if (rela_normal)
9423 irela->r_addend += sec->output_offset;
9424 }
9425 else
9426 {
9427 if (finfo->indices[r_symndx] == -1)
9428 {
9429 unsigned long shlink;
9430 const char *name;
9431 asection *osec;
9432
9433 if (finfo->info->strip == strip_all)
9434 {
9435 /* You can't do ld -r -s. */
9436 bfd_set_error (bfd_error_invalid_operation);
9437 return FALSE;
9438 }
9439
9440 /* This symbol was skipped earlier, but
9441 since it is needed by a reloc, we
9442 must output it now. */
9443 shlink = symtab_hdr->sh_link;
9444 name = (bfd_elf_string_from_elf_section
9445 (input_bfd, shlink, sym.st_name));
9446 if (name == NULL)
9447 return FALSE;
9448
9449 osec = sec->output_section;
9450 sym.st_shndx =
9451 _bfd_elf_section_from_bfd_section (output_bfd,
9452 osec);
9453 if (sym.st_shndx == SHN_BAD)
9454 return FALSE;
9455
9456 sym.st_value += sec->output_offset;
9457 if (! finfo->info->relocatable)
9458 {
9459 sym.st_value += osec->vma;
9460 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9461 {
9462 /* STT_TLS symbols are relative to PT_TLS
9463 segment base. */
9464 BFD_ASSERT (elf_hash_table (finfo->info)
9465 ->tls_sec != NULL);
9466 sym.st_value -= (elf_hash_table (finfo->info)
9467 ->tls_sec->vma);
9468 }
9469 }
9470
9471 finfo->indices[r_symndx]
9472 = bfd_get_symcount (output_bfd);
9473
9474 if (! elf_link_output_sym (finfo, name, &sym, sec,
9475 NULL))
9476 return FALSE;
9477 }
9478
9479 r_symndx = finfo->indices[r_symndx];
9480 }
9481
9482 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9483 | (irela->r_info & r_type_mask));
9484 }
9485
9486 /* Swap out the relocs. */
c152c796 9487 if (input_rel_hdr->sh_size != 0
eac338cf
PB
9488 && !bed->elf_backend_emit_relocs (output_bfd, o,
9489 input_rel_hdr,
9490 internal_relocs,
9491 rel_hash_list))
c152c796
AM
9492 return FALSE;
9493
9494 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9495 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9496 {
9497 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9498 * bed->s->int_rels_per_ext_rel);
eac338cf
PB
9499 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9500 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9501 input_rel_hdr2,
9502 internal_relocs,
9503 rel_hash_list))
c152c796
AM
9504 return FALSE;
9505 }
9506 }
9507 }
9508
9509 /* Write out the modified section contents. */
9510 if (bed->elf_backend_write_section
c7b8f16e
JB
9511 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9512 contents))
c152c796
AM
9513 {
9514 /* Section written out. */
9515 }
9516 else switch (o->sec_info_type)
9517 {
9518 case ELF_INFO_TYPE_STABS:
9519 if (! (_bfd_write_section_stabs
9520 (output_bfd,
9521 &elf_hash_table (finfo->info)->stab_info,
9522 o, &elf_section_data (o)->sec_info, contents)))
9523 return FALSE;
9524 break;
9525 case ELF_INFO_TYPE_MERGE:
9526 if (! _bfd_write_merged_section (output_bfd, o,
9527 elf_section_data (o)->sec_info))
9528 return FALSE;
9529 break;
9530 case ELF_INFO_TYPE_EH_FRAME:
9531 {
9532 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9533 o, contents))
9534 return FALSE;
9535 }
9536 break;
9537 default:
9538 {
c152c796 9539 if (! (o->flags & SEC_EXCLUDE)
ace79388 9540 && ! (o->output_section->flags & SEC_NEVER_LOAD)
c152c796
AM
9541 && ! bfd_set_section_contents (output_bfd, o->output_section,
9542 contents,
9543 (file_ptr) o->output_offset,
eea6121a 9544 o->size))
c152c796
AM
9545 return FALSE;
9546 }
9547 break;
9548 }
9549 }
9550
9551 return TRUE;
9552}
9553
9554/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9555 requested by the linker, and does not come from any input file. This
c152c796
AM
9556 is used to build constructor and destructor tables when linking
9557 with -Ur. */
9558
9559static bfd_boolean
9560elf_reloc_link_order (bfd *output_bfd,
9561 struct bfd_link_info *info,
9562 asection *output_section,
9563 struct bfd_link_order *link_order)
9564{
9565 reloc_howto_type *howto;
9566 long indx;
9567 bfd_vma offset;
9568 bfd_vma addend;
9569 struct elf_link_hash_entry **rel_hash_ptr;
9570 Elf_Internal_Shdr *rel_hdr;
9571 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9572 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9573 bfd_byte *erel;
9574 unsigned int i;
9575
9576 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9577 if (howto == NULL)
9578 {
9579 bfd_set_error (bfd_error_bad_value);
9580 return FALSE;
9581 }
9582
9583 addend = link_order->u.reloc.p->addend;
9584
9585 /* Figure out the symbol index. */
9586 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9587 + elf_section_data (output_section)->rel_count
9588 + elf_section_data (output_section)->rel_count2);
9589 if (link_order->type == bfd_section_reloc_link_order)
9590 {
9591 indx = link_order->u.reloc.p->u.section->target_index;
9592 BFD_ASSERT (indx != 0);
9593 *rel_hash_ptr = NULL;
9594 }
9595 else
9596 {
9597 struct elf_link_hash_entry *h;
9598
9599 /* Treat a reloc against a defined symbol as though it were
9600 actually against the section. */
9601 h = ((struct elf_link_hash_entry *)
9602 bfd_wrapped_link_hash_lookup (output_bfd, info,
9603 link_order->u.reloc.p->u.name,
9604 FALSE, FALSE, TRUE));
9605 if (h != NULL
9606 && (h->root.type == bfd_link_hash_defined
9607 || h->root.type == bfd_link_hash_defweak))
9608 {
9609 asection *section;
9610
9611 section = h->root.u.def.section;
9612 indx = section->output_section->target_index;
9613 *rel_hash_ptr = NULL;
9614 /* It seems that we ought to add the symbol value to the
9615 addend here, but in practice it has already been added
9616 because it was passed to constructor_callback. */
9617 addend += section->output_section->vma + section->output_offset;
9618 }
9619 else if (h != NULL)
9620 {
9621 /* Setting the index to -2 tells elf_link_output_extsym that
9622 this symbol is used by a reloc. */
9623 h->indx = -2;
9624 *rel_hash_ptr = h;
9625 indx = 0;
9626 }
9627 else
9628 {
9629 if (! ((*info->callbacks->unattached_reloc)
9630 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9631 return FALSE;
9632 indx = 0;
9633 }
9634 }
9635
9636 /* If this is an inplace reloc, we must write the addend into the
9637 object file. */
9638 if (howto->partial_inplace && addend != 0)
9639 {
9640 bfd_size_type size;
9641 bfd_reloc_status_type rstat;
9642 bfd_byte *buf;
9643 bfd_boolean ok;
9644 const char *sym_name;
9645
9646 size = bfd_get_reloc_size (howto);
9647 buf = bfd_zmalloc (size);
9648 if (buf == NULL)
9649 return FALSE;
9650 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9651 switch (rstat)
9652 {
9653 case bfd_reloc_ok:
9654 break;
9655
9656 default:
9657 case bfd_reloc_outofrange:
9658 abort ();
9659
9660 case bfd_reloc_overflow:
9661 if (link_order->type == bfd_section_reloc_link_order)
9662 sym_name = bfd_section_name (output_bfd,
9663 link_order->u.reloc.p->u.section);
9664 else
9665 sym_name = link_order->u.reloc.p->u.name;
9666 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
9667 (info, NULL, sym_name, howto->name, addend, NULL,
9668 NULL, (bfd_vma) 0)))
c152c796
AM
9669 {
9670 free (buf);
9671 return FALSE;
9672 }
9673 break;
9674 }
9675 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9676 link_order->offset, size);
9677 free (buf);
9678 if (! ok)
9679 return FALSE;
9680 }
9681
9682 /* The address of a reloc is relative to the section in a
9683 relocatable file, and is a virtual address in an executable
9684 file. */
9685 offset = link_order->offset;
9686 if (! info->relocatable)
9687 offset += output_section->vma;
9688
9689 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9690 {
9691 irel[i].r_offset = offset;
9692 irel[i].r_info = 0;
9693 irel[i].r_addend = 0;
9694 }
9695 if (bed->s->arch_size == 32)
9696 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9697 else
9698 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9699
9700 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9701 erel = rel_hdr->contents;
9702 if (rel_hdr->sh_type == SHT_REL)
9703 {
9704 erel += (elf_section_data (output_section)->rel_count
9705 * bed->s->sizeof_rel);
9706 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9707 }
9708 else
9709 {
9710 irel[0].r_addend = addend;
9711 erel += (elf_section_data (output_section)->rel_count
9712 * bed->s->sizeof_rela);
9713 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9714 }
9715
9716 ++elf_section_data (output_section)->rel_count;
9717
9718 return TRUE;
9719}
9720
0b52efa6
PB
9721
9722/* Get the output vma of the section pointed to by the sh_link field. */
9723
9724static bfd_vma
9725elf_get_linked_section_vma (struct bfd_link_order *p)
9726{
9727 Elf_Internal_Shdr **elf_shdrp;
9728 asection *s;
9729 int elfsec;
9730
9731 s = p->u.indirect.section;
9732 elf_shdrp = elf_elfsections (s->owner);
9733 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9734 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
9735 /* PR 290:
9736 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 9737 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
9738 sh_info fields. Hence we could get the situation
9739 where elfsec is 0. */
9740 if (elfsec == 0)
9741 {
9742 const struct elf_backend_data *bed
9743 = get_elf_backend_data (s->owner);
9744 if (bed->link_order_error_handler)
d003868e
AM
9745 bed->link_order_error_handler
9746 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
9747 return 0;
9748 }
9749 else
9750 {
9751 s = elf_shdrp[elfsec]->bfd_section;
9752 return s->output_section->vma + s->output_offset;
9753 }
0b52efa6
PB
9754}
9755
9756
9757/* Compare two sections based on the locations of the sections they are
9758 linked to. Used by elf_fixup_link_order. */
9759
9760static int
9761compare_link_order (const void * a, const void * b)
9762{
9763 bfd_vma apos;
9764 bfd_vma bpos;
9765
9766 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9767 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9768 if (apos < bpos)
9769 return -1;
9770 return apos > bpos;
9771}
9772
9773
9774/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9775 order as their linked sections. Returns false if this could not be done
9776 because an output section includes both ordered and unordered
9777 sections. Ideally we'd do this in the linker proper. */
9778
9779static bfd_boolean
9780elf_fixup_link_order (bfd *abfd, asection *o)
9781{
9782 int seen_linkorder;
9783 int seen_other;
9784 int n;
9785 struct bfd_link_order *p;
9786 bfd *sub;
9787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 9788 unsigned elfsec;
0b52efa6 9789 struct bfd_link_order **sections;
d33cdfe3 9790 asection *s, *other_sec, *linkorder_sec;
0b52efa6 9791 bfd_vma offset;
3b36f7e6 9792
d33cdfe3
L
9793 other_sec = NULL;
9794 linkorder_sec = NULL;
0b52efa6
PB
9795 seen_other = 0;
9796 seen_linkorder = 0;
8423293d 9797 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 9798 {
d33cdfe3 9799 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
9800 {
9801 s = p->u.indirect.section;
d33cdfe3
L
9802 sub = s->owner;
9803 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9804 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
9805 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9806 && elfsec < elf_numsections (sub)
4fbb74a6
AM
9807 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9808 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
9809 {
9810 seen_linkorder++;
9811 linkorder_sec = s;
9812 }
0b52efa6 9813 else
d33cdfe3
L
9814 {
9815 seen_other++;
9816 other_sec = s;
9817 }
0b52efa6
PB
9818 }
9819 else
9820 seen_other++;
d33cdfe3
L
9821
9822 if (seen_other && seen_linkorder)
9823 {
9824 if (other_sec && linkorder_sec)
9825 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9826 o, linkorder_sec,
9827 linkorder_sec->owner, other_sec,
9828 other_sec->owner);
9829 else
9830 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9831 o);
9832 bfd_set_error (bfd_error_bad_value);
9833 return FALSE;
9834 }
0b52efa6
PB
9835 }
9836
9837 if (!seen_linkorder)
9838 return TRUE;
9839
0b52efa6 9840 sections = (struct bfd_link_order **)
14b1c01e
AM
9841 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9842 if (sections == NULL)
9843 return FALSE;
0b52efa6 9844 seen_linkorder = 0;
3b36f7e6 9845
8423293d 9846 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
9847 {
9848 sections[seen_linkorder++] = p;
9849 }
9850 /* Sort the input sections in the order of their linked section. */
9851 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9852 compare_link_order);
9853
9854 /* Change the offsets of the sections. */
9855 offset = 0;
9856 for (n = 0; n < seen_linkorder; n++)
9857 {
9858 s = sections[n]->u.indirect.section;
461686a3 9859 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
9860 s->output_offset = offset;
9861 sections[n]->offset = offset;
9862 offset += sections[n]->size;
9863 }
9864
4dd07732 9865 free (sections);
0b52efa6
PB
9866 return TRUE;
9867}
9868
9869
c152c796
AM
9870/* Do the final step of an ELF link. */
9871
9872bfd_boolean
9873bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9874{
9875 bfd_boolean dynamic;
9876 bfd_boolean emit_relocs;
9877 bfd *dynobj;
9878 struct elf_final_link_info finfo;
9879 register asection *o;
9880 register struct bfd_link_order *p;
9881 register bfd *sub;
9882 bfd_size_type max_contents_size;
9883 bfd_size_type max_external_reloc_size;
9884 bfd_size_type max_internal_reloc_count;
9885 bfd_size_type max_sym_count;
9886 bfd_size_type max_sym_shndx_count;
9887 file_ptr off;
9888 Elf_Internal_Sym elfsym;
9889 unsigned int i;
9890 Elf_Internal_Shdr *symtab_hdr;
9891 Elf_Internal_Shdr *symtab_shndx_hdr;
9892 Elf_Internal_Shdr *symstrtab_hdr;
9893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9894 struct elf_outext_info eoinfo;
9895 bfd_boolean merged;
9896 size_t relativecount = 0;
9897 asection *reldyn = 0;
9898 bfd_size_type amt;
104d59d1
JM
9899 asection *attr_section = NULL;
9900 bfd_vma attr_size = 0;
9901 const char *std_attrs_section;
c152c796
AM
9902
9903 if (! is_elf_hash_table (info->hash))
9904 return FALSE;
9905
9906 if (info->shared)
9907 abfd->flags |= DYNAMIC;
9908
9909 dynamic = elf_hash_table (info)->dynamic_sections_created;
9910 dynobj = elf_hash_table (info)->dynobj;
9911
9912 emit_relocs = (info->relocatable
a4676736 9913 || info->emitrelocations);
c152c796
AM
9914
9915 finfo.info = info;
9916 finfo.output_bfd = abfd;
9917 finfo.symstrtab = _bfd_elf_stringtab_init ();
9918 if (finfo.symstrtab == NULL)
9919 return FALSE;
9920
9921 if (! dynamic)
9922 {
9923 finfo.dynsym_sec = NULL;
9924 finfo.hash_sec = NULL;
9925 finfo.symver_sec = NULL;
9926 }
9927 else
9928 {
9929 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
9930 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 9931 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
9932 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
9933 /* Note that it is OK if symver_sec is NULL. */
9934 }
9935
9936 finfo.contents = NULL;
9937 finfo.external_relocs = NULL;
9938 finfo.internal_relocs = NULL;
9939 finfo.external_syms = NULL;
9940 finfo.locsym_shndx = NULL;
9941 finfo.internal_syms = NULL;
9942 finfo.indices = NULL;
9943 finfo.sections = NULL;
9944 finfo.symbuf = NULL;
9945 finfo.symshndxbuf = NULL;
9946 finfo.symbuf_count = 0;
9947 finfo.shndxbuf_size = 0;
9948
104d59d1
JM
9949 /* The object attributes have been merged. Remove the input
9950 sections from the link, and set the contents of the output
9951 secton. */
9952 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
9953 for (o = abfd->sections; o != NULL; o = o->next)
9954 {
9955 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
9956 || strcmp (o->name, ".gnu.attributes") == 0)
9957 {
9958 for (p = o->map_head.link_order; p != NULL; p = p->next)
9959 {
9960 asection *input_section;
9961
9962 if (p->type != bfd_indirect_link_order)
9963 continue;
9964 input_section = p->u.indirect.section;
9965 /* Hack: reset the SEC_HAS_CONTENTS flag so that
9966 elf_link_input_bfd ignores this section. */
9967 input_section->flags &= ~SEC_HAS_CONTENTS;
9968 }
a0c8462f 9969
104d59d1
JM
9970 attr_size = bfd_elf_obj_attr_size (abfd);
9971 if (attr_size)
9972 {
9973 bfd_set_section_size (abfd, o, attr_size);
9974 attr_section = o;
9975 /* Skip this section later on. */
9976 o->map_head.link_order = NULL;
9977 }
9978 else
9979 o->flags |= SEC_EXCLUDE;
9980 }
9981 }
9982
c152c796
AM
9983 /* Count up the number of relocations we will output for each output
9984 section, so that we know the sizes of the reloc sections. We
9985 also figure out some maximum sizes. */
9986 max_contents_size = 0;
9987 max_external_reloc_size = 0;
9988 max_internal_reloc_count = 0;
9989 max_sym_count = 0;
9990 max_sym_shndx_count = 0;
9991 merged = FALSE;
9992 for (o = abfd->sections; o != NULL; o = o->next)
9993 {
9994 struct bfd_elf_section_data *esdo = elf_section_data (o);
9995 o->reloc_count = 0;
9996
8423293d 9997 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
9998 {
9999 unsigned int reloc_count = 0;
10000 struct bfd_elf_section_data *esdi = NULL;
10001 unsigned int *rel_count1;
10002
10003 if (p->type == bfd_section_reloc_link_order
10004 || p->type == bfd_symbol_reloc_link_order)
10005 reloc_count = 1;
10006 else if (p->type == bfd_indirect_link_order)
10007 {
10008 asection *sec;
10009
10010 sec = p->u.indirect.section;
10011 esdi = elf_section_data (sec);
10012
10013 /* Mark all sections which are to be included in the
10014 link. This will normally be every section. We need
10015 to do this so that we can identify any sections which
10016 the linker has decided to not include. */
10017 sec->linker_mark = TRUE;
10018
10019 if (sec->flags & SEC_MERGE)
10020 merged = TRUE;
10021
10022 if (info->relocatable || info->emitrelocations)
10023 reloc_count = sec->reloc_count;
10024 else if (bed->elf_backend_count_relocs)
10025 {
10026 Elf_Internal_Rela * relocs;
10027
1182cb93
AM
10028 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
10029 NULL, NULL,
c152c796
AM
10030 info->keep_memory);
10031
1182cb93
AM
10032 if (relocs != NULL)
10033 {
10034 reloc_count
10035 = (*bed->elf_backend_count_relocs) (sec, relocs);
c152c796 10036
1182cb93
AM
10037 if (elf_section_data (sec)->relocs != relocs)
10038 free (relocs);
10039 }
c152c796
AM
10040 }
10041
eea6121a
AM
10042 if (sec->rawsize > max_contents_size)
10043 max_contents_size = sec->rawsize;
10044 if (sec->size > max_contents_size)
10045 max_contents_size = sec->size;
c152c796
AM
10046
10047 /* We are interested in just local symbols, not all
10048 symbols. */
10049 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10050 && (sec->owner->flags & DYNAMIC) == 0)
10051 {
10052 size_t sym_count;
10053
10054 if (elf_bad_symtab (sec->owner))
10055 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10056 / bed->s->sizeof_sym);
10057 else
10058 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10059
10060 if (sym_count > max_sym_count)
10061 max_sym_count = sym_count;
10062
10063 if (sym_count > max_sym_shndx_count
10064 && elf_symtab_shndx (sec->owner) != 0)
10065 max_sym_shndx_count = sym_count;
10066
10067 if ((sec->flags & SEC_RELOC) != 0)
10068 {
10069 size_t ext_size;
10070
10071 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10072 if (ext_size > max_external_reloc_size)
10073 max_external_reloc_size = ext_size;
10074 if (sec->reloc_count > max_internal_reloc_count)
10075 max_internal_reloc_count = sec->reloc_count;
10076 }
10077 }
10078 }
10079
10080 if (reloc_count == 0)
10081 continue;
10082
10083 o->reloc_count += reloc_count;
10084
10085 /* MIPS may have a mix of REL and RELA relocs on sections.
10086 To support this curious ABI we keep reloc counts in
10087 elf_section_data too. We must be careful to add the
10088 relocations from the input section to the right output
10089 count. FIXME: Get rid of one count. We have
10090 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10091 rel_count1 = &esdo->rel_count;
10092 if (esdi != NULL)
10093 {
10094 bfd_boolean same_size;
10095 bfd_size_type entsize1;
10096
10097 entsize1 = esdi->rel_hdr.sh_entsize;
10098 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10099 || entsize1 == bed->s->sizeof_rela);
10100 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10101
10102 if (!same_size)
10103 rel_count1 = &esdo->rel_count2;
10104
10105 if (esdi->rel_hdr2 != NULL)
10106 {
10107 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10108 unsigned int alt_count;
10109 unsigned int *rel_count2;
10110
10111 BFD_ASSERT (entsize2 != entsize1
10112 && (entsize2 == bed->s->sizeof_rel
10113 || entsize2 == bed->s->sizeof_rela));
10114
10115 rel_count2 = &esdo->rel_count2;
10116 if (!same_size)
10117 rel_count2 = &esdo->rel_count;
10118
10119 /* The following is probably too simplistic if the
10120 backend counts output relocs unusually. */
10121 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10122 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10123 *rel_count2 += alt_count;
10124 reloc_count -= alt_count;
10125 }
10126 }
10127 *rel_count1 += reloc_count;
10128 }
10129
10130 if (o->reloc_count > 0)
10131 o->flags |= SEC_RELOC;
10132 else
10133 {
10134 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10135 set it (this is probably a bug) and if it is set
10136 assign_section_numbers will create a reloc section. */
10137 o->flags &=~ SEC_RELOC;
10138 }
10139
10140 /* If the SEC_ALLOC flag is not set, force the section VMA to
10141 zero. This is done in elf_fake_sections as well, but forcing
10142 the VMA to 0 here will ensure that relocs against these
10143 sections are handled correctly. */
10144 if ((o->flags & SEC_ALLOC) == 0
10145 && ! o->user_set_vma)
10146 o->vma = 0;
10147 }
10148
10149 if (! info->relocatable && merged)
10150 elf_link_hash_traverse (elf_hash_table (info),
10151 _bfd_elf_link_sec_merge_syms, abfd);
10152
10153 /* Figure out the file positions for everything but the symbol table
10154 and the relocs. We set symcount to force assign_section_numbers
10155 to create a symbol table. */
10156 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10157 BFD_ASSERT (! abfd->output_has_begun);
10158 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10159 goto error_return;
10160
ee75fd95 10161 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10162 for (o = abfd->sections; o != NULL; o = o->next)
10163 {
10164 if ((o->flags & SEC_RELOC) != 0)
10165 {
10166 if (!(_bfd_elf_link_size_reloc_section
10167 (abfd, &elf_section_data (o)->rel_hdr, o)))
10168 goto error_return;
10169
10170 if (elf_section_data (o)->rel_hdr2
10171 && !(_bfd_elf_link_size_reloc_section
10172 (abfd, elf_section_data (o)->rel_hdr2, o)))
10173 goto error_return;
10174 }
10175
10176 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10177 to count upwards while actually outputting the relocations. */
10178 elf_section_data (o)->rel_count = 0;
10179 elf_section_data (o)->rel_count2 = 0;
10180 }
10181
10182 _bfd_elf_assign_file_positions_for_relocs (abfd);
10183
10184 /* We have now assigned file positions for all the sections except
10185 .symtab and .strtab. We start the .symtab section at the current
10186 file position, and write directly to it. We build the .strtab
10187 section in memory. */
10188 bfd_get_symcount (abfd) = 0;
10189 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10190 /* sh_name is set in prep_headers. */
10191 symtab_hdr->sh_type = SHT_SYMTAB;
10192 /* sh_flags, sh_addr and sh_size all start off zero. */
10193 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10194 /* sh_link is set in assign_section_numbers. */
10195 /* sh_info is set below. */
10196 /* sh_offset is set just below. */
72de5009 10197 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10198
10199 off = elf_tdata (abfd)->next_file_pos;
10200 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10201
10202 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10203 incorrect. We do not yet know the size of the .symtab section.
10204 We correct next_file_pos below, after we do know the size. */
10205
10206 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10207 continuously seeking to the right position in the file. */
10208 if (! info->keep_memory || max_sym_count < 20)
10209 finfo.symbuf_size = 20;
10210 else
10211 finfo.symbuf_size = max_sym_count;
10212 amt = finfo.symbuf_size;
10213 amt *= bed->s->sizeof_sym;
10214 finfo.symbuf = bfd_malloc (amt);
10215 if (finfo.symbuf == NULL)
10216 goto error_return;
4fbb74a6 10217 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10218 {
10219 /* Wild guess at number of output symbols. realloc'd as needed. */
10220 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10221 finfo.shndxbuf_size = amt;
10222 amt *= sizeof (Elf_External_Sym_Shndx);
10223 finfo.symshndxbuf = bfd_zmalloc (amt);
10224 if (finfo.symshndxbuf == NULL)
10225 goto error_return;
10226 }
10227
10228 /* Start writing out the symbol table. The first symbol is always a
10229 dummy symbol. */
10230 if (info->strip != strip_all
10231 || emit_relocs)
10232 {
10233 elfsym.st_value = 0;
10234 elfsym.st_size = 0;
10235 elfsym.st_info = 0;
10236 elfsym.st_other = 0;
10237 elfsym.st_shndx = SHN_UNDEF;
10238 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10239 NULL))
10240 goto error_return;
10241 }
10242
c152c796
AM
10243 /* Output a symbol for each section. We output these even if we are
10244 discarding local symbols, since they are used for relocs. These
10245 symbols have no names. We store the index of each one in the
10246 index field of the section, so that we can find it again when
10247 outputting relocs. */
10248 if (info->strip != strip_all
10249 || emit_relocs)
10250 {
10251 elfsym.st_size = 0;
10252 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10253 elfsym.st_other = 0;
f0b5bb34 10254 elfsym.st_value = 0;
c152c796
AM
10255 for (i = 1; i < elf_numsections (abfd); i++)
10256 {
10257 o = bfd_section_from_elf_index (abfd, i);
10258 if (o != NULL)
f0b5bb34
AM
10259 {
10260 o->target_index = bfd_get_symcount (abfd);
10261 elfsym.st_shndx = i;
10262 if (!info->relocatable)
10263 elfsym.st_value = o->vma;
10264 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10265 goto error_return;
10266 }
c152c796
AM
10267 }
10268 }
10269
10270 /* Allocate some memory to hold information read in from the input
10271 files. */
10272 if (max_contents_size != 0)
10273 {
10274 finfo.contents = bfd_malloc (max_contents_size);
10275 if (finfo.contents == NULL)
10276 goto error_return;
10277 }
10278
10279 if (max_external_reloc_size != 0)
10280 {
10281 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10282 if (finfo.external_relocs == NULL)
10283 goto error_return;
10284 }
10285
10286 if (max_internal_reloc_count != 0)
10287 {
10288 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10289 amt *= sizeof (Elf_Internal_Rela);
10290 finfo.internal_relocs = bfd_malloc (amt);
10291 if (finfo.internal_relocs == NULL)
10292 goto error_return;
10293 }
10294
10295 if (max_sym_count != 0)
10296 {
10297 amt = max_sym_count * bed->s->sizeof_sym;
10298 finfo.external_syms = bfd_malloc (amt);
10299 if (finfo.external_syms == NULL)
10300 goto error_return;
10301
10302 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10303 finfo.internal_syms = bfd_malloc (amt);
10304 if (finfo.internal_syms == NULL)
10305 goto error_return;
10306
10307 amt = max_sym_count * sizeof (long);
10308 finfo.indices = bfd_malloc (amt);
10309 if (finfo.indices == NULL)
10310 goto error_return;
10311
10312 amt = max_sym_count * sizeof (asection *);
10313 finfo.sections = bfd_malloc (amt);
10314 if (finfo.sections == NULL)
10315 goto error_return;
10316 }
10317
10318 if (max_sym_shndx_count != 0)
10319 {
10320 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10321 finfo.locsym_shndx = bfd_malloc (amt);
10322 if (finfo.locsym_shndx == NULL)
10323 goto error_return;
10324 }
10325
10326 if (elf_hash_table (info)->tls_sec)
10327 {
10328 bfd_vma base, end = 0;
10329 asection *sec;
10330
10331 for (sec = elf_hash_table (info)->tls_sec;
10332 sec && (sec->flags & SEC_THREAD_LOCAL);
10333 sec = sec->next)
10334 {
3a800eb9 10335 bfd_size_type size = sec->size;
c152c796 10336
3a800eb9
AM
10337 if (size == 0
10338 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10339 {
3a800eb9
AM
10340 struct bfd_link_order *o = sec->map_tail.link_order;
10341 if (o != NULL)
10342 size = o->offset + o->size;
c152c796
AM
10343 }
10344 end = sec->vma + size;
10345 }
10346 base = elf_hash_table (info)->tls_sec->vma;
10347 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10348 elf_hash_table (info)->tls_size = end - base;
10349 }
10350
0b52efa6
PB
10351 /* Reorder SHF_LINK_ORDER sections. */
10352 for (o = abfd->sections; o != NULL; o = o->next)
10353 {
10354 if (!elf_fixup_link_order (abfd, o))
10355 return FALSE;
10356 }
10357
c152c796
AM
10358 /* Since ELF permits relocations to be against local symbols, we
10359 must have the local symbols available when we do the relocations.
10360 Since we would rather only read the local symbols once, and we
10361 would rather not keep them in memory, we handle all the
10362 relocations for a single input file at the same time.
10363
10364 Unfortunately, there is no way to know the total number of local
10365 symbols until we have seen all of them, and the local symbol
10366 indices precede the global symbol indices. This means that when
10367 we are generating relocatable output, and we see a reloc against
10368 a global symbol, we can not know the symbol index until we have
10369 finished examining all the local symbols to see which ones we are
10370 going to output. To deal with this, we keep the relocations in
10371 memory, and don't output them until the end of the link. This is
10372 an unfortunate waste of memory, but I don't see a good way around
10373 it. Fortunately, it only happens when performing a relocatable
10374 link, which is not the common case. FIXME: If keep_memory is set
10375 we could write the relocs out and then read them again; I don't
10376 know how bad the memory loss will be. */
10377
10378 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10379 sub->output_has_begun = FALSE;
10380 for (o = abfd->sections; o != NULL; o = o->next)
10381 {
8423293d 10382 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10383 {
10384 if (p->type == bfd_indirect_link_order
10385 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10386 == bfd_target_elf_flavour)
10387 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10388 {
10389 if (! sub->output_has_begun)
10390 {
10391 if (! elf_link_input_bfd (&finfo, sub))
10392 goto error_return;
10393 sub->output_has_begun = TRUE;
10394 }
10395 }
10396 else if (p->type == bfd_section_reloc_link_order
10397 || p->type == bfd_symbol_reloc_link_order)
10398 {
10399 if (! elf_reloc_link_order (abfd, info, o, p))
10400 goto error_return;
10401 }
10402 else
10403 {
10404 if (! _bfd_default_link_order (abfd, info, o, p))
10405 goto error_return;
10406 }
10407 }
10408 }
10409
c0f00686
L
10410 /* Free symbol buffer if needed. */
10411 if (!info->reduce_memory_overheads)
10412 {
10413 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10414 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10415 && elf_tdata (sub)->symbuf)
c0f00686
L
10416 {
10417 free (elf_tdata (sub)->symbuf);
10418 elf_tdata (sub)->symbuf = NULL;
10419 }
10420 }
10421
c152c796
AM
10422 /* Output any global symbols that got converted to local in a
10423 version script or due to symbol visibility. We do this in a
10424 separate step since ELF requires all local symbols to appear
10425 prior to any global symbols. FIXME: We should only do this if
10426 some global symbols were, in fact, converted to become local.
10427 FIXME: Will this work correctly with the Irix 5 linker? */
10428 eoinfo.failed = FALSE;
10429 eoinfo.finfo = &finfo;
10430 eoinfo.localsyms = TRUE;
10431 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10432 &eoinfo);
10433 if (eoinfo.failed)
10434 return FALSE;
10435
4e617b1e
PB
10436 /* If backend needs to output some local symbols not present in the hash
10437 table, do it now. */
10438 if (bed->elf_backend_output_arch_local_syms)
10439 {
10440 typedef bfd_boolean (*out_sym_func)
10441 (void *, const char *, Elf_Internal_Sym *, asection *,
10442 struct elf_link_hash_entry *);
10443
10444 if (! ((*bed->elf_backend_output_arch_local_syms)
10445 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10446 return FALSE;
10447 }
10448
c152c796
AM
10449 /* That wrote out all the local symbols. Finish up the symbol table
10450 with the global symbols. Even if we want to strip everything we
10451 can, we still need to deal with those global symbols that got
10452 converted to local in a version script. */
10453
10454 /* The sh_info field records the index of the first non local symbol. */
10455 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10456
10457 if (dynamic
10458 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10459 {
10460 Elf_Internal_Sym sym;
10461 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10462 long last_local = 0;
10463
10464 /* Write out the section symbols for the output sections. */
67687978 10465 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10466 {
10467 asection *s;
10468
10469 sym.st_size = 0;
10470 sym.st_name = 0;
10471 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10472 sym.st_other = 0;
10473
10474 for (s = abfd->sections; s != NULL; s = s->next)
10475 {
10476 int indx;
10477 bfd_byte *dest;
10478 long dynindx;
10479
c152c796 10480 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10481 if (dynindx <= 0)
10482 continue;
10483 indx = elf_section_data (s)->this_idx;
c152c796
AM
10484 BFD_ASSERT (indx > 0);
10485 sym.st_shndx = indx;
c0d5a53d
L
10486 if (! check_dynsym (abfd, &sym))
10487 return FALSE;
c152c796
AM
10488 sym.st_value = s->vma;
10489 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10490 if (last_local < dynindx)
10491 last_local = dynindx;
c152c796
AM
10492 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10493 }
c152c796
AM
10494 }
10495
10496 /* Write out the local dynsyms. */
10497 if (elf_hash_table (info)->dynlocal)
10498 {
10499 struct elf_link_local_dynamic_entry *e;
10500 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10501 {
10502 asection *s;
10503 bfd_byte *dest;
10504
10505 sym.st_size = e->isym.st_size;
10506 sym.st_other = e->isym.st_other;
10507
10508 /* Copy the internal symbol as is.
10509 Note that we saved a word of storage and overwrote
10510 the original st_name with the dynstr_index. */
10511 sym = e->isym;
10512
cb33740c
AM
10513 s = bfd_section_from_elf_index (e->input_bfd,
10514 e->isym.st_shndx);
10515 if (s != NULL)
c152c796 10516 {
c152c796
AM
10517 sym.st_shndx =
10518 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10519 if (! check_dynsym (abfd, &sym))
10520 return FALSE;
c152c796
AM
10521 sym.st_value = (s->output_section->vma
10522 + s->output_offset
10523 + e->isym.st_value);
10524 }
10525
10526 if (last_local < e->dynindx)
10527 last_local = e->dynindx;
10528
10529 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10530 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10531 }
10532 }
10533
10534 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10535 last_local + 1;
10536 }
10537
10538 /* We get the global symbols from the hash table. */
10539 eoinfo.failed = FALSE;
10540 eoinfo.localsyms = FALSE;
10541 eoinfo.finfo = &finfo;
10542 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10543 &eoinfo);
10544 if (eoinfo.failed)
10545 return FALSE;
10546
10547 /* If backend needs to output some symbols not present in the hash
10548 table, do it now. */
10549 if (bed->elf_backend_output_arch_syms)
10550 {
10551 typedef bfd_boolean (*out_sym_func)
10552 (void *, const char *, Elf_Internal_Sym *, asection *,
10553 struct elf_link_hash_entry *);
10554
10555 if (! ((*bed->elf_backend_output_arch_syms)
10556 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10557 return FALSE;
10558 }
10559
10560 /* Flush all symbols to the file. */
10561 if (! elf_link_flush_output_syms (&finfo, bed))
10562 return FALSE;
10563
10564 /* Now we know the size of the symtab section. */
10565 off += symtab_hdr->sh_size;
10566
10567 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10568 if (symtab_shndx_hdr->sh_name != 0)
10569 {
10570 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10571 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10572 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10573 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10574 symtab_shndx_hdr->sh_size = amt;
10575
10576 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10577 off, TRUE);
10578
10579 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10580 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10581 return FALSE;
10582 }
10583
10584
10585 /* Finish up and write out the symbol string table (.strtab)
10586 section. */
10587 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10588 /* sh_name was set in prep_headers. */
10589 symstrtab_hdr->sh_type = SHT_STRTAB;
10590 symstrtab_hdr->sh_flags = 0;
10591 symstrtab_hdr->sh_addr = 0;
10592 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10593 symstrtab_hdr->sh_entsize = 0;
10594 symstrtab_hdr->sh_link = 0;
10595 symstrtab_hdr->sh_info = 0;
10596 /* sh_offset is set just below. */
10597 symstrtab_hdr->sh_addralign = 1;
10598
10599 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10600 elf_tdata (abfd)->next_file_pos = off;
10601
10602 if (bfd_get_symcount (abfd) > 0)
10603 {
10604 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10605 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10606 return FALSE;
10607 }
10608
10609 /* Adjust the relocs to have the correct symbol indices. */
10610 for (o = abfd->sections; o != NULL; o = o->next)
10611 {
10612 if ((o->flags & SEC_RELOC) == 0)
10613 continue;
10614
10615 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10616 elf_section_data (o)->rel_count,
10617 elf_section_data (o)->rel_hashes);
10618 if (elf_section_data (o)->rel_hdr2 != NULL)
10619 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10620 elf_section_data (o)->rel_count2,
10621 (elf_section_data (o)->rel_hashes
10622 + elf_section_data (o)->rel_count));
10623
10624 /* Set the reloc_count field to 0 to prevent write_relocs from
10625 trying to swap the relocs out itself. */
10626 o->reloc_count = 0;
10627 }
10628
10629 if (dynamic && info->combreloc && dynobj != NULL)
10630 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10631
10632 /* If we are linking against a dynamic object, or generating a
10633 shared library, finish up the dynamic linking information. */
10634 if (dynamic)
10635 {
10636 bfd_byte *dyncon, *dynconend;
10637
10638 /* Fix up .dynamic entries. */
10639 o = bfd_get_section_by_name (dynobj, ".dynamic");
10640 BFD_ASSERT (o != NULL);
10641
10642 dyncon = o->contents;
eea6121a 10643 dynconend = o->contents + o->size;
c152c796
AM
10644 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10645 {
10646 Elf_Internal_Dyn dyn;
10647 const char *name;
10648 unsigned int type;
10649
10650 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10651
10652 switch (dyn.d_tag)
10653 {
10654 default:
10655 continue;
10656 case DT_NULL:
10657 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10658 {
10659 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10660 {
10661 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10662 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10663 default: continue;
10664 }
10665 dyn.d_un.d_val = relativecount;
10666 relativecount = 0;
10667 break;
10668 }
10669 continue;
10670
10671 case DT_INIT:
10672 name = info->init_function;
10673 goto get_sym;
10674 case DT_FINI:
10675 name = info->fini_function;
10676 get_sym:
10677 {
10678 struct elf_link_hash_entry *h;
10679
10680 h = elf_link_hash_lookup (elf_hash_table (info), name,
10681 FALSE, FALSE, TRUE);
10682 if (h != NULL
10683 && (h->root.type == bfd_link_hash_defined
10684 || h->root.type == bfd_link_hash_defweak))
10685 {
10686 dyn.d_un.d_val = h->root.u.def.value;
10687 o = h->root.u.def.section;
10688 if (o->output_section != NULL)
10689 dyn.d_un.d_val += (o->output_section->vma
10690 + o->output_offset);
10691 else
10692 {
10693 /* The symbol is imported from another shared
10694 library and does not apply to this one. */
10695 dyn.d_un.d_val = 0;
10696 }
10697 break;
10698 }
10699 }
10700 continue;
10701
10702 case DT_PREINIT_ARRAYSZ:
10703 name = ".preinit_array";
10704 goto get_size;
10705 case DT_INIT_ARRAYSZ:
10706 name = ".init_array";
10707 goto get_size;
10708 case DT_FINI_ARRAYSZ:
10709 name = ".fini_array";
10710 get_size:
10711 o = bfd_get_section_by_name (abfd, name);
10712 if (o == NULL)
10713 {
10714 (*_bfd_error_handler)
d003868e 10715 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10716 goto error_return;
10717 }
eea6121a 10718 if (o->size == 0)
c152c796
AM
10719 (*_bfd_error_handler)
10720 (_("warning: %s section has zero size"), name);
eea6121a 10721 dyn.d_un.d_val = o->size;
c152c796
AM
10722 break;
10723
10724 case DT_PREINIT_ARRAY:
10725 name = ".preinit_array";
10726 goto get_vma;
10727 case DT_INIT_ARRAY:
10728 name = ".init_array";
10729 goto get_vma;
10730 case DT_FINI_ARRAY:
10731 name = ".fini_array";
10732 goto get_vma;
10733
10734 case DT_HASH:
10735 name = ".hash";
10736 goto get_vma;
fdc90cb4
JJ
10737 case DT_GNU_HASH:
10738 name = ".gnu.hash";
10739 goto get_vma;
c152c796
AM
10740 case DT_STRTAB:
10741 name = ".dynstr";
10742 goto get_vma;
10743 case DT_SYMTAB:
10744 name = ".dynsym";
10745 goto get_vma;
10746 case DT_VERDEF:
10747 name = ".gnu.version_d";
10748 goto get_vma;
10749 case DT_VERNEED:
10750 name = ".gnu.version_r";
10751 goto get_vma;
10752 case DT_VERSYM:
10753 name = ".gnu.version";
10754 get_vma:
10755 o = bfd_get_section_by_name (abfd, name);
10756 if (o == NULL)
10757 {
10758 (*_bfd_error_handler)
d003868e 10759 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10760 goto error_return;
10761 }
10762 dyn.d_un.d_ptr = o->vma;
10763 break;
10764
10765 case DT_REL:
10766 case DT_RELA:
10767 case DT_RELSZ:
10768 case DT_RELASZ:
10769 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10770 type = SHT_REL;
10771 else
10772 type = SHT_RELA;
10773 dyn.d_un.d_val = 0;
10774 for (i = 1; i < elf_numsections (abfd); i++)
10775 {
10776 Elf_Internal_Shdr *hdr;
10777
10778 hdr = elf_elfsections (abfd)[i];
10779 if (hdr->sh_type == type
10780 && (hdr->sh_flags & SHF_ALLOC) != 0)
10781 {
10782 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10783 dyn.d_un.d_val += hdr->sh_size;
10784 else
10785 {
10786 if (dyn.d_un.d_val == 0
10787 || hdr->sh_addr < dyn.d_un.d_val)
10788 dyn.d_un.d_val = hdr->sh_addr;
10789 }
10790 }
10791 }
10792 break;
10793 }
10794 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10795 }
10796 }
10797
10798 /* If we have created any dynamic sections, then output them. */
10799 if (dynobj != NULL)
10800 {
10801 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10802 goto error_return;
10803
943284cc
DJ
10804 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10805 if (info->warn_shared_textrel && info->shared)
10806 {
10807 bfd_byte *dyncon, *dynconend;
10808
10809 /* Fix up .dynamic entries. */
10810 o = bfd_get_section_by_name (dynobj, ".dynamic");
10811 BFD_ASSERT (o != NULL);
10812
10813 dyncon = o->contents;
10814 dynconend = o->contents + o->size;
10815 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10816 {
10817 Elf_Internal_Dyn dyn;
10818
10819 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10820
10821 if (dyn.d_tag == DT_TEXTREL)
10822 {
a0c8462f 10823 info->callbacks->einfo
9267588c 10824 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
10825 break;
10826 }
10827 }
10828 }
10829
c152c796
AM
10830 for (o = dynobj->sections; o != NULL; o = o->next)
10831 {
10832 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10833 || o->size == 0
c152c796
AM
10834 || o->output_section == bfd_abs_section_ptr)
10835 continue;
10836 if ((o->flags & SEC_LINKER_CREATED) == 0)
10837 {
10838 /* At this point, we are only interested in sections
10839 created by _bfd_elf_link_create_dynamic_sections. */
10840 continue;
10841 }
3722b82f
AM
10842 if (elf_hash_table (info)->stab_info.stabstr == o)
10843 continue;
eea6121a
AM
10844 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10845 continue;
c152c796
AM
10846 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10847 != SHT_STRTAB)
10848 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10849 {
10850 if (! bfd_set_section_contents (abfd, o->output_section,
10851 o->contents,
10852 (file_ptr) o->output_offset,
eea6121a 10853 o->size))
c152c796
AM
10854 goto error_return;
10855 }
10856 else
10857 {
10858 /* The contents of the .dynstr section are actually in a
10859 stringtab. */
10860 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10861 if (bfd_seek (abfd, off, SEEK_SET) != 0
10862 || ! _bfd_elf_strtab_emit (abfd,
10863 elf_hash_table (info)->dynstr))
10864 goto error_return;
10865 }
10866 }
10867 }
10868
10869 if (info->relocatable)
10870 {
10871 bfd_boolean failed = FALSE;
10872
10873 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10874 if (failed)
10875 goto error_return;
10876 }
10877
10878 /* If we have optimized stabs strings, output them. */
3722b82f 10879 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
10880 {
10881 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10882 goto error_return;
10883 }
10884
10885 if (info->eh_frame_hdr)
10886 {
10887 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10888 goto error_return;
10889 }
10890
10891 if (finfo.symstrtab != NULL)
10892 _bfd_stringtab_free (finfo.symstrtab);
10893 if (finfo.contents != NULL)
10894 free (finfo.contents);
10895 if (finfo.external_relocs != NULL)
10896 free (finfo.external_relocs);
10897 if (finfo.internal_relocs != NULL)
10898 free (finfo.internal_relocs);
10899 if (finfo.external_syms != NULL)
10900 free (finfo.external_syms);
10901 if (finfo.locsym_shndx != NULL)
10902 free (finfo.locsym_shndx);
10903 if (finfo.internal_syms != NULL)
10904 free (finfo.internal_syms);
10905 if (finfo.indices != NULL)
10906 free (finfo.indices);
10907 if (finfo.sections != NULL)
10908 free (finfo.sections);
10909 if (finfo.symbuf != NULL)
10910 free (finfo.symbuf);
10911 if (finfo.symshndxbuf != NULL)
10912 free (finfo.symshndxbuf);
10913 for (o = abfd->sections; o != NULL; o = o->next)
10914 {
10915 if ((o->flags & SEC_RELOC) != 0
10916 && elf_section_data (o)->rel_hashes != NULL)
10917 free (elf_section_data (o)->rel_hashes);
10918 }
10919
10920 elf_tdata (abfd)->linker = TRUE;
10921
104d59d1
JM
10922 if (attr_section)
10923 {
10924 bfd_byte *contents = bfd_malloc (attr_size);
10925 if (contents == NULL)
d0f16d5e 10926 return FALSE; /* Bail out and fail. */
104d59d1
JM
10927 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
10928 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
10929 free (contents);
10930 }
10931
c152c796
AM
10932 return TRUE;
10933
10934 error_return:
10935 if (finfo.symstrtab != NULL)
10936 _bfd_stringtab_free (finfo.symstrtab);
10937 if (finfo.contents != NULL)
10938 free (finfo.contents);
10939 if (finfo.external_relocs != NULL)
10940 free (finfo.external_relocs);
10941 if (finfo.internal_relocs != NULL)
10942 free (finfo.internal_relocs);
10943 if (finfo.external_syms != NULL)
10944 free (finfo.external_syms);
10945 if (finfo.locsym_shndx != NULL)
10946 free (finfo.locsym_shndx);
10947 if (finfo.internal_syms != NULL)
10948 free (finfo.internal_syms);
10949 if (finfo.indices != NULL)
10950 free (finfo.indices);
10951 if (finfo.sections != NULL)
10952 free (finfo.sections);
10953 if (finfo.symbuf != NULL)
10954 free (finfo.symbuf);
10955 if (finfo.symshndxbuf != NULL)
10956 free (finfo.symshndxbuf);
10957 for (o = abfd->sections; o != NULL; o = o->next)
10958 {
10959 if ((o->flags & SEC_RELOC) != 0
10960 && elf_section_data (o)->rel_hashes != NULL)
10961 free (elf_section_data (o)->rel_hashes);
10962 }
10963
10964 return FALSE;
10965}
10966\f
5241d853
RS
10967/* Initialize COOKIE for input bfd ABFD. */
10968
10969static bfd_boolean
10970init_reloc_cookie (struct elf_reloc_cookie *cookie,
10971 struct bfd_link_info *info, bfd *abfd)
10972{
10973 Elf_Internal_Shdr *symtab_hdr;
10974 const struct elf_backend_data *bed;
10975
10976 bed = get_elf_backend_data (abfd);
10977 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10978
10979 cookie->abfd = abfd;
10980 cookie->sym_hashes = elf_sym_hashes (abfd);
10981 cookie->bad_symtab = elf_bad_symtab (abfd);
10982 if (cookie->bad_symtab)
10983 {
10984 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10985 cookie->extsymoff = 0;
10986 }
10987 else
10988 {
10989 cookie->locsymcount = symtab_hdr->sh_info;
10990 cookie->extsymoff = symtab_hdr->sh_info;
10991 }
10992
10993 if (bed->s->arch_size == 32)
10994 cookie->r_sym_shift = 8;
10995 else
10996 cookie->r_sym_shift = 32;
10997
10998 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10999 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11000 {
11001 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11002 cookie->locsymcount, 0,
11003 NULL, NULL, NULL);
11004 if (cookie->locsyms == NULL)
11005 {
11006 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11007 return FALSE;
11008 }
11009 if (info->keep_memory)
11010 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11011 }
11012 return TRUE;
11013}
11014
11015/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11016
11017static void
11018fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11019{
11020 Elf_Internal_Shdr *symtab_hdr;
11021
11022 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11023 if (cookie->locsyms != NULL
11024 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11025 free (cookie->locsyms);
11026}
11027
11028/* Initialize the relocation information in COOKIE for input section SEC
11029 of input bfd ABFD. */
11030
11031static bfd_boolean
11032init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11033 struct bfd_link_info *info, bfd *abfd,
11034 asection *sec)
11035{
11036 const struct elf_backend_data *bed;
11037
11038 if (sec->reloc_count == 0)
11039 {
11040 cookie->rels = NULL;
11041 cookie->relend = NULL;
11042 }
11043 else
11044 {
11045 bed = get_elf_backend_data (abfd);
11046
11047 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11048 info->keep_memory);
11049 if (cookie->rels == NULL)
11050 return FALSE;
11051 cookie->rel = cookie->rels;
11052 cookie->relend = (cookie->rels
11053 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11054 }
11055 cookie->rel = cookie->rels;
11056 return TRUE;
11057}
11058
11059/* Free the memory allocated by init_reloc_cookie_rels,
11060 if appropriate. */
11061
11062static void
11063fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11064 asection *sec)
11065{
11066 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11067 free (cookie->rels);
11068}
11069
11070/* Initialize the whole of COOKIE for input section SEC. */
11071
11072static bfd_boolean
11073init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11074 struct bfd_link_info *info,
11075 asection *sec)
11076{
11077 if (!init_reloc_cookie (cookie, info, sec->owner))
11078 goto error1;
11079 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11080 goto error2;
11081 return TRUE;
11082
11083 error2:
11084 fini_reloc_cookie (cookie, sec->owner);
11085 error1:
11086 return FALSE;
11087}
11088
11089/* Free the memory allocated by init_reloc_cookie_for_section,
11090 if appropriate. */
11091
11092static void
11093fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11094 asection *sec)
11095{
11096 fini_reloc_cookie_rels (cookie, sec);
11097 fini_reloc_cookie (cookie, sec->owner);
11098}
11099\f
c152c796
AM
11100/* Garbage collect unused sections. */
11101
07adf181
AM
11102/* Default gc_mark_hook. */
11103
11104asection *
11105_bfd_elf_gc_mark_hook (asection *sec,
11106 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11107 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11108 struct elf_link_hash_entry *h,
11109 Elf_Internal_Sym *sym)
11110{
11111 if (h != NULL)
11112 {
11113 switch (h->root.type)
11114 {
11115 case bfd_link_hash_defined:
11116 case bfd_link_hash_defweak:
11117 return h->root.u.def.section;
11118
11119 case bfd_link_hash_common:
11120 return h->root.u.c.p->section;
11121
11122 default:
11123 break;
11124 }
11125 }
11126 else
11127 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11128
11129 return NULL;
11130}
11131
5241d853
RS
11132/* COOKIE->rel describes a relocation against section SEC, which is
11133 a section we've decided to keep. Return the section that contains
11134 the relocation symbol, or NULL if no section contains it. */
11135
11136asection *
11137_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11138 elf_gc_mark_hook_fn gc_mark_hook,
11139 struct elf_reloc_cookie *cookie)
11140{
11141 unsigned long r_symndx;
11142 struct elf_link_hash_entry *h;
11143
11144 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11145 if (r_symndx == 0)
11146 return NULL;
11147
11148 if (r_symndx >= cookie->locsymcount
11149 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11150 {
11151 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11152 while (h->root.type == bfd_link_hash_indirect
11153 || h->root.type == bfd_link_hash_warning)
11154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11155 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11156 }
11157
11158 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11159 &cookie->locsyms[r_symndx]);
11160}
11161
11162/* COOKIE->rel describes a relocation against section SEC, which is
11163 a section we've decided to keep. Mark the section that contains
9d0a14d3 11164 the relocation symbol. */
5241d853
RS
11165
11166bfd_boolean
11167_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11168 asection *sec,
11169 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11170 struct elf_reloc_cookie *cookie)
5241d853
RS
11171{
11172 asection *rsec;
11173
11174 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11175 if (rsec && !rsec->gc_mark)
11176 {
11177 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11178 rsec->gc_mark = 1;
5241d853
RS
11179 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11180 return FALSE;
11181 }
11182 return TRUE;
11183}
11184
07adf181
AM
11185/* The mark phase of garbage collection. For a given section, mark
11186 it and any sections in this section's group, and all the sections
11187 which define symbols to which it refers. */
11188
ccfa59ea
AM
11189bfd_boolean
11190_bfd_elf_gc_mark (struct bfd_link_info *info,
11191 asection *sec,
6a5bb875 11192 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11193{
11194 bfd_boolean ret;
9d0a14d3 11195 asection *group_sec, *eh_frame;
c152c796
AM
11196
11197 sec->gc_mark = 1;
11198
11199 /* Mark all the sections in the group. */
11200 group_sec = elf_section_data (sec)->next_in_group;
11201 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11202 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11203 return FALSE;
11204
11205 /* Look through the section relocs. */
11206 ret = TRUE;
9d0a14d3
RS
11207 eh_frame = elf_eh_frame_section (sec->owner);
11208 if ((sec->flags & SEC_RELOC) != 0
11209 && sec->reloc_count > 0
11210 && sec != eh_frame)
c152c796 11211 {
5241d853 11212 struct elf_reloc_cookie cookie;
c152c796 11213
5241d853
RS
11214 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11215 ret = FALSE;
c152c796 11216 else
c152c796 11217 {
5241d853 11218 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11219 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11220 {
11221 ret = FALSE;
11222 break;
11223 }
11224 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11225 }
11226 }
9d0a14d3
RS
11227
11228 if (ret && eh_frame && elf_fde_list (sec))
11229 {
11230 struct elf_reloc_cookie cookie;
11231
11232 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11233 ret = FALSE;
11234 else
11235 {
11236 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11237 gc_mark_hook, &cookie))
11238 ret = FALSE;
11239 fini_reloc_cookie_for_section (&cookie, eh_frame);
11240 }
11241 }
11242
c152c796
AM
11243 return ret;
11244}
11245
11246/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11247
c17d87de
NC
11248struct elf_gc_sweep_symbol_info
11249{
ccabcbe5
AM
11250 struct bfd_link_info *info;
11251 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11252 bfd_boolean);
11253};
11254
c152c796 11255static bfd_boolean
ccabcbe5 11256elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11257{
c152c796
AM
11258 if (h->root.type == bfd_link_hash_warning)
11259 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11260
ccabcbe5
AM
11261 if ((h->root.type == bfd_link_hash_defined
11262 || h->root.type == bfd_link_hash_defweak)
11263 && !h->root.u.def.section->gc_mark
11264 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11265 {
11266 struct elf_gc_sweep_symbol_info *inf = data;
11267 (*inf->hide_symbol) (inf->info, h, TRUE);
11268 }
c152c796
AM
11269
11270 return TRUE;
11271}
11272
11273/* The sweep phase of garbage collection. Remove all garbage sections. */
11274
11275typedef bfd_boolean (*gc_sweep_hook_fn)
11276 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11277
11278static bfd_boolean
ccabcbe5 11279elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11280{
11281 bfd *sub;
ccabcbe5
AM
11282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11283 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11284 unsigned long section_sym_count;
11285 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11286
11287 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11288 {
11289 asection *o;
11290
11291 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11292 continue;
11293
11294 for (o = sub->sections; o != NULL; o = o->next)
11295 {
7c2c8505
AM
11296 /* Keep debug and special sections. */
11297 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
dea5f36a 11298 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
c152c796
AM
11299 o->gc_mark = 1;
11300
11301 if (o->gc_mark)
11302 continue;
11303
11304 /* Skip sweeping sections already excluded. */
11305 if (o->flags & SEC_EXCLUDE)
11306 continue;
11307
11308 /* Since this is early in the link process, it is simple
11309 to remove a section from the output. */
11310 o->flags |= SEC_EXCLUDE;
11311
c55fe096 11312 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11313 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11314
c152c796
AM
11315 /* But we also have to update some of the relocation
11316 info we collected before. */
11317 if (gc_sweep_hook
e8aaee2a
AM
11318 && (o->flags & SEC_RELOC) != 0
11319 && o->reloc_count > 0
11320 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11321 {
11322 Elf_Internal_Rela *internal_relocs;
11323 bfd_boolean r;
11324
11325 internal_relocs
11326 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11327 info->keep_memory);
11328 if (internal_relocs == NULL)
11329 return FALSE;
11330
11331 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11332
11333 if (elf_section_data (o)->relocs != internal_relocs)
11334 free (internal_relocs);
11335
11336 if (!r)
11337 return FALSE;
11338 }
11339 }
11340 }
11341
11342 /* Remove the symbols that were in the swept sections from the dynamic
11343 symbol table. GCFIXME: Anyone know how to get them out of the
11344 static symbol table as well? */
ccabcbe5
AM
11345 sweep_info.info = info;
11346 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11347 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11348 &sweep_info);
c152c796 11349
ccabcbe5 11350 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11351 return TRUE;
11352}
11353
11354/* Propagate collected vtable information. This is called through
11355 elf_link_hash_traverse. */
11356
11357static bfd_boolean
11358elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11359{
11360 if (h->root.type == bfd_link_hash_warning)
11361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11362
11363 /* Those that are not vtables. */
f6e332e6 11364 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11365 return TRUE;
11366
11367 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11368 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11369 return TRUE;
11370
11371 /* If we've already been done, exit. */
f6e332e6 11372 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11373 return TRUE;
11374
11375 /* Make sure the parent's table is up to date. */
f6e332e6 11376 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11377
f6e332e6 11378 if (h->vtable->used == NULL)
c152c796
AM
11379 {
11380 /* None of this table's entries were referenced. Re-use the
11381 parent's table. */
f6e332e6
AM
11382 h->vtable->used = h->vtable->parent->vtable->used;
11383 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11384 }
11385 else
11386 {
11387 size_t n;
11388 bfd_boolean *cu, *pu;
11389
11390 /* Or the parent's entries into ours. */
f6e332e6 11391 cu = h->vtable->used;
c152c796 11392 cu[-1] = TRUE;
f6e332e6 11393 pu = h->vtable->parent->vtable->used;
c152c796
AM
11394 if (pu != NULL)
11395 {
11396 const struct elf_backend_data *bed;
11397 unsigned int log_file_align;
11398
11399 bed = get_elf_backend_data (h->root.u.def.section->owner);
11400 log_file_align = bed->s->log_file_align;
f6e332e6 11401 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11402 while (n--)
11403 {
11404 if (*pu)
11405 *cu = TRUE;
11406 pu++;
11407 cu++;
11408 }
11409 }
11410 }
11411
11412 return TRUE;
11413}
11414
11415static bfd_boolean
11416elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11417{
11418 asection *sec;
11419 bfd_vma hstart, hend;
11420 Elf_Internal_Rela *relstart, *relend, *rel;
11421 const struct elf_backend_data *bed;
11422 unsigned int log_file_align;
11423
11424 if (h->root.type == bfd_link_hash_warning)
11425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11426
11427 /* Take care of both those symbols that do not describe vtables as
11428 well as those that are not loaded. */
f6e332e6 11429 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11430 return TRUE;
11431
11432 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11433 || h->root.type == bfd_link_hash_defweak);
11434
11435 sec = h->root.u.def.section;
11436 hstart = h->root.u.def.value;
11437 hend = hstart + h->size;
11438
11439 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11440 if (!relstart)
11441 return *(bfd_boolean *) okp = FALSE;
11442 bed = get_elf_backend_data (sec->owner);
11443 log_file_align = bed->s->log_file_align;
11444
11445 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11446
11447 for (rel = relstart; rel < relend; ++rel)
11448 if (rel->r_offset >= hstart && rel->r_offset < hend)
11449 {
11450 /* If the entry is in use, do nothing. */
f6e332e6
AM
11451 if (h->vtable->used
11452 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11453 {
11454 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11455 if (h->vtable->used[entry])
c152c796
AM
11456 continue;
11457 }
11458 /* Otherwise, kill it. */
11459 rel->r_offset = rel->r_info = rel->r_addend = 0;
11460 }
11461
11462 return TRUE;
11463}
11464
87538722
AM
11465/* Mark sections containing dynamically referenced symbols. When
11466 building shared libraries, we must assume that any visible symbol is
11467 referenced. */
715df9b8 11468
64d03ab5
AM
11469bfd_boolean
11470bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11471{
87538722
AM
11472 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11473
715df9b8
EB
11474 if (h->root.type == bfd_link_hash_warning)
11475 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11476
11477 if ((h->root.type == bfd_link_hash_defined
11478 || h->root.type == bfd_link_hash_defweak)
87538722 11479 && (h->ref_dynamic
5adcfd8b 11480 || (!info->executable
87538722
AM
11481 && h->def_regular
11482 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11483 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11484 h->root.u.def.section->flags |= SEC_KEEP;
11485
11486 return TRUE;
11487}
3b36f7e6 11488
74f0fb50
AM
11489/* Keep all sections containing symbols undefined on the command-line,
11490 and the section containing the entry symbol. */
11491
11492void
11493_bfd_elf_gc_keep (struct bfd_link_info *info)
11494{
11495 struct bfd_sym_chain *sym;
11496
11497 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11498 {
11499 struct elf_link_hash_entry *h;
11500
11501 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11502 FALSE, FALSE, FALSE);
11503
11504 if (h != NULL
11505 && (h->root.type == bfd_link_hash_defined
11506 || h->root.type == bfd_link_hash_defweak)
11507 && !bfd_is_abs_section (h->root.u.def.section))
11508 h->root.u.def.section->flags |= SEC_KEEP;
11509 }
11510}
11511
c152c796
AM
11512/* Do mark and sweep of unused sections. */
11513
11514bfd_boolean
11515bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11516{
11517 bfd_boolean ok = TRUE;
11518 bfd *sub;
6a5bb875 11519 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11520 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11521
64d03ab5 11522 if (!bed->can_gc_sections
715df9b8 11523 || !is_elf_hash_table (info->hash))
c152c796
AM
11524 {
11525 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11526 return TRUE;
11527 }
11528
74f0fb50
AM
11529 bed->gc_keep (info);
11530
9d0a14d3
RS
11531 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11532 at the .eh_frame section if we can mark the FDEs individually. */
11533 _bfd_elf_begin_eh_frame_parsing (info);
11534 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11535 {
11536 asection *sec;
11537 struct elf_reloc_cookie cookie;
11538
11539 sec = bfd_get_section_by_name (sub, ".eh_frame");
11540 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11541 {
11542 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11543 if (elf_section_data (sec)->sec_info)
11544 elf_eh_frame_section (sub) = sec;
11545 fini_reloc_cookie_for_section (&cookie, sec);
11546 }
11547 }
11548 _bfd_elf_end_eh_frame_parsing (info);
11549
c152c796
AM
11550 /* Apply transitive closure to the vtable entry usage info. */
11551 elf_link_hash_traverse (elf_hash_table (info),
11552 elf_gc_propagate_vtable_entries_used,
11553 &ok);
11554 if (!ok)
11555 return FALSE;
11556
11557 /* Kill the vtable relocations that were not used. */
11558 elf_link_hash_traverse (elf_hash_table (info),
11559 elf_gc_smash_unused_vtentry_relocs,
11560 &ok);
11561 if (!ok)
11562 return FALSE;
11563
715df9b8
EB
11564 /* Mark dynamically referenced symbols. */
11565 if (elf_hash_table (info)->dynamic_sections_created)
11566 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 11567 bed->gc_mark_dynamic_ref,
87538722 11568 info);
c152c796 11569
715df9b8 11570 /* Grovel through relocs to find out who stays ... */
64d03ab5 11571 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
11572 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11573 {
11574 asection *o;
11575
11576 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11577 continue;
11578
11579 for (o = sub->sections; o != NULL; o = o->next)
a14a5de3 11580 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
39c2f51b
AM
11581 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11582 return FALSE;
c152c796
AM
11583 }
11584
6a5bb875
PB
11585 /* Allow the backend to mark additional target specific sections. */
11586 if (bed->gc_mark_extra_sections)
74f0fb50 11587 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 11588
c152c796 11589 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 11590 return elf_gc_sweep (abfd, info);
c152c796
AM
11591}
11592\f
11593/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11594
11595bfd_boolean
11596bfd_elf_gc_record_vtinherit (bfd *abfd,
11597 asection *sec,
11598 struct elf_link_hash_entry *h,
11599 bfd_vma offset)
11600{
11601 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11602 struct elf_link_hash_entry **search, *child;
11603 bfd_size_type extsymcount;
11604 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11605
11606 /* The sh_info field of the symtab header tells us where the
11607 external symbols start. We don't care about the local symbols at
11608 this point. */
11609 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11610 if (!elf_bad_symtab (abfd))
11611 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11612
11613 sym_hashes = elf_sym_hashes (abfd);
11614 sym_hashes_end = sym_hashes + extsymcount;
11615
11616 /* Hunt down the child symbol, which is in this section at the same
11617 offset as the relocation. */
11618 for (search = sym_hashes; search != sym_hashes_end; ++search)
11619 {
11620 if ((child = *search) != NULL
11621 && (child->root.type == bfd_link_hash_defined
11622 || child->root.type == bfd_link_hash_defweak)
11623 && child->root.u.def.section == sec
11624 && child->root.u.def.value == offset)
11625 goto win;
11626 }
11627
d003868e
AM
11628 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11629 abfd, sec, (unsigned long) offset);
c152c796
AM
11630 bfd_set_error (bfd_error_invalid_operation);
11631 return FALSE;
11632
11633 win:
f6e332e6
AM
11634 if (!child->vtable)
11635 {
11636 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11637 if (!child->vtable)
11638 return FALSE;
11639 }
c152c796
AM
11640 if (!h)
11641 {
11642 /* This *should* only be the absolute section. It could potentially
11643 be that someone has defined a non-global vtable though, which
11644 would be bad. It isn't worth paging in the local symbols to be
11645 sure though; that case should simply be handled by the assembler. */
11646
f6e332e6 11647 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
11648 }
11649 else
f6e332e6 11650 child->vtable->parent = h;
c152c796
AM
11651
11652 return TRUE;
11653}
11654
11655/* Called from check_relocs to record the existence of a VTENTRY reloc. */
11656
11657bfd_boolean
11658bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11659 asection *sec ATTRIBUTE_UNUSED,
11660 struct elf_link_hash_entry *h,
11661 bfd_vma addend)
11662{
11663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11664 unsigned int log_file_align = bed->s->log_file_align;
11665
f6e332e6
AM
11666 if (!h->vtable)
11667 {
11668 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11669 if (!h->vtable)
11670 return FALSE;
11671 }
11672
11673 if (addend >= h->vtable->size)
c152c796
AM
11674 {
11675 size_t size, bytes, file_align;
f6e332e6 11676 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
11677
11678 /* While the symbol is undefined, we have to be prepared to handle
11679 a zero size. */
11680 file_align = 1 << log_file_align;
11681 if (h->root.type == bfd_link_hash_undefined)
11682 size = addend + file_align;
11683 else
11684 {
11685 size = h->size;
11686 if (addend >= size)
11687 {
11688 /* Oops! We've got a reference past the defined end of
11689 the table. This is probably a bug -- shall we warn? */
11690 size = addend + file_align;
11691 }
11692 }
11693 size = (size + file_align - 1) & -file_align;
11694
11695 /* Allocate one extra entry for use as a "done" flag for the
11696 consolidation pass. */
11697 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11698
11699 if (ptr)
11700 {
11701 ptr = bfd_realloc (ptr - 1, bytes);
11702
11703 if (ptr != NULL)
11704 {
11705 size_t oldbytes;
11706
f6e332e6 11707 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
11708 * sizeof (bfd_boolean));
11709 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11710 }
11711 }
11712 else
11713 ptr = bfd_zmalloc (bytes);
11714
11715 if (ptr == NULL)
11716 return FALSE;
11717
11718 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
11719 h->vtable->used = ptr + 1;
11720 h->vtable->size = size;
c152c796
AM
11721 }
11722
f6e332e6 11723 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
11724
11725 return TRUE;
11726}
11727
11728struct alloc_got_off_arg {
11729 bfd_vma gotoff;
11730 unsigned int got_elt_size;
11731};
11732
11733/* We need a special top-level link routine to convert got reference counts
11734 to real got offsets. */
11735
11736static bfd_boolean
11737elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11738{
11739 struct alloc_got_off_arg *gofarg = arg;
11740
11741 if (h->root.type == bfd_link_hash_warning)
11742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11743
11744 if (h->got.refcount > 0)
11745 {
11746 h->got.offset = gofarg->gotoff;
11747 gofarg->gotoff += gofarg->got_elt_size;
11748 }
11749 else
11750 h->got.offset = (bfd_vma) -1;
11751
11752 return TRUE;
11753}
11754
11755/* And an accompanying bit to work out final got entry offsets once
11756 we're done. Should be called from final_link. */
11757
11758bfd_boolean
11759bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11760 struct bfd_link_info *info)
11761{
11762 bfd *i;
11763 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11764 bfd_vma gotoff;
11765 unsigned int got_elt_size = bed->s->arch_size / 8;
11766 struct alloc_got_off_arg gofarg;
11767
11768 if (! is_elf_hash_table (info->hash))
11769 return FALSE;
11770
11771 /* The GOT offset is relative to the .got section, but the GOT header is
11772 put into the .got.plt section, if the backend uses it. */
11773 if (bed->want_got_plt)
11774 gotoff = 0;
11775 else
11776 gotoff = bed->got_header_size;
11777
11778 /* Do the local .got entries first. */
11779 for (i = info->input_bfds; i; i = i->link_next)
11780 {
11781 bfd_signed_vma *local_got;
11782 bfd_size_type j, locsymcount;
11783 Elf_Internal_Shdr *symtab_hdr;
11784
11785 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11786 continue;
11787
11788 local_got = elf_local_got_refcounts (i);
11789 if (!local_got)
11790 continue;
11791
11792 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11793 if (elf_bad_symtab (i))
11794 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11795 else
11796 locsymcount = symtab_hdr->sh_info;
11797
11798 for (j = 0; j < locsymcount; ++j)
11799 {
11800 if (local_got[j] > 0)
11801 {
11802 local_got[j] = gotoff;
11803 gotoff += got_elt_size;
11804 }
11805 else
11806 local_got[j] = (bfd_vma) -1;
11807 }
11808 }
11809
11810 /* Then the global .got entries. .plt refcounts are handled by
11811 adjust_dynamic_symbol */
11812 gofarg.gotoff = gotoff;
11813 gofarg.got_elt_size = got_elt_size;
11814 elf_link_hash_traverse (elf_hash_table (info),
11815 elf_gc_allocate_got_offsets,
11816 &gofarg);
11817 return TRUE;
11818}
11819
11820/* Many folk need no more in the way of final link than this, once
11821 got entry reference counting is enabled. */
11822
11823bfd_boolean
11824bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11825{
11826 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11827 return FALSE;
11828
11829 /* Invoke the regular ELF backend linker to do all the work. */
11830 return bfd_elf_final_link (abfd, info);
11831}
11832
11833bfd_boolean
11834bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11835{
11836 struct elf_reloc_cookie *rcookie = cookie;
11837
11838 if (rcookie->bad_symtab)
11839 rcookie->rel = rcookie->rels;
11840
11841 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11842 {
11843 unsigned long r_symndx;
11844
11845 if (! rcookie->bad_symtab)
11846 if (rcookie->rel->r_offset > offset)
11847 return FALSE;
11848 if (rcookie->rel->r_offset != offset)
11849 continue;
11850
11851 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11852 if (r_symndx == SHN_UNDEF)
11853 return TRUE;
11854
11855 if (r_symndx >= rcookie->locsymcount
11856 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11857 {
11858 struct elf_link_hash_entry *h;
11859
11860 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11861
11862 while (h->root.type == bfd_link_hash_indirect
11863 || h->root.type == bfd_link_hash_warning)
11864 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11865
11866 if ((h->root.type == bfd_link_hash_defined
11867 || h->root.type == bfd_link_hash_defweak)
11868 && elf_discarded_section (h->root.u.def.section))
11869 return TRUE;
11870 else
11871 return FALSE;
11872 }
11873 else
11874 {
11875 /* It's not a relocation against a global symbol,
11876 but it could be a relocation against a local
11877 symbol for a discarded section. */
11878 asection *isec;
11879 Elf_Internal_Sym *isym;
11880
11881 /* Need to: get the symbol; get the section. */
11882 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
11883 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11884 if (isec != NULL && elf_discarded_section (isec))
11885 return TRUE;
c152c796
AM
11886 }
11887 return FALSE;
11888 }
11889 return FALSE;
11890}
11891
11892/* Discard unneeded references to discarded sections.
11893 Returns TRUE if any section's size was changed. */
11894/* This function assumes that the relocations are in sorted order,
11895 which is true for all known assemblers. */
11896
11897bfd_boolean
11898bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11899{
11900 struct elf_reloc_cookie cookie;
11901 asection *stab, *eh;
c152c796
AM
11902 const struct elf_backend_data *bed;
11903 bfd *abfd;
c152c796
AM
11904 bfd_boolean ret = FALSE;
11905
11906 if (info->traditional_format
11907 || !is_elf_hash_table (info->hash))
11908 return FALSE;
11909
ca92cecb 11910 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
11911 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11912 {
11913 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11914 continue;
11915
11916 bed = get_elf_backend_data (abfd);
11917
11918 if ((abfd->flags & DYNAMIC) != 0)
11919 continue;
11920
8da3dbc5
AM
11921 eh = NULL;
11922 if (!info->relocatable)
11923 {
11924 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11925 if (eh != NULL
eea6121a 11926 && (eh->size == 0
8da3dbc5
AM
11927 || bfd_is_abs_section (eh->output_section)))
11928 eh = NULL;
11929 }
c152c796
AM
11930
11931 stab = bfd_get_section_by_name (abfd, ".stab");
11932 if (stab != NULL
eea6121a 11933 && (stab->size == 0
c152c796
AM
11934 || bfd_is_abs_section (stab->output_section)
11935 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11936 stab = NULL;
11937
11938 if (stab == NULL
11939 && eh == NULL
11940 && bed->elf_backend_discard_info == NULL)
11941 continue;
11942
5241d853
RS
11943 if (!init_reloc_cookie (&cookie, info, abfd))
11944 return FALSE;
c152c796 11945
5241d853
RS
11946 if (stab != NULL
11947 && stab->reloc_count > 0
11948 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 11949 {
5241d853
RS
11950 if (_bfd_discard_section_stabs (abfd, stab,
11951 elf_section_data (stab)->sec_info,
11952 bfd_elf_reloc_symbol_deleted_p,
11953 &cookie))
11954 ret = TRUE;
11955 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
11956 }
11957
5241d853
RS
11958 if (eh != NULL
11959 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 11960 {
ca92cecb 11961 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
11962 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
11963 bfd_elf_reloc_symbol_deleted_p,
11964 &cookie))
11965 ret = TRUE;
5241d853 11966 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
11967 }
11968
11969 if (bed->elf_backend_discard_info != NULL
11970 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
11971 ret = TRUE;
11972
5241d853 11973 fini_reloc_cookie (&cookie, abfd);
c152c796 11974 }
ca92cecb 11975 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
11976
11977 if (info->eh_frame_hdr
11978 && !info->relocatable
11979 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
11980 ret = TRUE;
11981
11982 return ret;
11983}
082b7297
L
11984
11985void
c0f00686
L
11986_bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
11987 struct bfd_link_info *info)
082b7297
L
11988{
11989 flagword flags;
6d2cd210 11990 const char *name, *p;
082b7297
L
11991 struct bfd_section_already_linked *l;
11992 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 11993
3d7f7666
L
11994 if (sec->output_section == bfd_abs_section_ptr)
11995 return;
082b7297
L
11996
11997 flags = sec->flags;
3d7f7666 11998
c2370991
AM
11999 /* Return if it isn't a linkonce section. A comdat group section
12000 also has SEC_LINK_ONCE set. */
12001 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12002 return;
12003
c2370991
AM
12004 /* Don't put group member sections on our list of already linked
12005 sections. They are handled as a group via their group section. */
12006 if (elf_sec_group (sec) != NULL)
12007 return;
3d7f7666 12008
082b7297
L
12009 /* FIXME: When doing a relocatable link, we may have trouble
12010 copying relocations in other sections that refer to local symbols
12011 in the section being discarded. Those relocations will have to
12012 be converted somehow; as of this writing I'm not sure that any of
12013 the backends handle that correctly.
12014
12015 It is tempting to instead not discard link once sections when
12016 doing a relocatable link (technically, they should be discarded
12017 whenever we are building constructors). However, that fails,
12018 because the linker winds up combining all the link once sections
12019 into a single large link once section, which defeats the purpose
12020 of having link once sections in the first place.
12021
12022 Also, not merging link once sections in a relocatable link
12023 causes trouble for MIPS ELF, which relies on link once semantics
12024 to handle the .reginfo section correctly. */
12025
12026 name = bfd_get_section_name (abfd, sec);
12027
0112cd26 12028 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12029 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12030 p++;
12031 else
12032 p = name;
12033
12034 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12035
12036 for (l = already_linked_list->entry; l != NULL; l = l->next)
12037 {
c2370991
AM
12038 /* We may have 2 different types of sections on the list: group
12039 sections and linkonce sections. Match like sections. */
3d7f7666 12040 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
6d2cd210 12041 && strcmp (name, l->sec->name) == 0
082b7297
L
12042 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12043 {
12044 /* The section has already been linked. See if we should
6d2cd210 12045 issue a warning. */
082b7297
L
12046 switch (flags & SEC_LINK_DUPLICATES)
12047 {
12048 default:
12049 abort ();
12050
12051 case SEC_LINK_DUPLICATES_DISCARD:
12052 break;
12053
12054 case SEC_LINK_DUPLICATES_ONE_ONLY:
12055 (*_bfd_error_handler)
c93625e2 12056 (_("%B: ignoring duplicate section `%A'"),
d003868e 12057 abfd, sec);
082b7297
L
12058 break;
12059
12060 case SEC_LINK_DUPLICATES_SAME_SIZE:
12061 if (sec->size != l->sec->size)
12062 (*_bfd_error_handler)
c93625e2 12063 (_("%B: duplicate section `%A' has different size"),
d003868e 12064 abfd, sec);
082b7297 12065 break;
ea5158d8
DJ
12066
12067 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12068 if (sec->size != l->sec->size)
12069 (*_bfd_error_handler)
c93625e2 12070 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12071 abfd, sec);
12072 else if (sec->size != 0)
12073 {
12074 bfd_byte *sec_contents, *l_sec_contents;
12075
12076 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12077 (*_bfd_error_handler)
c93625e2 12078 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12079 abfd, sec);
12080 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12081 &l_sec_contents))
12082 (*_bfd_error_handler)
c93625e2 12083 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12084 l->sec->owner, l->sec);
12085 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12086 (*_bfd_error_handler)
c93625e2 12087 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12088 abfd, sec);
12089
12090 if (sec_contents)
12091 free (sec_contents);
12092 if (l_sec_contents)
12093 free (l_sec_contents);
12094 }
12095 break;
082b7297
L
12096 }
12097
12098 /* Set the output_section field so that lang_add_section
12099 does not create a lang_input_section structure for this
12100 section. Since there might be a symbol in the section
12101 being discarded, we must retain a pointer to the section
12102 which we are really going to use. */
12103 sec->output_section = bfd_abs_section_ptr;
12104 sec->kept_section = l->sec;
3b36f7e6 12105
082b7297 12106 if (flags & SEC_GROUP)
3d7f7666
L
12107 {
12108 asection *first = elf_next_in_group (sec);
12109 asection *s = first;
12110
12111 while (s != NULL)
12112 {
12113 s->output_section = bfd_abs_section_ptr;
12114 /* Record which group discards it. */
12115 s->kept_section = l->sec;
12116 s = elf_next_in_group (s);
12117 /* These lists are circular. */
12118 if (s == first)
12119 break;
12120 }
12121 }
082b7297
L
12122
12123 return;
12124 }
12125 }
12126
c2370991
AM
12127 /* A single member comdat group section may be discarded by a
12128 linkonce section and vice versa. */
12129
12130 if ((flags & SEC_GROUP) != 0)
3d7f7666 12131 {
c2370991
AM
12132 asection *first = elf_next_in_group (sec);
12133
12134 if (first != NULL && elf_next_in_group (first) == first)
12135 /* Check this single member group against linkonce sections. */
12136 for (l = already_linked_list->entry; l != NULL; l = l->next)
12137 if ((l->sec->flags & SEC_GROUP) == 0
12138 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12139 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12140 {
12141 first->output_section = bfd_abs_section_ptr;
12142 first->kept_section = l->sec;
12143 sec->output_section = bfd_abs_section_ptr;
12144 break;
12145 }
3d7f7666
L
12146 }
12147 else
c2370991 12148 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12149 for (l = already_linked_list->entry; l != NULL; l = l->next)
12150 if (l->sec->flags & SEC_GROUP)
12151 {
12152 asection *first = elf_next_in_group (l->sec);
12153
12154 if (first != NULL
12155 && elf_next_in_group (first) == first
c0f00686 12156 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12157 {
12158 sec->output_section = bfd_abs_section_ptr;
c2370991 12159 sec->kept_section = first;
6d2cd210
JJ
12160 break;
12161 }
12162 }
12163
082b7297 12164 /* This is the first section with this name. Record it. */
a6626e8c
MS
12165 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12166 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
082b7297 12167}
81e1b023 12168
a4d8e49b
L
12169bfd_boolean
12170_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12171{
12172 return sym->st_shndx == SHN_COMMON;
12173}
12174
12175unsigned int
12176_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12177{
12178 return SHN_COMMON;
12179}
12180
12181asection *
12182_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12183{
12184 return bfd_com_section_ptr;
12185}
This page took 1.230197 seconds and 4 git commands to generate.