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