bfd/
[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
d8880531
L
4299 {
4300 h->def_regular = 1;
4301 if (h->def_dynamic)
4302 {
4303 h->def_dynamic = 0;
4304 h->ref_dynamic = 1;
4305 h->dynamic_def = 1;
4306 }
4307 }
4ad4eba5 4308 if (! info->executable
f5385ebf
AM
4309 || h->def_dynamic
4310 || h->ref_dynamic)
4ad4eba5
AM
4311 dynsym = TRUE;
4312 }
4313 else
4314 {
4315 if (! definition)
f5385ebf 4316 h->ref_dynamic = 1;
4ad4eba5 4317 else
f5385ebf
AM
4318 h->def_dynamic = 1;
4319 if (h->def_regular
4320 || h->ref_regular
f6e332e6 4321 || (h->u.weakdef != NULL
4ad4eba5 4322 && ! new_weakdef
f6e332e6 4323 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4324 dynsym = TRUE;
4325 }
4326
b2064611 4327 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
92b7c7b6
L
4328 {
4329 /* We don't want to make debug symbol dynamic. */
4330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4331 dynsym = FALSE;
4332 }
4333
4ad4eba5
AM
4334 /* Check to see if we need to add an indirect symbol for
4335 the default name. */
4336 if (definition || h->root.type == bfd_link_hash_common)
4337 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4338 &sec, &value, &dynsym,
4339 override))
4340 goto error_free_vers;
4341
4342 if (definition && !dynamic)
4343 {
4344 char *p = strchr (name, ELF_VER_CHR);
4345 if (p != NULL && p[1] != ELF_VER_CHR)
4346 {
4347 /* Queue non-default versions so that .symver x, x@FOO
4348 aliases can be checked. */
66eb6687 4349 if (!nondeflt_vers)
4ad4eba5 4350 {
66eb6687
AM
4351 amt = ((isymend - isym + 1)
4352 * sizeof (struct elf_link_hash_entry *));
4ad4eba5 4353 nondeflt_vers = bfd_malloc (amt);
14b1c01e
AM
4354 if (!nondeflt_vers)
4355 goto error_free_vers;
4ad4eba5 4356 }
66eb6687 4357 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4358 }
4359 }
4360
4361 if (dynsym && h->dynindx == -1)
4362 {
c152c796 4363 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4364 goto error_free_vers;
f6e332e6 4365 if (h->u.weakdef != NULL
4ad4eba5 4366 && ! new_weakdef
f6e332e6 4367 && h->u.weakdef->dynindx == -1)
4ad4eba5 4368 {
66eb6687 4369 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4370 goto error_free_vers;
4371 }
4372 }
4373 else if (dynsym && h->dynindx != -1)
4374 /* If the symbol already has a dynamic index, but
4375 visibility says it should not be visible, turn it into
4376 a local symbol. */
4377 switch (ELF_ST_VISIBILITY (h->other))
4378 {
4379 case STV_INTERNAL:
4380 case STV_HIDDEN:
4381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4382 dynsym = FALSE;
4383 break;
4384 }
4385
4386 if (!add_needed
4387 && definition
4388 && dynsym
f5385ebf 4389 && h->ref_regular)
4ad4eba5
AM
4390 {
4391 int ret;
4392 const char *soname = elf_dt_name (abfd);
4393
4394 /* A symbol from a library loaded via DT_NEEDED of some
4395 other library is referenced by a regular object.
e56f61be
L
4396 Add a DT_NEEDED entry for it. Issue an error if
4397 --no-add-needed is used. */
4398 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4399 {
4400 (*_bfd_error_handler)
4401 (_("%s: invalid DSO for symbol `%s' definition"),
d003868e 4402 abfd, name);
e56f61be
L
4403 bfd_set_error (bfd_error_bad_value);
4404 goto error_free_vers;
4405 }
4406
a5db907e
AM
4407 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4408
4ad4eba5 4409 add_needed = TRUE;
7e9f0867 4410 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4411 if (ret < 0)
4412 goto error_free_vers;
4413
4414 BFD_ASSERT (ret == 0);
4415 }
4416 }
4417 }
4418
66eb6687
AM
4419 if (extversym != NULL)
4420 {
4421 free (extversym);
4422 extversym = NULL;
4423 }
4424
4425 if (isymbuf != NULL)
4426 {
4427 free (isymbuf);
4428 isymbuf = NULL;
4429 }
4430
4431 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4432 {
4433 unsigned int i;
4434
4435 /* Restore the symbol table. */
97fed1c9
JJ
4436 if (bed->as_needed_cleanup)
4437 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4438 old_hash = (char *) old_tab + tabsize;
4439 old_ent = (char *) old_hash + hashsize;
4440 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4441 htab->root.table.table = old_table;
4442 htab->root.table.size = old_size;
4443 htab->root.table.count = old_count;
66eb6687
AM
4444 memcpy (htab->root.table.table, old_tab, tabsize);
4445 memcpy (sym_hash, old_hash, hashsize);
4446 htab->root.undefs = old_undefs;
4447 htab->root.undefs_tail = old_undefs_tail;
4448 for (i = 0; i < htab->root.table.size; i++)
4449 {
4450 struct bfd_hash_entry *p;
4451 struct elf_link_hash_entry *h;
4452
4453 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4454 {
4455 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4456 if (h->root.type == bfd_link_hash_warning)
4457 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4458 if (h->dynindx >= old_dynsymcount)
4459 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4460
66eb6687
AM
4461 memcpy (p, old_ent, htab->root.table.entsize);
4462 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4463 h = (struct elf_link_hash_entry *) p;
4464 if (h->root.type == bfd_link_hash_warning)
4465 {
4466 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4467 old_ent = (char *) old_ent + htab->root.table.entsize;
4468 }
66eb6687
AM
4469 }
4470 }
4471
5061a885
AM
4472 /* Make a special call to the linker "notice" function to
4473 tell it that symbols added for crefs may need to be removed. */
4474 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4475 notice_not_needed))
9af2a943 4476 goto error_free_vers;
5061a885 4477
66eb6687
AM
4478 free (old_tab);
4479 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4480 alloc_mark);
4481 if (nondeflt_vers != NULL)
4482 free (nondeflt_vers);
4483 return TRUE;
4484 }
2de92251 4485
66eb6687
AM
4486 if (old_tab != NULL)
4487 {
5061a885
AM
4488 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4489 notice_needed))
9af2a943 4490 goto error_free_vers;
66eb6687
AM
4491 free (old_tab);
4492 old_tab = NULL;
4493 }
4494
4ad4eba5
AM
4495 /* Now that all the symbols from this input file are created, handle
4496 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4497 if (nondeflt_vers != NULL)
4498 {
4499 bfd_size_type cnt, symidx;
4500
4501 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4502 {
4503 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4504 char *shortname, *p;
4505
4506 p = strchr (h->root.root.string, ELF_VER_CHR);
4507 if (p == NULL
4508 || (h->root.type != bfd_link_hash_defined
4509 && h->root.type != bfd_link_hash_defweak))
4510 continue;
4511
4512 amt = p - h->root.root.string;
4513 shortname = bfd_malloc (amt + 1);
14b1c01e
AM
4514 if (!shortname)
4515 goto error_free_vers;
4ad4eba5
AM
4516 memcpy (shortname, h->root.root.string, amt);
4517 shortname[amt] = '\0';
4518
4519 hi = (struct elf_link_hash_entry *)
66eb6687 4520 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4521 FALSE, FALSE, FALSE);
4522 if (hi != NULL
4523 && hi->root.type == h->root.type
4524 && hi->root.u.def.value == h->root.u.def.value
4525 && hi->root.u.def.section == h->root.u.def.section)
4526 {
4527 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4528 hi->root.type = bfd_link_hash_indirect;
4529 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4530 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4531 sym_hash = elf_sym_hashes (abfd);
4532 if (sym_hash)
4533 for (symidx = 0; symidx < extsymcount; ++symidx)
4534 if (sym_hash[symidx] == hi)
4535 {
4536 sym_hash[symidx] = h;
4537 break;
4538 }
4539 }
4540 free (shortname);
4541 }
4542 free (nondeflt_vers);
4543 nondeflt_vers = NULL;
4544 }
4545
4ad4eba5
AM
4546 /* Now set the weakdefs field correctly for all the weak defined
4547 symbols we found. The only way to do this is to search all the
4548 symbols. Since we only need the information for non functions in
4549 dynamic objects, that's the only time we actually put anything on
4550 the list WEAKS. We need this information so that if a regular
4551 object refers to a symbol defined weakly in a dynamic object, the
4552 real symbol in the dynamic object is also put in the dynamic
4553 symbols; we also must arrange for both symbols to point to the
4554 same memory location. We could handle the general case of symbol
4555 aliasing, but a general symbol alias can only be generated in
4556 assembler code, handling it correctly would be very time
4557 consuming, and other ELF linkers don't handle general aliasing
4558 either. */
4559 if (weaks != NULL)
4560 {
4561 struct elf_link_hash_entry **hpp;
4562 struct elf_link_hash_entry **hppend;
4563 struct elf_link_hash_entry **sorted_sym_hash;
4564 struct elf_link_hash_entry *h;
4565 size_t sym_count;
4566
4567 /* Since we have to search the whole symbol list for each weak
4568 defined symbol, search time for N weak defined symbols will be
4569 O(N^2). Binary search will cut it down to O(NlogN). */
4570 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4571 sorted_sym_hash = bfd_malloc (amt);
4572 if (sorted_sym_hash == NULL)
4573 goto error_return;
4574 sym_hash = sorted_sym_hash;
4575 hpp = elf_sym_hashes (abfd);
4576 hppend = hpp + extsymcount;
4577 sym_count = 0;
4578 for (; hpp < hppend; hpp++)
4579 {
4580 h = *hpp;
4581 if (h != NULL
4582 && h->root.type == bfd_link_hash_defined
fcb93ecf 4583 && !bed->is_function_type (h->type))
4ad4eba5
AM
4584 {
4585 *sym_hash = h;
4586 sym_hash++;
4587 sym_count++;
4588 }
4589 }
4590
4591 qsort (sorted_sym_hash, sym_count,
4592 sizeof (struct elf_link_hash_entry *),
4593 elf_sort_symbol);
4594
4595 while (weaks != NULL)
4596 {
4597 struct elf_link_hash_entry *hlook;
4598 asection *slook;
4599 bfd_vma vlook;
4600 long ilook;
4601 size_t i, j, idx;
4602
4603 hlook = weaks;
f6e332e6
AM
4604 weaks = hlook->u.weakdef;
4605 hlook->u.weakdef = NULL;
4ad4eba5
AM
4606
4607 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4608 || hlook->root.type == bfd_link_hash_defweak
4609 || hlook->root.type == bfd_link_hash_common
4610 || hlook->root.type == bfd_link_hash_indirect);
4611 slook = hlook->root.u.def.section;
4612 vlook = hlook->root.u.def.value;
4613
4614 ilook = -1;
4615 i = 0;
4616 j = sym_count;
4617 while (i < j)
4618 {
4619 bfd_signed_vma vdiff;
4620 idx = (i + j) / 2;
4621 h = sorted_sym_hash [idx];
4622 vdiff = vlook - h->root.u.def.value;
4623 if (vdiff < 0)
4624 j = idx;
4625 else if (vdiff > 0)
4626 i = idx + 1;
4627 else
4628 {
a9b881be 4629 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4630 if (sdiff < 0)
4631 j = idx;
4632 else if (sdiff > 0)
4633 i = idx + 1;
4634 else
4635 {
4636 ilook = idx;
4637 break;
4638 }
4639 }
4640 }
4641
4642 /* We didn't find a value/section match. */
4643 if (ilook == -1)
4644 continue;
4645
4646 for (i = ilook; i < sym_count; i++)
4647 {
4648 h = sorted_sym_hash [i];
4649
4650 /* Stop if value or section doesn't match. */
4651 if (h->root.u.def.value != vlook
4652 || h->root.u.def.section != slook)
4653 break;
4654 else if (h != hlook)
4655 {
f6e332e6 4656 hlook->u.weakdef = h;
4ad4eba5
AM
4657
4658 /* If the weak definition is in the list of dynamic
4659 symbols, make sure the real definition is put
4660 there as well. */
4661 if (hlook->dynindx != -1 && h->dynindx == -1)
4662 {
c152c796 4663 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4664 {
4665 err_free_sym_hash:
4666 free (sorted_sym_hash);
4667 goto error_return;
4668 }
4ad4eba5
AM
4669 }
4670
4671 /* If the real definition is in the list of dynamic
4672 symbols, make sure the weak definition is put
4673 there as well. If we don't do this, then the
4674 dynamic loader might not merge the entries for the
4675 real definition and the weak definition. */
4676 if (h->dynindx != -1 && hlook->dynindx == -1)
4677 {
c152c796 4678 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4679 goto err_free_sym_hash;
4ad4eba5
AM
4680 }
4681 break;
4682 }
4683 }
4684 }
4685
4686 free (sorted_sym_hash);
4687 }
4688
33177bb1
AM
4689 if (bed->check_directives
4690 && !(*bed->check_directives) (abfd, info))
4691 return FALSE;
85fbca6a 4692
4ad4eba5
AM
4693 /* If this object is the same format as the output object, and it is
4694 not a shared library, then let the backend look through the
4695 relocs.
4696
4697 This is required to build global offset table entries and to
4698 arrange for dynamic relocs. It is not required for the
4699 particular common case of linking non PIC code, even when linking
4700 against shared libraries, but unfortunately there is no way of
4701 knowing whether an object file has been compiled PIC or not.
4702 Looking through the relocs is not particularly time consuming.
4703 The problem is that we must either (1) keep the relocs in memory,
4704 which causes the linker to require additional runtime memory or
4705 (2) read the relocs twice from the input file, which wastes time.
4706 This would be a good case for using mmap.
4707
4708 I have no idea how to handle linking PIC code into a file of a
4709 different format. It probably can't be done. */
4ad4eba5 4710 if (! dynamic
66eb6687 4711 && is_elf_hash_table (htab)
13285a1b 4712 && bed->check_relocs != NULL
f13a99db 4713 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4714 {
4715 asection *o;
4716
4717 for (o = abfd->sections; o != NULL; o = o->next)
4718 {
4719 Elf_Internal_Rela *internal_relocs;
4720 bfd_boolean ok;
4721
4722 if ((o->flags & SEC_RELOC) == 0
4723 || o->reloc_count == 0
4724 || ((info->strip == strip_all || info->strip == strip_debugger)
4725 && (o->flags & SEC_DEBUGGING) != 0)
4726 || bfd_is_abs_section (o->output_section))
4727 continue;
4728
4729 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4730 info->keep_memory);
4731 if (internal_relocs == NULL)
4732 goto error_return;
4733
66eb6687 4734 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4735
4736 if (elf_section_data (o)->relocs != internal_relocs)
4737 free (internal_relocs);
4738
4739 if (! ok)
4740 goto error_return;
4741 }
4742 }
4743
4744 /* If this is a non-traditional link, try to optimize the handling
4745 of the .stab/.stabstr sections. */
4746 if (! dynamic
4747 && ! info->traditional_format
66eb6687 4748 && is_elf_hash_table (htab)
4ad4eba5
AM
4749 && (info->strip != strip_all && info->strip != strip_debugger))
4750 {
4751 asection *stabstr;
4752
4753 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4754 if (stabstr != NULL)
4755 {
4756 bfd_size_type string_offset = 0;
4757 asection *stab;
4758
4759 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4760 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4761 && (!stab->name[5] ||
4762 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4763 && (stab->flags & SEC_MERGE) == 0
4764 && !bfd_is_abs_section (stab->output_section))
4765 {
4766 struct bfd_elf_section_data *secdata;
4767
4768 secdata = elf_section_data (stab);
66eb6687
AM
4769 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4770 stabstr, &secdata->sec_info,
4ad4eba5
AM
4771 &string_offset))
4772 goto error_return;
4773 if (secdata->sec_info)
4774 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4775 }
4776 }
4777 }
4778
66eb6687 4779 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4780 {
4781 /* Add this bfd to the loaded list. */
4782 struct elf_link_loaded_list *n;
4783
4784 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4785 if (n == NULL)
4786 goto error_return;
4787 n->abfd = abfd;
66eb6687
AM
4788 n->next = htab->loaded;
4789 htab->loaded = n;
4ad4eba5
AM
4790 }
4791
4792 return TRUE;
4793
4794 error_free_vers:
66eb6687
AM
4795 if (old_tab != NULL)
4796 free (old_tab);
4ad4eba5
AM
4797 if (nondeflt_vers != NULL)
4798 free (nondeflt_vers);
4799 if (extversym != NULL)
4800 free (extversym);
4801 error_free_sym:
4802 if (isymbuf != NULL)
4803 free (isymbuf);
4804 error_return:
4805 return FALSE;
4806}
4807
8387904d
AM
4808/* Return the linker hash table entry of a symbol that might be
4809 satisfied by an archive symbol. Return -1 on error. */
4810
4811struct elf_link_hash_entry *
4812_bfd_elf_archive_symbol_lookup (bfd *abfd,
4813 struct bfd_link_info *info,
4814 const char *name)
4815{
4816 struct elf_link_hash_entry *h;
4817 char *p, *copy;
4818 size_t len, first;
4819
4820 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4821 if (h != NULL)
4822 return h;
4823
4824 /* If this is a default version (the name contains @@), look up the
4825 symbol again with only one `@' as well as without the version.
4826 The effect is that references to the symbol with and without the
4827 version will be matched by the default symbol in the archive. */
4828
4829 p = strchr (name, ELF_VER_CHR);
4830 if (p == NULL || p[1] != ELF_VER_CHR)
4831 return h;
4832
4833 /* First check with only one `@'. */
4834 len = strlen (name);
4835 copy = bfd_alloc (abfd, len);
4836 if (copy == NULL)
4837 return (struct elf_link_hash_entry *) 0 - 1;
4838
4839 first = p - name + 1;
4840 memcpy (copy, name, first);
4841 memcpy (copy + first, name + first + 1, len - first);
4842
4843 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4844 if (h == NULL)
4845 {
4846 /* We also need to check references to the symbol without the
4847 version. */
4848 copy[first - 1] = '\0';
4849 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4850 FALSE, FALSE, FALSE);
4851 }
4852
4853 bfd_release (abfd, copy);
4854 return h;
4855}
4856
0ad989f9
L
4857/* Add symbols from an ELF archive file to the linker hash table. We
4858 don't use _bfd_generic_link_add_archive_symbols because of a
4859 problem which arises on UnixWare. The UnixWare libc.so is an
4860 archive which includes an entry libc.so.1 which defines a bunch of
4861 symbols. The libc.so archive also includes a number of other
4862 object files, which also define symbols, some of which are the same
4863 as those defined in libc.so.1. Correct linking requires that we
4864 consider each object file in turn, and include it if it defines any
4865 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4866 this; it looks through the list of undefined symbols, and includes
4867 any object file which defines them. When this algorithm is used on
4868 UnixWare, it winds up pulling in libc.so.1 early and defining a
4869 bunch of symbols. This means that some of the other objects in the
4870 archive are not included in the link, which is incorrect since they
4871 precede libc.so.1 in the archive.
4872
4873 Fortunately, ELF archive handling is simpler than that done by
4874 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4875 oddities. In ELF, if we find a symbol in the archive map, and the
4876 symbol is currently undefined, we know that we must pull in that
4877 object file.
4878
4879 Unfortunately, we do have to make multiple passes over the symbol
4880 table until nothing further is resolved. */
4881
4ad4eba5
AM
4882static bfd_boolean
4883elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4884{
4885 symindex c;
4886 bfd_boolean *defined = NULL;
4887 bfd_boolean *included = NULL;
4888 carsym *symdefs;
4889 bfd_boolean loop;
4890 bfd_size_type amt;
8387904d
AM
4891 const struct elf_backend_data *bed;
4892 struct elf_link_hash_entry * (*archive_symbol_lookup)
4893 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4894
4895 if (! bfd_has_map (abfd))
4896 {
4897 /* An empty archive is a special case. */
4898 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4899 return TRUE;
4900 bfd_set_error (bfd_error_no_armap);
4901 return FALSE;
4902 }
4903
4904 /* Keep track of all symbols we know to be already defined, and all
4905 files we know to be already included. This is to speed up the
4906 second and subsequent passes. */
4907 c = bfd_ardata (abfd)->symdef_count;
4908 if (c == 0)
4909 return TRUE;
4910 amt = c;
4911 amt *= sizeof (bfd_boolean);
4912 defined = bfd_zmalloc (amt);
4913 included = bfd_zmalloc (amt);
4914 if (defined == NULL || included == NULL)
4915 goto error_return;
4916
4917 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4918 bed = get_elf_backend_data (abfd);
4919 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4920
4921 do
4922 {
4923 file_ptr last;
4924 symindex i;
4925 carsym *symdef;
4926 carsym *symdefend;
4927
4928 loop = FALSE;
4929 last = -1;
4930
4931 symdef = symdefs;
4932 symdefend = symdef + c;
4933 for (i = 0; symdef < symdefend; symdef++, i++)
4934 {
4935 struct elf_link_hash_entry *h;
4936 bfd *element;
4937 struct bfd_link_hash_entry *undefs_tail;
4938 symindex mark;
4939
4940 if (defined[i] || included[i])
4941 continue;
4942 if (symdef->file_offset == last)
4943 {
4944 included[i] = TRUE;
4945 continue;
4946 }
4947
8387904d
AM
4948 h = archive_symbol_lookup (abfd, info, symdef->name);
4949 if (h == (struct elf_link_hash_entry *) 0 - 1)
4950 goto error_return;
0ad989f9
L
4951
4952 if (h == NULL)
4953 continue;
4954
4955 if (h->root.type == bfd_link_hash_common)
4956 {
4957 /* We currently have a common symbol. The archive map contains
4958 a reference to this symbol, so we may want to include it. We
4959 only want to include it however, if this archive element
4960 contains a definition of the symbol, not just another common
4961 declaration of it.
4962
4963 Unfortunately some archivers (including GNU ar) will put
4964 declarations of common symbols into their archive maps, as
4965 well as real definitions, so we cannot just go by the archive
4966 map alone. Instead we must read in the element's symbol
4967 table and check that to see what kind of symbol definition
4968 this is. */
4969 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4970 continue;
4971 }
4972 else if (h->root.type != bfd_link_hash_undefined)
4973 {
4974 if (h->root.type != bfd_link_hash_undefweak)
4975 defined[i] = TRUE;
4976 continue;
4977 }
4978
4979 /* We need to include this archive member. */
4980 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4981 if (element == NULL)
4982 goto error_return;
4983
4984 if (! bfd_check_format (element, bfd_object))
4985 goto error_return;
4986
4987 /* Doublecheck that we have not included this object
4988 already--it should be impossible, but there may be
4989 something wrong with the archive. */
4990 if (element->archive_pass != 0)
4991 {
4992 bfd_set_error (bfd_error_bad_value);
4993 goto error_return;
4994 }
4995 element->archive_pass = 1;
4996
4997 undefs_tail = info->hash->undefs_tail;
4998
4999 if (! (*info->callbacks->add_archive_element) (info, element,
5000 symdef->name))
5001 goto error_return;
5002 if (! bfd_link_add_symbols (element, info))
5003 goto error_return;
5004
5005 /* If there are any new undefined symbols, we need to make
5006 another pass through the archive in order to see whether
5007 they can be defined. FIXME: This isn't perfect, because
5008 common symbols wind up on undefs_tail and because an
5009 undefined symbol which is defined later on in this pass
5010 does not require another pass. This isn't a bug, but it
5011 does make the code less efficient than it could be. */
5012 if (undefs_tail != info->hash->undefs_tail)
5013 loop = TRUE;
5014
5015 /* Look backward to mark all symbols from this object file
5016 which we have already seen in this pass. */
5017 mark = i;
5018 do
5019 {
5020 included[mark] = TRUE;
5021 if (mark == 0)
5022 break;
5023 --mark;
5024 }
5025 while (symdefs[mark].file_offset == symdef->file_offset);
5026
5027 /* We mark subsequent symbols from this object file as we go
5028 on through the loop. */
5029 last = symdef->file_offset;
5030 }
5031 }
5032 while (loop);
5033
5034 free (defined);
5035 free (included);
5036
5037 return TRUE;
5038
5039 error_return:
5040 if (defined != NULL)
5041 free (defined);
5042 if (included != NULL)
5043 free (included);
5044 return FALSE;
5045}
4ad4eba5
AM
5046
5047/* Given an ELF BFD, add symbols to the global hash table as
5048 appropriate. */
5049
5050bfd_boolean
5051bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5052{
5053 switch (bfd_get_format (abfd))
5054 {
5055 case bfd_object:
5056 return elf_link_add_object_symbols (abfd, info);
5057 case bfd_archive:
5058 return elf_link_add_archive_symbols (abfd, info);
5059 default:
5060 bfd_set_error (bfd_error_wrong_format);
5061 return FALSE;
5062 }
5063}
5a580b3a 5064\f
14b1c01e
AM
5065struct hash_codes_info
5066{
5067 unsigned long *hashcodes;
5068 bfd_boolean error;
5069};
a0c8462f 5070
5a580b3a
AM
5071/* This function will be called though elf_link_hash_traverse to store
5072 all hash value of the exported symbols in an array. */
5073
5074static bfd_boolean
5075elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5076{
14b1c01e 5077 struct hash_codes_info *inf = data;
5a580b3a
AM
5078 const char *name;
5079 char *p;
5080 unsigned long ha;
5081 char *alc = NULL;
5082
5083 if (h->root.type == bfd_link_hash_warning)
5084 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5085
5086 /* Ignore indirect symbols. These are added by the versioning code. */
5087 if (h->dynindx == -1)
5088 return TRUE;
5089
5090 name = h->root.root.string;
5091 p = strchr (name, ELF_VER_CHR);
5092 if (p != NULL)
5093 {
5094 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5095 if (alc == NULL)
5096 {
5097 inf->error = TRUE;
5098 return FALSE;
5099 }
5a580b3a
AM
5100 memcpy (alc, name, p - name);
5101 alc[p - name] = '\0';
5102 name = alc;
5103 }
5104
5105 /* Compute the hash value. */
5106 ha = bfd_elf_hash (name);
5107
5108 /* Store the found hash value in the array given as the argument. */
14b1c01e 5109 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5110
5111 /* And store it in the struct so that we can put it in the hash table
5112 later. */
f6e332e6 5113 h->u.elf_hash_value = ha;
5a580b3a
AM
5114
5115 if (alc != NULL)
5116 free (alc);
5117
5118 return TRUE;
5119}
5120
fdc90cb4
JJ
5121struct collect_gnu_hash_codes
5122{
5123 bfd *output_bfd;
5124 const struct elf_backend_data *bed;
5125 unsigned long int nsyms;
5126 unsigned long int maskbits;
5127 unsigned long int *hashcodes;
5128 unsigned long int *hashval;
5129 unsigned long int *indx;
5130 unsigned long int *counts;
5131 bfd_vma *bitmask;
5132 bfd_byte *contents;
5133 long int min_dynindx;
5134 unsigned long int bucketcount;
5135 unsigned long int symindx;
5136 long int local_indx;
5137 long int shift1, shift2;
5138 unsigned long int mask;
14b1c01e 5139 bfd_boolean error;
fdc90cb4
JJ
5140};
5141
5142/* This function will be called though elf_link_hash_traverse to store
5143 all hash value of the exported symbols in an array. */
5144
5145static bfd_boolean
5146elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5147{
5148 struct collect_gnu_hash_codes *s = data;
5149 const char *name;
5150 char *p;
5151 unsigned long ha;
5152 char *alc = NULL;
5153
5154 if (h->root.type == bfd_link_hash_warning)
5155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5156
5157 /* Ignore indirect symbols. These are added by the versioning code. */
5158 if (h->dynindx == -1)
5159 return TRUE;
5160
5161 /* Ignore also local symbols and undefined symbols. */
5162 if (! (*s->bed->elf_hash_symbol) (h))
5163 return TRUE;
5164
5165 name = h->root.root.string;
5166 p = strchr (name, ELF_VER_CHR);
5167 if (p != NULL)
5168 {
5169 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5170 if (alc == NULL)
5171 {
5172 s->error = TRUE;
5173 return FALSE;
5174 }
fdc90cb4
JJ
5175 memcpy (alc, name, p - name);
5176 alc[p - name] = '\0';
5177 name = alc;
5178 }
5179
5180 /* Compute the hash value. */
5181 ha = bfd_elf_gnu_hash (name);
5182
5183 /* Store the found hash value in the array for compute_bucket_count,
5184 and also for .dynsym reordering purposes. */
5185 s->hashcodes[s->nsyms] = ha;
5186 s->hashval[h->dynindx] = ha;
5187 ++s->nsyms;
5188 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5189 s->min_dynindx = h->dynindx;
5190
5191 if (alc != NULL)
5192 free (alc);
5193
5194 return TRUE;
5195}
5196
5197/* This function will be called though elf_link_hash_traverse to do
5198 final dynaminc symbol renumbering. */
5199
5200static bfd_boolean
5201elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5202{
5203 struct collect_gnu_hash_codes *s = data;
5204 unsigned long int bucket;
5205 unsigned long int val;
5206
5207 if (h->root.type == bfd_link_hash_warning)
5208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5209
5210 /* Ignore indirect symbols. */
5211 if (h->dynindx == -1)
5212 return TRUE;
5213
5214 /* Ignore also local symbols and undefined symbols. */
5215 if (! (*s->bed->elf_hash_symbol) (h))
5216 {
5217 if (h->dynindx >= s->min_dynindx)
5218 h->dynindx = s->local_indx++;
5219 return TRUE;
5220 }
5221
5222 bucket = s->hashval[h->dynindx] % s->bucketcount;
5223 val = (s->hashval[h->dynindx] >> s->shift1)
5224 & ((s->maskbits >> s->shift1) - 1);
5225 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5226 s->bitmask[val]
5227 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5228 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5229 if (s->counts[bucket] == 1)
5230 /* Last element terminates the chain. */
5231 val |= 1;
5232 bfd_put_32 (s->output_bfd, val,
5233 s->contents + (s->indx[bucket] - s->symindx) * 4);
5234 --s->counts[bucket];
5235 h->dynindx = s->indx[bucket]++;
5236 return TRUE;
5237}
5238
5239/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5240
5241bfd_boolean
5242_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5243{
5244 return !(h->forced_local
5245 || h->root.type == bfd_link_hash_undefined
5246 || h->root.type == bfd_link_hash_undefweak
5247 || ((h->root.type == bfd_link_hash_defined
5248 || h->root.type == bfd_link_hash_defweak)
5249 && h->root.u.def.section->output_section == NULL));
5250}
5251
5a580b3a
AM
5252/* Array used to determine the number of hash table buckets to use
5253 based on the number of symbols there are. If there are fewer than
5254 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5255 fewer than 37 we use 17 buckets, and so forth. We never use more
5256 than 32771 buckets. */
5257
5258static const size_t elf_buckets[] =
5259{
5260 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5261 16411, 32771, 0
5262};
5263
5264/* Compute bucket count for hashing table. We do not use a static set
5265 of possible tables sizes anymore. Instead we determine for all
5266 possible reasonable sizes of the table the outcome (i.e., the
5267 number of collisions etc) and choose the best solution. The
5268 weighting functions are not too simple to allow the table to grow
5269 without bounds. Instead one of the weighting factors is the size.
5270 Therefore the result is always a good payoff between few collisions
5271 (= short chain lengths) and table size. */
5272static size_t
d40f3da9
AM
5273compute_bucket_count (struct bfd_link_info *info,
5274 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5275 unsigned long int nsyms,
5276 int gnu_hash)
5a580b3a 5277{
5a580b3a 5278 size_t best_size = 0;
5a580b3a 5279 unsigned long int i;
5a580b3a 5280
5a580b3a
AM
5281 /* We have a problem here. The following code to optimize the table
5282 size requires an integer type with more the 32 bits. If
5283 BFD_HOST_U_64_BIT is set we know about such a type. */
5284#ifdef BFD_HOST_U_64_BIT
5285 if (info->optimize)
5286 {
5a580b3a
AM
5287 size_t minsize;
5288 size_t maxsize;
5289 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5290 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5291 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5292 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5293 unsigned long int *counts;
d40f3da9 5294 bfd_size_type amt;
5a580b3a
AM
5295
5296 /* Possible optimization parameters: if we have NSYMS symbols we say
5297 that the hashing table must at least have NSYMS/4 and at most
5298 2*NSYMS buckets. */
5299 minsize = nsyms / 4;
5300 if (minsize == 0)
5301 minsize = 1;
5302 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5303 if (gnu_hash)
5304 {
5305 if (minsize < 2)
5306 minsize = 2;
5307 if ((best_size & 31) == 0)
5308 ++best_size;
5309 }
5a580b3a
AM
5310
5311 /* Create array where we count the collisions in. We must use bfd_malloc
5312 since the size could be large. */
5313 amt = maxsize;
5314 amt *= sizeof (unsigned long int);
5315 counts = bfd_malloc (amt);
5316 if (counts == NULL)
fdc90cb4 5317 return 0;
5a580b3a
AM
5318
5319 /* Compute the "optimal" size for the hash table. The criteria is a
5320 minimal chain length. The minor criteria is (of course) the size
5321 of the table. */
5322 for (i = minsize; i < maxsize; ++i)
5323 {
5324 /* Walk through the array of hashcodes and count the collisions. */
5325 BFD_HOST_U_64_BIT max;
5326 unsigned long int j;
5327 unsigned long int fact;
5328
fdc90cb4
JJ
5329 if (gnu_hash && (i & 31) == 0)
5330 continue;
5331
5a580b3a
AM
5332 memset (counts, '\0', i * sizeof (unsigned long int));
5333
5334 /* Determine how often each hash bucket is used. */
5335 for (j = 0; j < nsyms; ++j)
5336 ++counts[hashcodes[j] % i];
5337
5338 /* For the weight function we need some information about the
5339 pagesize on the target. This is information need not be 100%
5340 accurate. Since this information is not available (so far) we
5341 define it here to a reasonable default value. If it is crucial
5342 to have a better value some day simply define this value. */
5343# ifndef BFD_TARGET_PAGESIZE
5344# define BFD_TARGET_PAGESIZE (4096)
5345# endif
5346
fdc90cb4
JJ
5347 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5348 and the chains. */
5349 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5350
5351# if 1
5352 /* Variant 1: optimize for short chains. We add the squares
5353 of all the chain lengths (which favors many small chain
5354 over a few long chains). */
5355 for (j = 0; j < i; ++j)
5356 max += counts[j] * counts[j];
5357
5358 /* This adds penalties for the overall size of the table. */
fdc90cb4 5359 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5360 max *= fact * fact;
5361# else
5362 /* Variant 2: Optimize a lot more for small table. Here we
5363 also add squares of the size but we also add penalties for
5364 empty slots (the +1 term). */
5365 for (j = 0; j < i; ++j)
5366 max += (1 + counts[j]) * (1 + counts[j]);
5367
5368 /* The overall size of the table is considered, but not as
5369 strong as in variant 1, where it is squared. */
fdc90cb4 5370 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5371 max *= fact;
5372# endif
5373
5374 /* Compare with current best results. */
5375 if (max < best_chlen)
5376 {
5377 best_chlen = max;
5378 best_size = i;
5379 }
5380 }
5381
5382 free (counts);
5383 }
5384 else
5385#endif /* defined (BFD_HOST_U_64_BIT) */
5386 {
5387 /* This is the fallback solution if no 64bit type is available or if we
5388 are not supposed to spend much time on optimizations. We select the
5389 bucket count using a fixed set of numbers. */
5390 for (i = 0; elf_buckets[i] != 0; i++)
5391 {
5392 best_size = elf_buckets[i];
fdc90cb4 5393 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5394 break;
5395 }
fdc90cb4
JJ
5396 if (gnu_hash && best_size < 2)
5397 best_size = 2;
5a580b3a
AM
5398 }
5399
5a580b3a
AM
5400 return best_size;
5401}
5402
5403/* Set up the sizes and contents of the ELF dynamic sections. This is
5404 called by the ELF linker emulation before_allocation routine. We
5405 must set the sizes of the sections before the linker sets the
5406 addresses of the various sections. */
5407
5408bfd_boolean
5409bfd_elf_size_dynamic_sections (bfd *output_bfd,
5410 const char *soname,
5411 const char *rpath,
5412 const char *filter_shlib,
5413 const char * const *auxiliary_filters,
5414 struct bfd_link_info *info,
5415 asection **sinterpptr,
5416 struct bfd_elf_version_tree *verdefs)
5417{
5418 bfd_size_type soname_indx;
5419 bfd *dynobj;
5420 const struct elf_backend_data *bed;
5421 struct elf_assign_sym_version_info asvinfo;
5422
5423 *sinterpptr = NULL;
5424
5425 soname_indx = (bfd_size_type) -1;
5426
5427 if (!is_elf_hash_table (info->hash))
5428 return TRUE;
5429
6bfdb61b 5430 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5431 if (info->execstack)
5432 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5433 else if (info->noexecstack)
5434 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5435 else
5436 {
5437 bfd *inputobj;
5438 asection *notesec = NULL;
5439 int exec = 0;
5440
5441 for (inputobj = info->input_bfds;
5442 inputobj;
5443 inputobj = inputobj->link_next)
5444 {
5445 asection *s;
5446
a94b9d2d 5447 if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5a580b3a
AM
5448 continue;
5449 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5450 if (s)
5451 {
5452 if (s->flags & SEC_CODE)
5453 exec = PF_X;
5454 notesec = s;
5455 }
6bfdb61b 5456 else if (bed->default_execstack)
5a580b3a
AM
5457 exec = PF_X;
5458 }
5459 if (notesec)
5460 {
5461 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5462 if (exec && info->relocatable
5463 && notesec->output_section != bfd_abs_section_ptr)
5464 notesec->output_section->flags |= SEC_CODE;
5465 }
5466 }
5467
5468 /* Any syms created from now on start with -1 in
5469 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5470 elf_hash_table (info)->init_got_refcount
5471 = elf_hash_table (info)->init_got_offset;
5472 elf_hash_table (info)->init_plt_refcount
5473 = elf_hash_table (info)->init_plt_offset;
5a580b3a
AM
5474
5475 /* The backend may have to create some sections regardless of whether
5476 we're dynamic or not. */
5a580b3a
AM
5477 if (bed->elf_backend_always_size_sections
5478 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5479 return FALSE;
5480
eb3d5f3b
JB
5481 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5482 return FALSE;
5483
5a580b3a
AM
5484 dynobj = elf_hash_table (info)->dynobj;
5485
5486 /* If there were no dynamic objects in the link, there is nothing to
5487 do here. */
5488 if (dynobj == NULL)
5489 return TRUE;
5490
5a580b3a
AM
5491 if (elf_hash_table (info)->dynamic_sections_created)
5492 {
5493 struct elf_info_failed eif;
5494 struct elf_link_hash_entry *h;
5495 asection *dynstr;
5496 struct bfd_elf_version_tree *t;
5497 struct bfd_elf_version_expr *d;
046183de 5498 asection *s;
5a580b3a
AM
5499 bfd_boolean all_defined;
5500
5501 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5502 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5503
5504 if (soname != NULL)
5505 {
5506 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5507 soname, TRUE);
5508 if (soname_indx == (bfd_size_type) -1
5509 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5510 return FALSE;
5511 }
5512
5513 if (info->symbolic)
5514 {
5515 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5516 return FALSE;
5517 info->flags |= DF_SYMBOLIC;
5518 }
5519
5520 if (rpath != NULL)
5521 {
5522 bfd_size_type indx;
5523
5524 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5525 TRUE);
5526 if (indx == (bfd_size_type) -1
5527 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5528 return FALSE;
5529
5530 if (info->new_dtags)
5531 {
5532 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5533 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5534 return FALSE;
5535 }
5536 }
5537
5538 if (filter_shlib != NULL)
5539 {
5540 bfd_size_type indx;
5541
5542 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5543 filter_shlib, TRUE);
5544 if (indx == (bfd_size_type) -1
5545 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5546 return FALSE;
5547 }
5548
5549 if (auxiliary_filters != NULL)
5550 {
5551 const char * const *p;
5552
5553 for (p = auxiliary_filters; *p != NULL; p++)
5554 {
5555 bfd_size_type indx;
5556
5557 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5558 *p, TRUE);
5559 if (indx == (bfd_size_type) -1
5560 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5561 return FALSE;
5562 }
5563 }
5564
5565 eif.info = info;
5566 eif.verdefs = verdefs;
5567 eif.failed = FALSE;
5568
5569 /* If we are supposed to export all symbols into the dynamic symbol
5570 table (this is not the normal case), then do so. */
55255dae
L
5571 if (info->export_dynamic
5572 || (info->executable && info->dynamic))
5a580b3a
AM
5573 {
5574 elf_link_hash_traverse (elf_hash_table (info),
5575 _bfd_elf_export_symbol,
5576 &eif);
5577 if (eif.failed)
5578 return FALSE;
5579 }
5580
5581 /* Make all global versions with definition. */
5582 for (t = verdefs; t != NULL; t = t->next)
5583 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5584 if (!d->symver && d->literal)
5a580b3a
AM
5585 {
5586 const char *verstr, *name;
5587 size_t namelen, verlen, newlen;
5588 char *newname, *p;
5589 struct elf_link_hash_entry *newh;
5590
ae5a3597 5591 name = d->pattern;
5a580b3a
AM
5592 namelen = strlen (name);
5593 verstr = t->name;
5594 verlen = strlen (verstr);
5595 newlen = namelen + verlen + 3;
5596
5597 newname = bfd_malloc (newlen);
5598 if (newname == NULL)
5599 return FALSE;
5600 memcpy (newname, name, namelen);
5601
5602 /* Check the hidden versioned definition. */
5603 p = newname + namelen;
5604 *p++ = ELF_VER_CHR;
5605 memcpy (p, verstr, verlen + 1);
5606 newh = elf_link_hash_lookup (elf_hash_table (info),
5607 newname, FALSE, FALSE,
5608 FALSE);
5609 if (newh == NULL
5610 || (newh->root.type != bfd_link_hash_defined
5611 && newh->root.type != bfd_link_hash_defweak))
5612 {
5613 /* Check the default versioned definition. */
5614 *p++ = ELF_VER_CHR;
5615 memcpy (p, verstr, verlen + 1);
5616 newh = elf_link_hash_lookup (elf_hash_table (info),
5617 newname, FALSE, FALSE,
5618 FALSE);
5619 }
5620 free (newname);
5621
5622 /* Mark this version if there is a definition and it is
5623 not defined in a shared object. */
5624 if (newh != NULL
f5385ebf 5625 && !newh->def_dynamic
5a580b3a
AM
5626 && (newh->root.type == bfd_link_hash_defined
5627 || newh->root.type == bfd_link_hash_defweak))
5628 d->symver = 1;
5629 }
5630
5631 /* Attach all the symbols to their version information. */
5632 asvinfo.output_bfd = output_bfd;
5633 asvinfo.info = info;
5634 asvinfo.verdefs = verdefs;
5635 asvinfo.failed = FALSE;
5636
5637 elf_link_hash_traverse (elf_hash_table (info),
5638 _bfd_elf_link_assign_sym_version,
5639 &asvinfo);
5640 if (asvinfo.failed)
5641 return FALSE;
5642
5643 if (!info->allow_undefined_version)
5644 {
5645 /* Check if all global versions have a definition. */
5646 all_defined = TRUE;
5647 for (t = verdefs; t != NULL; t = t->next)
5648 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5649 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5650 {
5651 (*_bfd_error_handler)
5652 (_("%s: undefined version: %s"),
5653 d->pattern, t->name);
5654 all_defined = FALSE;
5655 }
5656
5657 if (!all_defined)
5658 {
5659 bfd_set_error (bfd_error_bad_value);
5660 return FALSE;
5661 }
5662 }
5663
5664 /* Find all symbols which were defined in a dynamic object and make
5665 the backend pick a reasonable value for them. */
5666 elf_link_hash_traverse (elf_hash_table (info),
5667 _bfd_elf_adjust_dynamic_symbol,
5668 &eif);
5669 if (eif.failed)
5670 return FALSE;
5671
5672 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5673 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5674 now so that we know the final size of the .dynamic section. */
5675
5676 /* If there are initialization and/or finalization functions to
5677 call then add the corresponding DT_INIT/DT_FINI entries. */
5678 h = (info->init_function
5679 ? elf_link_hash_lookup (elf_hash_table (info),
5680 info->init_function, FALSE,
5681 FALSE, FALSE)
5682 : NULL);
5683 if (h != NULL
f5385ebf
AM
5684 && (h->ref_regular
5685 || h->def_regular))
5a580b3a
AM
5686 {
5687 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5688 return FALSE;
5689 }
5690 h = (info->fini_function
5691 ? elf_link_hash_lookup (elf_hash_table (info),
5692 info->fini_function, FALSE,
5693 FALSE, FALSE)
5694 : NULL);
5695 if (h != NULL
f5385ebf
AM
5696 && (h->ref_regular
5697 || h->def_regular))
5a580b3a
AM
5698 {
5699 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5700 return FALSE;
5701 }
5702
046183de
AM
5703 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5704 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5705 {
5706 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5707 if (! info->executable)
5708 {
5709 bfd *sub;
5710 asection *o;
5711
5712 for (sub = info->input_bfds; sub != NULL;
5713 sub = sub->link_next)
3fcd97f1
JJ
5714 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5715 for (o = sub->sections; o != NULL; o = o->next)
5716 if (elf_section_data (o)->this_hdr.sh_type
5717 == SHT_PREINIT_ARRAY)
5718 {
5719 (*_bfd_error_handler)
5720 (_("%B: .preinit_array section is not allowed in DSO"),
5721 sub);
5722 break;
5723 }
5a580b3a
AM
5724
5725 bfd_set_error (bfd_error_nonrepresentable_section);
5726 return FALSE;
5727 }
5728
5729 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5730 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5731 return FALSE;
5732 }
046183de
AM
5733 s = bfd_get_section_by_name (output_bfd, ".init_array");
5734 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5735 {
5736 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5737 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5738 return FALSE;
5739 }
046183de
AM
5740 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5741 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5742 {
5743 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5744 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5745 return FALSE;
5746 }
5747
5748 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5749 /* If .dynstr is excluded from the link, we don't want any of
5750 these tags. Strictly, we should be checking each section
5751 individually; This quick check covers for the case where
5752 someone does a /DISCARD/ : { *(*) }. */
5753 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5754 {
5755 bfd_size_type strsize;
5756
5757 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5758 if ((info->emit_hash
5759 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5760 || (info->emit_gnu_hash
5761 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5762 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5763 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5764 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5765 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5766 bed->s->sizeof_sym))
5767 return FALSE;
5768 }
5769 }
5770
5771 /* The backend must work out the sizes of all the other dynamic
5772 sections. */
5773 if (bed->elf_backend_size_dynamic_sections
5774 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5775 return FALSE;
5776
5777 if (elf_hash_table (info)->dynamic_sections_created)
5778 {
554220db 5779 unsigned long section_sym_count;
5a580b3a 5780 asection *s;
5a580b3a
AM
5781
5782 /* Set up the version definition section. */
5783 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5784 BFD_ASSERT (s != NULL);
5785
5786 /* We may have created additional version definitions if we are
5787 just linking a regular application. */
5788 verdefs = asvinfo.verdefs;
5789
5790 /* Skip anonymous version tag. */
5791 if (verdefs != NULL && verdefs->vernum == 0)
5792 verdefs = verdefs->next;
5793
3e3b46e5 5794 if (verdefs == NULL && !info->create_default_symver)
8423293d 5795 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5796 else
5797 {
5798 unsigned int cdefs;
5799 bfd_size_type size;
5800 struct bfd_elf_version_tree *t;
5801 bfd_byte *p;
5802 Elf_Internal_Verdef def;
5803 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5804 struct bfd_link_hash_entry *bh;
5805 struct elf_link_hash_entry *h;
5806 const char *name;
5a580b3a
AM
5807
5808 cdefs = 0;
5809 size = 0;
5810
5811 /* Make space for the base version. */
5812 size += sizeof (Elf_External_Verdef);
5813 size += sizeof (Elf_External_Verdaux);
5814 ++cdefs;
5815
3e3b46e5
PB
5816 /* Make space for the default version. */
5817 if (info->create_default_symver)
5818 {
5819 size += sizeof (Elf_External_Verdef);
5820 ++cdefs;
5821 }
5822
5a580b3a
AM
5823 for (t = verdefs; t != NULL; t = t->next)
5824 {
5825 struct bfd_elf_version_deps *n;
5826
5827 size += sizeof (Elf_External_Verdef);
5828 size += sizeof (Elf_External_Verdaux);
5829 ++cdefs;
5830
5831 for (n = t->deps; n != NULL; n = n->next)
5832 size += sizeof (Elf_External_Verdaux);
5833 }
5834
eea6121a
AM
5835 s->size = size;
5836 s->contents = bfd_alloc (output_bfd, s->size);
5837 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5838 return FALSE;
5839
5840 /* Fill in the version definition section. */
5841
5842 p = s->contents;
5843
5844 def.vd_version = VER_DEF_CURRENT;
5845 def.vd_flags = VER_FLG_BASE;
5846 def.vd_ndx = 1;
5847 def.vd_cnt = 1;
3e3b46e5
PB
5848 if (info->create_default_symver)
5849 {
5850 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5851 def.vd_next = sizeof (Elf_External_Verdef);
5852 }
5853 else
5854 {
5855 def.vd_aux = sizeof (Elf_External_Verdef);
5856 def.vd_next = (sizeof (Elf_External_Verdef)
5857 + sizeof (Elf_External_Verdaux));
5858 }
5a580b3a
AM
5859
5860 if (soname_indx != (bfd_size_type) -1)
5861 {
5862 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5863 soname_indx);
5864 def.vd_hash = bfd_elf_hash (soname);
5865 defaux.vda_name = soname_indx;
3e3b46e5 5866 name = soname;
5a580b3a
AM
5867 }
5868 else
5869 {
5a580b3a
AM
5870 bfd_size_type indx;
5871
06084812 5872 name = lbasename (output_bfd->filename);
5a580b3a
AM
5873 def.vd_hash = bfd_elf_hash (name);
5874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5875 name, FALSE);
5876 if (indx == (bfd_size_type) -1)
5877 return FALSE;
5878 defaux.vda_name = indx;
5879 }
5880 defaux.vda_next = 0;
5881
5882 _bfd_elf_swap_verdef_out (output_bfd, &def,
5883 (Elf_External_Verdef *) p);
5884 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
5885 if (info->create_default_symver)
5886 {
5887 /* Add a symbol representing this version. */
5888 bh = NULL;
5889 if (! (_bfd_generic_link_add_one_symbol
5890 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5891 0, NULL, FALSE,
5892 get_elf_backend_data (dynobj)->collect, &bh)))
5893 return FALSE;
5894 h = (struct elf_link_hash_entry *) bh;
5895 h->non_elf = 0;
5896 h->def_regular = 1;
5897 h->type = STT_OBJECT;
5898 h->verinfo.vertree = NULL;
5899
5900 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5901 return FALSE;
5902
5903 /* Create a duplicate of the base version with the same
5904 aux block, but different flags. */
5905 def.vd_flags = 0;
5906 def.vd_ndx = 2;
5907 def.vd_aux = sizeof (Elf_External_Verdef);
5908 if (verdefs)
5909 def.vd_next = (sizeof (Elf_External_Verdef)
5910 + sizeof (Elf_External_Verdaux));
5911 else
5912 def.vd_next = 0;
5913 _bfd_elf_swap_verdef_out (output_bfd, &def,
5914 (Elf_External_Verdef *) p);
5915 p += sizeof (Elf_External_Verdef);
5916 }
5a580b3a
AM
5917 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5918 (Elf_External_Verdaux *) p);
5919 p += sizeof (Elf_External_Verdaux);
5920
5921 for (t = verdefs; t != NULL; t = t->next)
5922 {
5923 unsigned int cdeps;
5924 struct bfd_elf_version_deps *n;
5a580b3a
AM
5925
5926 cdeps = 0;
5927 for (n = t->deps; n != NULL; n = n->next)
5928 ++cdeps;
5929
5930 /* Add a symbol representing this version. */
5931 bh = NULL;
5932 if (! (_bfd_generic_link_add_one_symbol
5933 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5934 0, NULL, FALSE,
5935 get_elf_backend_data (dynobj)->collect, &bh)))
5936 return FALSE;
5937 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5938 h->non_elf = 0;
5939 h->def_regular = 1;
5a580b3a
AM
5940 h->type = STT_OBJECT;
5941 h->verinfo.vertree = t;
5942
c152c796 5943 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
5944 return FALSE;
5945
5946 def.vd_version = VER_DEF_CURRENT;
5947 def.vd_flags = 0;
5948 if (t->globals.list == NULL
5949 && t->locals.list == NULL
5950 && ! t->used)
5951 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 5952 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
5953 def.vd_cnt = cdeps + 1;
5954 def.vd_hash = bfd_elf_hash (t->name);
5955 def.vd_aux = sizeof (Elf_External_Verdef);
5956 def.vd_next = 0;
5957 if (t->next != NULL)
5958 def.vd_next = (sizeof (Elf_External_Verdef)
5959 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5960
5961 _bfd_elf_swap_verdef_out (output_bfd, &def,
5962 (Elf_External_Verdef *) p);
5963 p += sizeof (Elf_External_Verdef);
5964
5965 defaux.vda_name = h->dynstr_index;
5966 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5967 h->dynstr_index);
5968 defaux.vda_next = 0;
5969 if (t->deps != NULL)
5970 defaux.vda_next = sizeof (Elf_External_Verdaux);
5971 t->name_indx = defaux.vda_name;
5972
5973 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5974 (Elf_External_Verdaux *) p);
5975 p += sizeof (Elf_External_Verdaux);
5976
5977 for (n = t->deps; n != NULL; n = n->next)
5978 {
5979 if (n->version_needed == NULL)
5980 {
5981 /* This can happen if there was an error in the
5982 version script. */
5983 defaux.vda_name = 0;
5984 }
5985 else
5986 {
5987 defaux.vda_name = n->version_needed->name_indx;
5988 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5989 defaux.vda_name);
5990 }
5991 if (n->next == NULL)
5992 defaux.vda_next = 0;
5993 else
5994 defaux.vda_next = sizeof (Elf_External_Verdaux);
5995
5996 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5997 (Elf_External_Verdaux *) p);
5998 p += sizeof (Elf_External_Verdaux);
5999 }
6000 }
6001
6002 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6003 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6004 return FALSE;
6005
6006 elf_tdata (output_bfd)->cverdefs = cdefs;
6007 }
6008
6009 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6010 {
6011 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6012 return FALSE;
6013 }
6014 else if (info->flags & DF_BIND_NOW)
6015 {
6016 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6017 return FALSE;
6018 }
6019
6020 if (info->flags_1)
6021 {
6022 if (info->executable)
6023 info->flags_1 &= ~ (DF_1_INITFIRST
6024 | DF_1_NODELETE
6025 | DF_1_NOOPEN);
6026 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6027 return FALSE;
6028 }
6029
6030 /* Work out the size of the version reference section. */
6031
6032 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6033 BFD_ASSERT (s != NULL);
6034 {
6035 struct elf_find_verdep_info sinfo;
6036
6037 sinfo.output_bfd = output_bfd;
6038 sinfo.info = info;
6039 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6040 if (sinfo.vers == 0)
6041 sinfo.vers = 1;
6042 sinfo.failed = FALSE;
6043
6044 elf_link_hash_traverse (elf_hash_table (info),
6045 _bfd_elf_link_find_version_dependencies,
6046 &sinfo);
14b1c01e
AM
6047 if (sinfo.failed)
6048 return FALSE;
5a580b3a
AM
6049
6050 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6051 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6052 else
6053 {
6054 Elf_Internal_Verneed *t;
6055 unsigned int size;
6056 unsigned int crefs;
6057 bfd_byte *p;
6058
6059 /* Build the version definition section. */
6060 size = 0;
6061 crefs = 0;
6062 for (t = elf_tdata (output_bfd)->verref;
6063 t != NULL;
6064 t = t->vn_nextref)
6065 {
6066 Elf_Internal_Vernaux *a;
6067
6068 size += sizeof (Elf_External_Verneed);
6069 ++crefs;
6070 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6071 size += sizeof (Elf_External_Vernaux);
6072 }
6073
eea6121a
AM
6074 s->size = size;
6075 s->contents = bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6076 if (s->contents == NULL)
6077 return FALSE;
6078
6079 p = s->contents;
6080 for (t = elf_tdata (output_bfd)->verref;
6081 t != NULL;
6082 t = t->vn_nextref)
6083 {
6084 unsigned int caux;
6085 Elf_Internal_Vernaux *a;
6086 bfd_size_type indx;
6087
6088 caux = 0;
6089 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6090 ++caux;
6091
6092 t->vn_version = VER_NEED_CURRENT;
6093 t->vn_cnt = caux;
6094 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6095 elf_dt_name (t->vn_bfd) != NULL
6096 ? elf_dt_name (t->vn_bfd)
06084812 6097 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6098 FALSE);
6099 if (indx == (bfd_size_type) -1)
6100 return FALSE;
6101 t->vn_file = indx;
6102 t->vn_aux = sizeof (Elf_External_Verneed);
6103 if (t->vn_nextref == NULL)
6104 t->vn_next = 0;
6105 else
6106 t->vn_next = (sizeof (Elf_External_Verneed)
6107 + caux * sizeof (Elf_External_Vernaux));
6108
6109 _bfd_elf_swap_verneed_out (output_bfd, t,
6110 (Elf_External_Verneed *) p);
6111 p += sizeof (Elf_External_Verneed);
6112
6113 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6114 {
6115 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6116 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6117 a->vna_nodename, FALSE);
6118 if (indx == (bfd_size_type) -1)
6119 return FALSE;
6120 a->vna_name = indx;
6121 if (a->vna_nextptr == NULL)
6122 a->vna_next = 0;
6123 else
6124 a->vna_next = sizeof (Elf_External_Vernaux);
6125
6126 _bfd_elf_swap_vernaux_out (output_bfd, a,
6127 (Elf_External_Vernaux *) p);
6128 p += sizeof (Elf_External_Vernaux);
6129 }
6130 }
6131
6132 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6133 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6134 return FALSE;
6135
6136 elf_tdata (output_bfd)->cverrefs = crefs;
6137 }
6138 }
6139
8423293d
AM
6140 if ((elf_tdata (output_bfd)->cverrefs == 0
6141 && elf_tdata (output_bfd)->cverdefs == 0)
6142 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6143 &section_sym_count) == 0)
6144 {
6145 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6146 s->flags |= SEC_EXCLUDE;
6147 }
6148 }
6149 return TRUE;
6150}
6151
74541ad4
AM
6152/* Find the first non-excluded output section. We'll use its
6153 section symbol for some emitted relocs. */
6154void
6155_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6156{
6157 asection *s;
6158
6159 for (s = output_bfd->sections; s != NULL; s = s->next)
6160 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6161 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6162 {
6163 elf_hash_table (info)->text_index_section = s;
6164 break;
6165 }
6166}
6167
6168/* Find two non-excluded output sections, one for code, one for data.
6169 We'll use their section symbols for some emitted relocs. */
6170void
6171_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6172{
6173 asection *s;
6174
266b05cf
DJ
6175 /* Data first, since setting text_index_section changes
6176 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6177 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6178 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6179 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6180 {
266b05cf 6181 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6182 break;
6183 }
6184
6185 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6186 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6187 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6188 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6189 {
266b05cf 6190 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6191 break;
6192 }
6193
6194 if (elf_hash_table (info)->text_index_section == NULL)
6195 elf_hash_table (info)->text_index_section
6196 = elf_hash_table (info)->data_index_section;
6197}
6198
8423293d
AM
6199bfd_boolean
6200bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6201{
74541ad4
AM
6202 const struct elf_backend_data *bed;
6203
8423293d
AM
6204 if (!is_elf_hash_table (info->hash))
6205 return TRUE;
6206
74541ad4
AM
6207 bed = get_elf_backend_data (output_bfd);
6208 (*bed->elf_backend_init_index_section) (output_bfd, info);
6209
8423293d
AM
6210 if (elf_hash_table (info)->dynamic_sections_created)
6211 {
6212 bfd *dynobj;
8423293d
AM
6213 asection *s;
6214 bfd_size_type dynsymcount;
6215 unsigned long section_sym_count;
8423293d
AM
6216 unsigned int dtagcount;
6217
6218 dynobj = elf_hash_table (info)->dynobj;
6219
5a580b3a
AM
6220 /* Assign dynsym indicies. In a shared library we generate a
6221 section symbol for each output section, which come first.
6222 Next come all of the back-end allocated local dynamic syms,
6223 followed by the rest of the global symbols. */
6224
554220db
AM
6225 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6226 &section_sym_count);
5a580b3a
AM
6227
6228 /* Work out the size of the symbol version section. */
6229 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6230 BFD_ASSERT (s != NULL);
8423293d
AM
6231 if (dynsymcount != 0
6232 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6233 {
eea6121a
AM
6234 s->size = dynsymcount * sizeof (Elf_External_Versym);
6235 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6236 if (s->contents == NULL)
6237 return FALSE;
6238
6239 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6240 return FALSE;
6241 }
6242
6243 /* Set the size of the .dynsym and .hash sections. We counted
6244 the number of dynamic symbols in elf_link_add_object_symbols.
6245 We will build the contents of .dynsym and .hash when we build
6246 the final symbol table, because until then we do not know the
6247 correct value to give the symbols. We built the .dynstr
6248 section as we went along in elf_link_add_object_symbols. */
6249 s = bfd_get_section_by_name (dynobj, ".dynsym");
6250 BFD_ASSERT (s != NULL);
eea6121a 6251 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6252
6253 if (dynsymcount != 0)
6254 {
554220db
AM
6255 s->contents = bfd_alloc (output_bfd, s->size);
6256 if (s->contents == NULL)
6257 return FALSE;
5a580b3a 6258
554220db
AM
6259 /* The first entry in .dynsym is a dummy symbol.
6260 Clear all the section syms, in case we don't output them all. */
6261 ++section_sym_count;
6262 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6263 }
6264
fdc90cb4
JJ
6265 elf_hash_table (info)->bucketcount = 0;
6266
5a580b3a
AM
6267 /* Compute the size of the hashing table. As a side effect this
6268 computes the hash values for all the names we export. */
fdc90cb4
JJ
6269 if (info->emit_hash)
6270 {
6271 unsigned long int *hashcodes;
14b1c01e 6272 struct hash_codes_info hashinf;
fdc90cb4
JJ
6273 bfd_size_type amt;
6274 unsigned long int nsyms;
6275 size_t bucketcount;
6276 size_t hash_entry_size;
6277
6278 /* Compute the hash values for all exported symbols. At the same
6279 time store the values in an array so that we could use them for
6280 optimizations. */
6281 amt = dynsymcount * sizeof (unsigned long int);
6282 hashcodes = bfd_malloc (amt);
6283 if (hashcodes == NULL)
6284 return FALSE;
14b1c01e
AM
6285 hashinf.hashcodes = hashcodes;
6286 hashinf.error = FALSE;
5a580b3a 6287
fdc90cb4
JJ
6288 /* Put all hash values in HASHCODES. */
6289 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6290 elf_collect_hash_codes, &hashinf);
6291 if (hashinf.error)
4dd07732
AM
6292 {
6293 free (hashcodes);
6294 return FALSE;
6295 }
5a580b3a 6296
14b1c01e 6297 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6298 bucketcount
6299 = compute_bucket_count (info, hashcodes, nsyms, 0);
6300 free (hashcodes);
6301
6302 if (bucketcount == 0)
6303 return FALSE;
5a580b3a 6304
fdc90cb4
JJ
6305 elf_hash_table (info)->bucketcount = bucketcount;
6306
6307 s = bfd_get_section_by_name (dynobj, ".hash");
6308 BFD_ASSERT (s != NULL);
6309 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6310 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6311 s->contents = bfd_zalloc (output_bfd, s->size);
6312 if (s->contents == NULL)
6313 return FALSE;
6314
6315 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6316 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6317 s->contents + hash_entry_size);
6318 }
6319
6320 if (info->emit_gnu_hash)
6321 {
6322 size_t i, cnt;
6323 unsigned char *contents;
6324 struct collect_gnu_hash_codes cinfo;
6325 bfd_size_type amt;
6326 size_t bucketcount;
6327
6328 memset (&cinfo, 0, sizeof (cinfo));
6329
6330 /* Compute the hash values for all exported symbols. At the same
6331 time store the values in an array so that we could use them for
6332 optimizations. */
6333 amt = dynsymcount * 2 * sizeof (unsigned long int);
6334 cinfo.hashcodes = bfd_malloc (amt);
6335 if (cinfo.hashcodes == NULL)
6336 return FALSE;
6337
6338 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6339 cinfo.min_dynindx = -1;
6340 cinfo.output_bfd = output_bfd;
6341 cinfo.bed = bed;
6342
6343 /* Put all hash values in HASHCODES. */
6344 elf_link_hash_traverse (elf_hash_table (info),
6345 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6346 if (cinfo.error)
4dd07732
AM
6347 {
6348 free (cinfo.hashcodes);
6349 return FALSE;
6350 }
fdc90cb4
JJ
6351
6352 bucketcount
6353 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6354
6355 if (bucketcount == 0)
6356 {
6357 free (cinfo.hashcodes);
6358 return FALSE;
6359 }
6360
6361 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6362 BFD_ASSERT (s != NULL);
6363
6364 if (cinfo.nsyms == 0)
6365 {
6366 /* Empty .gnu.hash section is special. */
6367 BFD_ASSERT (cinfo.min_dynindx == -1);
6368 free (cinfo.hashcodes);
6369 s->size = 5 * 4 + bed->s->arch_size / 8;
6370 contents = bfd_zalloc (output_bfd, s->size);
6371 if (contents == NULL)
6372 return FALSE;
6373 s->contents = contents;
6374 /* 1 empty bucket. */
6375 bfd_put_32 (output_bfd, 1, contents);
6376 /* SYMIDX above the special symbol 0. */
6377 bfd_put_32 (output_bfd, 1, contents + 4);
6378 /* Just one word for bitmask. */
6379 bfd_put_32 (output_bfd, 1, contents + 8);
6380 /* Only hash fn bloom filter. */
6381 bfd_put_32 (output_bfd, 0, contents + 12);
6382 /* No hashes are valid - empty bitmask. */
6383 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6384 /* No hashes in the only bucket. */
6385 bfd_put_32 (output_bfd, 0,
6386 contents + 16 + bed->s->arch_size / 8);
6387 }
6388 else
6389 {
fdc90cb4 6390 unsigned long int maskwords, maskbitslog2;
0b33793d 6391 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4
JJ
6392
6393 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6394 if (maskbitslog2 < 3)
6395 maskbitslog2 = 5;
6396 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6397 maskbitslog2 = maskbitslog2 + 3;
6398 else
6399 maskbitslog2 = maskbitslog2 + 2;
6400 if (bed->s->arch_size == 64)
6401 {
6402 if (maskbitslog2 == 5)
6403 maskbitslog2 = 6;
6404 cinfo.shift1 = 6;
6405 }
6406 else
6407 cinfo.shift1 = 5;
6408 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6409 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6410 cinfo.maskbits = 1 << maskbitslog2;
6411 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6412 amt = bucketcount * sizeof (unsigned long int) * 2;
6413 amt += maskwords * sizeof (bfd_vma);
6414 cinfo.bitmask = bfd_malloc (amt);
6415 if (cinfo.bitmask == NULL)
6416 {
6417 free (cinfo.hashcodes);
6418 return FALSE;
6419 }
6420
6421 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6422 cinfo.indx = cinfo.counts + bucketcount;
6423 cinfo.symindx = dynsymcount - cinfo.nsyms;
6424 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6425
6426 /* Determine how often each hash bucket is used. */
6427 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6428 for (i = 0; i < cinfo.nsyms; ++i)
6429 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6430
6431 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6432 if (cinfo.counts[i] != 0)
6433 {
6434 cinfo.indx[i] = cnt;
6435 cnt += cinfo.counts[i];
6436 }
6437 BFD_ASSERT (cnt == dynsymcount);
6438 cinfo.bucketcount = bucketcount;
6439 cinfo.local_indx = cinfo.min_dynindx;
6440
6441 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6442 s->size += cinfo.maskbits / 8;
6443 contents = bfd_zalloc (output_bfd, s->size);
6444 if (contents == NULL)
6445 {
6446 free (cinfo.bitmask);
6447 free (cinfo.hashcodes);
6448 return FALSE;
6449 }
6450
6451 s->contents = contents;
6452 bfd_put_32 (output_bfd, bucketcount, contents);
6453 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6454 bfd_put_32 (output_bfd, maskwords, contents + 8);
6455 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6456 contents += 16 + cinfo.maskbits / 8;
6457
6458 for (i = 0; i < bucketcount; ++i)
6459 {
6460 if (cinfo.counts[i] == 0)
6461 bfd_put_32 (output_bfd, 0, contents);
6462 else
6463 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6464 contents += 4;
6465 }
6466
6467 cinfo.contents = contents;
6468
6469 /* Renumber dynamic symbols, populate .gnu.hash section. */
6470 elf_link_hash_traverse (elf_hash_table (info),
6471 elf_renumber_gnu_hash_syms, &cinfo);
6472
6473 contents = s->contents + 16;
6474 for (i = 0; i < maskwords; ++i)
6475 {
6476 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6477 contents);
6478 contents += bed->s->arch_size / 8;
6479 }
6480
6481 free (cinfo.bitmask);
6482 free (cinfo.hashcodes);
6483 }
6484 }
5a580b3a
AM
6485
6486 s = bfd_get_section_by_name (dynobj, ".dynstr");
6487 BFD_ASSERT (s != NULL);
6488
4ad4eba5 6489 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6490
eea6121a 6491 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6492
6493 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6494 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6495 return FALSE;
6496 }
6497
6498 return TRUE;
6499}
4d269e42
AM
6500\f
6501/* Indicate that we are only retrieving symbol values from this
6502 section. */
6503
6504void
6505_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6506{
6507 if (is_elf_hash_table (info->hash))
6508 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6509 _bfd_generic_link_just_syms (sec, info);
6510}
6511
6512/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6513
6514static void
6515merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6516 asection *sec)
6517{
6518 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6519 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6520}
6521
6522/* Finish SHF_MERGE section merging. */
6523
6524bfd_boolean
6525_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6526{
6527 bfd *ibfd;
6528 asection *sec;
6529
6530 if (!is_elf_hash_table (info->hash))
6531 return FALSE;
6532
6533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6534 if ((ibfd->flags & DYNAMIC) == 0)
6535 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6536 if ((sec->flags & SEC_MERGE) != 0
6537 && !bfd_is_abs_section (sec->output_section))
6538 {
6539 struct bfd_elf_section_data *secdata;
6540
6541 secdata = elf_section_data (sec);
6542 if (! _bfd_add_merge_section (abfd,
6543 &elf_hash_table (info)->merge_info,
6544 sec, &secdata->sec_info))
6545 return FALSE;
6546 else if (secdata->sec_info)
6547 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6548 }
6549
6550 if (elf_hash_table (info)->merge_info != NULL)
6551 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6552 merge_sections_remove_hook);
6553 return TRUE;
6554}
6555
6556/* Create an entry in an ELF linker hash table. */
6557
6558struct bfd_hash_entry *
6559_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6560 struct bfd_hash_table *table,
6561 const char *string)
6562{
6563 /* Allocate the structure if it has not already been allocated by a
6564 subclass. */
6565 if (entry == NULL)
6566 {
6567 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6568 if (entry == NULL)
6569 return entry;
6570 }
6571
6572 /* Call the allocation method of the superclass. */
6573 entry = _bfd_link_hash_newfunc (entry, table, string);
6574 if (entry != NULL)
6575 {
6576 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6577 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6578
6579 /* Set local fields. */
6580 ret->indx = -1;
6581 ret->dynindx = -1;
6582 ret->got = htab->init_got_refcount;
6583 ret->plt = htab->init_plt_refcount;
6584 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6585 - offsetof (struct elf_link_hash_entry, size)));
6586 /* Assume that we have been called by a non-ELF symbol reader.
6587 This flag is then reset by the code which reads an ELF input
6588 file. This ensures that a symbol created by a non-ELF symbol
6589 reader will have the flag set correctly. */
6590 ret->non_elf = 1;
6591 }
6592
6593 return entry;
6594}
6595
6596/* Copy data from an indirect symbol to its direct symbol, hiding the
6597 old indirect symbol. Also used for copying flags to a weakdef. */
6598
6599void
6600_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6601 struct elf_link_hash_entry *dir,
6602 struct elf_link_hash_entry *ind)
6603{
6604 struct elf_link_hash_table *htab;
6605
6606 /* Copy down any references that we may have already seen to the
6607 symbol which just became indirect. */
6608
6609 dir->ref_dynamic |= ind->ref_dynamic;
6610 dir->ref_regular |= ind->ref_regular;
6611 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6612 dir->non_got_ref |= ind->non_got_ref;
6613 dir->needs_plt |= ind->needs_plt;
6614 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6615
6616 if (ind->root.type != bfd_link_hash_indirect)
6617 return;
6618
6619 /* Copy over the global and procedure linkage table refcount entries.
6620 These may have been already set up by a check_relocs routine. */
6621 htab = elf_hash_table (info);
6622 if (ind->got.refcount > htab->init_got_refcount.refcount)
6623 {
6624 if (dir->got.refcount < 0)
6625 dir->got.refcount = 0;
6626 dir->got.refcount += ind->got.refcount;
6627 ind->got.refcount = htab->init_got_refcount.refcount;
6628 }
6629
6630 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6631 {
6632 if (dir->plt.refcount < 0)
6633 dir->plt.refcount = 0;
6634 dir->plt.refcount += ind->plt.refcount;
6635 ind->plt.refcount = htab->init_plt_refcount.refcount;
6636 }
6637
6638 if (ind->dynindx != -1)
6639 {
6640 if (dir->dynindx != -1)
6641 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6642 dir->dynindx = ind->dynindx;
6643 dir->dynstr_index = ind->dynstr_index;
6644 ind->dynindx = -1;
6645 ind->dynstr_index = 0;
6646 }
6647}
6648
6649void
6650_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6651 struct elf_link_hash_entry *h,
6652 bfd_boolean force_local)
6653{
6654 h->plt = elf_hash_table (info)->init_plt_offset;
6655 h->needs_plt = 0;
6656 if (force_local)
6657 {
6658 h->forced_local = 1;
6659 if (h->dynindx != -1)
6660 {
6661 h->dynindx = -1;
6662 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6663 h->dynstr_index);
6664 }
6665 }
6666}
6667
6668/* Initialize an ELF linker hash table. */
6669
6670bfd_boolean
6671_bfd_elf_link_hash_table_init
6672 (struct elf_link_hash_table *table,
6673 bfd *abfd,
6674 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6675 struct bfd_hash_table *,
6676 const char *),
6677 unsigned int entsize)
6678{
6679 bfd_boolean ret;
6680 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6681
6682 memset (table, 0, sizeof * table);
6683 table->init_got_refcount.refcount = can_refcount - 1;
6684 table->init_plt_refcount.refcount = can_refcount - 1;
6685 table->init_got_offset.offset = -(bfd_vma) 1;
6686 table->init_plt_offset.offset = -(bfd_vma) 1;
6687 /* The first dynamic symbol is a dummy. */
6688 table->dynsymcount = 1;
6689
6690 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6691 table->root.type = bfd_link_elf_hash_table;
6692
6693 return ret;
6694}
6695
6696/* Create an ELF linker hash table. */
6697
6698struct bfd_link_hash_table *
6699_bfd_elf_link_hash_table_create (bfd *abfd)
6700{
6701 struct elf_link_hash_table *ret;
6702 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6703
6704 ret = bfd_malloc (amt);
6705 if (ret == NULL)
6706 return NULL;
6707
6708 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6709 sizeof (struct elf_link_hash_entry)))
6710 {
6711 free (ret);
6712 return NULL;
6713 }
6714
6715 return &ret->root;
6716}
6717
6718/* This is a hook for the ELF emulation code in the generic linker to
6719 tell the backend linker what file name to use for the DT_NEEDED
6720 entry for a dynamic object. */
6721
6722void
6723bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6724{
6725 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6726 && bfd_get_format (abfd) == bfd_object)
6727 elf_dt_name (abfd) = name;
6728}
6729
6730int
6731bfd_elf_get_dyn_lib_class (bfd *abfd)
6732{
6733 int lib_class;
6734 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6735 && bfd_get_format (abfd) == bfd_object)
6736 lib_class = elf_dyn_lib_class (abfd);
6737 else
6738 lib_class = 0;
6739 return lib_class;
6740}
6741
6742void
6743bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6744{
6745 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6746 && bfd_get_format (abfd) == bfd_object)
6747 elf_dyn_lib_class (abfd) = lib_class;
6748}
6749
6750/* Get the list of DT_NEEDED entries for a link. This is a hook for
6751 the linker ELF emulation code. */
6752
6753struct bfd_link_needed_list *
6754bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6755 struct bfd_link_info *info)
6756{
6757 if (! is_elf_hash_table (info->hash))
6758 return NULL;
6759 return elf_hash_table (info)->needed;
6760}
6761
6762/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6763 hook for the linker ELF emulation code. */
6764
6765struct bfd_link_needed_list *
6766bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6767 struct bfd_link_info *info)
6768{
6769 if (! is_elf_hash_table (info->hash))
6770 return NULL;
6771 return elf_hash_table (info)->runpath;
6772}
6773
6774/* Get the name actually used for a dynamic object for a link. This
6775 is the SONAME entry if there is one. Otherwise, it is the string
6776 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6777
6778const char *
6779bfd_elf_get_dt_soname (bfd *abfd)
6780{
6781 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6782 && bfd_get_format (abfd) == bfd_object)
6783 return elf_dt_name (abfd);
6784 return NULL;
6785}
6786
6787/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6788 the ELF linker emulation code. */
6789
6790bfd_boolean
6791bfd_elf_get_bfd_needed_list (bfd *abfd,
6792 struct bfd_link_needed_list **pneeded)
6793{
6794 asection *s;
6795 bfd_byte *dynbuf = NULL;
cb33740c 6796 unsigned int elfsec;
4d269e42
AM
6797 unsigned long shlink;
6798 bfd_byte *extdyn, *extdynend;
6799 size_t extdynsize;
6800 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6801
6802 *pneeded = NULL;
6803
6804 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6805 || bfd_get_format (abfd) != bfd_object)
6806 return TRUE;
6807
6808 s = bfd_get_section_by_name (abfd, ".dynamic");
6809 if (s == NULL || s->size == 0)
6810 return TRUE;
6811
6812 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6813 goto error_return;
6814
6815 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 6816 if (elfsec == SHN_BAD)
4d269e42
AM
6817 goto error_return;
6818
6819 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6820
4d269e42
AM
6821 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6822 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6823
6824 extdyn = dynbuf;
6825 extdynend = extdyn + s->size;
6826 for (; extdyn < extdynend; extdyn += extdynsize)
6827 {
6828 Elf_Internal_Dyn dyn;
6829
6830 (*swap_dyn_in) (abfd, extdyn, &dyn);
6831
6832 if (dyn.d_tag == DT_NULL)
6833 break;
6834
6835 if (dyn.d_tag == DT_NEEDED)
6836 {
6837 const char *string;
6838 struct bfd_link_needed_list *l;
6839 unsigned int tagv = dyn.d_un.d_val;
6840 bfd_size_type amt;
6841
6842 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6843 if (string == NULL)
6844 goto error_return;
6845
6846 amt = sizeof *l;
6847 l = bfd_alloc (abfd, amt);
6848 if (l == NULL)
6849 goto error_return;
6850
6851 l->by = abfd;
6852 l->name = string;
6853 l->next = *pneeded;
6854 *pneeded = l;
6855 }
6856 }
6857
6858 free (dynbuf);
6859
6860 return TRUE;
6861
6862 error_return:
6863 if (dynbuf != NULL)
6864 free (dynbuf);
6865 return FALSE;
6866}
6867
6868struct elf_symbuf_symbol
6869{
6870 unsigned long st_name; /* Symbol name, index in string tbl */
6871 unsigned char st_info; /* Type and binding attributes */
6872 unsigned char st_other; /* Visibilty, and target specific */
6873};
6874
6875struct elf_symbuf_head
6876{
6877 struct elf_symbuf_symbol *ssym;
6878 bfd_size_type count;
6879 unsigned int st_shndx;
6880};
6881
6882struct elf_symbol
6883{
6884 union
6885 {
6886 Elf_Internal_Sym *isym;
6887 struct elf_symbuf_symbol *ssym;
6888 } u;
6889 const char *name;
6890};
6891
6892/* Sort references to symbols by ascending section number. */
6893
6894static int
6895elf_sort_elf_symbol (const void *arg1, const void *arg2)
6896{
6897 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6898 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6899
6900 return s1->st_shndx - s2->st_shndx;
6901}
6902
6903static int
6904elf_sym_name_compare (const void *arg1, const void *arg2)
6905{
6906 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6907 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6908 return strcmp (s1->name, s2->name);
6909}
6910
6911static struct elf_symbuf_head *
6912elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6913{
14b1c01e 6914 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
6915 struct elf_symbuf_symbol *ssym;
6916 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 6917 bfd_size_type i, shndx_count, total_size;
4d269e42 6918
14b1c01e 6919 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
6920 if (indbuf == NULL)
6921 return NULL;
6922
6923 for (ind = indbuf, i = 0; i < symcount; i++)
6924 if (isymbuf[i].st_shndx != SHN_UNDEF)
6925 *ind++ = &isymbuf[i];
6926 indbufend = ind;
6927
6928 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6929 elf_sort_elf_symbol);
6930
6931 shndx_count = 0;
6932 if (indbufend > indbuf)
6933 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6934 if (ind[0]->st_shndx != ind[1]->st_shndx)
6935 shndx_count++;
6936
3ae181ee
L
6937 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
6938 + (indbufend - indbuf) * sizeof (*ssym));
6939 ssymbuf = bfd_malloc (total_size);
4d269e42
AM
6940 if (ssymbuf == NULL)
6941 {
6942 free (indbuf);
6943 return NULL;
6944 }
6945
3ae181ee 6946 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
6947 ssymbuf->ssym = NULL;
6948 ssymbuf->count = shndx_count;
6949 ssymbuf->st_shndx = 0;
6950 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6951 {
6952 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6953 {
6954 ssymhead++;
6955 ssymhead->ssym = ssym;
6956 ssymhead->count = 0;
6957 ssymhead->st_shndx = (*ind)->st_shndx;
6958 }
6959 ssym->st_name = (*ind)->st_name;
6960 ssym->st_info = (*ind)->st_info;
6961 ssym->st_other = (*ind)->st_other;
6962 ssymhead->count++;
6963 }
3ae181ee
L
6964 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
6965 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
6966 == total_size));
4d269e42
AM
6967
6968 free (indbuf);
6969 return ssymbuf;
6970}
6971
6972/* Check if 2 sections define the same set of local and global
6973 symbols. */
6974
8f317e31 6975static bfd_boolean
4d269e42
AM
6976bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6977 struct bfd_link_info *info)
6978{
6979 bfd *bfd1, *bfd2;
6980 const struct elf_backend_data *bed1, *bed2;
6981 Elf_Internal_Shdr *hdr1, *hdr2;
6982 bfd_size_type symcount1, symcount2;
6983 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6984 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6985 Elf_Internal_Sym *isym, *isymend;
6986 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6987 bfd_size_type count1, count2, i;
cb33740c 6988 unsigned int shndx1, shndx2;
4d269e42
AM
6989 bfd_boolean result;
6990
6991 bfd1 = sec1->owner;
6992 bfd2 = sec2->owner;
6993
4d269e42
AM
6994 /* Both sections have to be in ELF. */
6995 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6996 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6997 return FALSE;
6998
6999 if (elf_section_type (sec1) != elf_section_type (sec2))
7000 return FALSE;
7001
4d269e42
AM
7002 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7003 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7004 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7005 return FALSE;
7006
7007 bed1 = get_elf_backend_data (bfd1);
7008 bed2 = get_elf_backend_data (bfd2);
7009 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7010 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7011 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7012 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7013
7014 if (symcount1 == 0 || symcount2 == 0)
7015 return FALSE;
7016
7017 result = FALSE;
7018 isymbuf1 = NULL;
7019 isymbuf2 = NULL;
7020 ssymbuf1 = elf_tdata (bfd1)->symbuf;
7021 ssymbuf2 = elf_tdata (bfd2)->symbuf;
7022
7023 if (ssymbuf1 == NULL)
7024 {
7025 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7026 NULL, NULL, NULL);
7027 if (isymbuf1 == NULL)
7028 goto done;
7029
7030 if (!info->reduce_memory_overheads)
7031 elf_tdata (bfd1)->symbuf = ssymbuf1
7032 = elf_create_symbuf (symcount1, isymbuf1);
7033 }
7034
7035 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7036 {
7037 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7038 NULL, NULL, NULL);
7039 if (isymbuf2 == NULL)
7040 goto done;
7041
7042 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7043 elf_tdata (bfd2)->symbuf = ssymbuf2
7044 = elf_create_symbuf (symcount2, isymbuf2);
7045 }
7046
7047 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7048 {
7049 /* Optimized faster version. */
7050 bfd_size_type lo, hi, mid;
7051 struct elf_symbol *symp;
7052 struct elf_symbuf_symbol *ssym, *ssymend;
7053
7054 lo = 0;
7055 hi = ssymbuf1->count;
7056 ssymbuf1++;
7057 count1 = 0;
7058 while (lo < hi)
7059 {
7060 mid = (lo + hi) / 2;
cb33740c 7061 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7062 hi = mid;
cb33740c 7063 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7064 lo = mid + 1;
7065 else
7066 {
7067 count1 = ssymbuf1[mid].count;
7068 ssymbuf1 += mid;
7069 break;
7070 }
7071 }
7072
7073 lo = 0;
7074 hi = ssymbuf2->count;
7075 ssymbuf2++;
7076 count2 = 0;
7077 while (lo < hi)
7078 {
7079 mid = (lo + hi) / 2;
cb33740c 7080 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7081 hi = mid;
cb33740c 7082 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7083 lo = mid + 1;
7084 else
7085 {
7086 count2 = ssymbuf2[mid].count;
7087 ssymbuf2 += mid;
7088 break;
7089 }
7090 }
7091
7092 if (count1 == 0 || count2 == 0 || count1 != count2)
7093 goto done;
7094
7095 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7096 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7097 if (symtable1 == NULL || symtable2 == NULL)
7098 goto done;
7099
7100 symp = symtable1;
7101 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7102 ssym < ssymend; ssym++, symp++)
7103 {
7104 symp->u.ssym = ssym;
7105 symp->name = bfd_elf_string_from_elf_section (bfd1,
7106 hdr1->sh_link,
7107 ssym->st_name);
7108 }
7109
7110 symp = symtable2;
7111 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7112 ssym < ssymend; ssym++, symp++)
7113 {
7114 symp->u.ssym = ssym;
7115 symp->name = bfd_elf_string_from_elf_section (bfd2,
7116 hdr2->sh_link,
7117 ssym->st_name);
7118 }
7119
7120 /* Sort symbol by name. */
7121 qsort (symtable1, count1, sizeof (struct elf_symbol),
7122 elf_sym_name_compare);
7123 qsort (symtable2, count1, sizeof (struct elf_symbol),
7124 elf_sym_name_compare);
7125
7126 for (i = 0; i < count1; i++)
7127 /* Two symbols must have the same binding, type and name. */
7128 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7129 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7130 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7131 goto done;
7132
7133 result = TRUE;
7134 goto done;
7135 }
7136
7137 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7138 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7139 if (symtable1 == NULL || symtable2 == NULL)
7140 goto done;
7141
7142 /* Count definitions in the section. */
7143 count1 = 0;
7144 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7145 if (isym->st_shndx == shndx1)
4d269e42
AM
7146 symtable1[count1++].u.isym = isym;
7147
7148 count2 = 0;
7149 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7150 if (isym->st_shndx == shndx2)
4d269e42
AM
7151 symtable2[count2++].u.isym = isym;
7152
7153 if (count1 == 0 || count2 == 0 || count1 != count2)
7154 goto done;
7155
7156 for (i = 0; i < count1; i++)
7157 symtable1[i].name
7158 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7159 symtable1[i].u.isym->st_name);
7160
7161 for (i = 0; i < count2; i++)
7162 symtable2[i].name
7163 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7164 symtable2[i].u.isym->st_name);
7165
7166 /* Sort symbol by name. */
7167 qsort (symtable1, count1, sizeof (struct elf_symbol),
7168 elf_sym_name_compare);
7169 qsort (symtable2, count1, sizeof (struct elf_symbol),
7170 elf_sym_name_compare);
7171
7172 for (i = 0; i < count1; i++)
7173 /* Two symbols must have the same binding, type and name. */
7174 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7175 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7176 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7177 goto done;
7178
7179 result = TRUE;
7180
7181done:
7182 if (symtable1)
7183 free (symtable1);
7184 if (symtable2)
7185 free (symtable2);
7186 if (isymbuf1)
7187 free (isymbuf1);
7188 if (isymbuf2)
7189 free (isymbuf2);
7190
7191 return result;
7192}
7193
7194/* Return TRUE if 2 section types are compatible. */
7195
7196bfd_boolean
7197_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7198 bfd *bbfd, const asection *bsec)
7199{
7200 if (asec == NULL
7201 || bsec == NULL
7202 || abfd->xvec->flavour != bfd_target_elf_flavour
7203 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7204 return TRUE;
7205
7206 return elf_section_type (asec) == elf_section_type (bsec);
7207}
7208\f
c152c796
AM
7209/* Final phase of ELF linker. */
7210
7211/* A structure we use to avoid passing large numbers of arguments. */
7212
7213struct elf_final_link_info
7214{
7215 /* General link information. */
7216 struct bfd_link_info *info;
7217 /* Output BFD. */
7218 bfd *output_bfd;
7219 /* Symbol string table. */
7220 struct bfd_strtab_hash *symstrtab;
7221 /* .dynsym section. */
7222 asection *dynsym_sec;
7223 /* .hash section. */
7224 asection *hash_sec;
7225 /* symbol version section (.gnu.version). */
7226 asection *symver_sec;
7227 /* Buffer large enough to hold contents of any section. */
7228 bfd_byte *contents;
7229 /* Buffer large enough to hold external relocs of any section. */
7230 void *external_relocs;
7231 /* Buffer large enough to hold internal relocs of any section. */
7232 Elf_Internal_Rela *internal_relocs;
7233 /* Buffer large enough to hold external local symbols of any input
7234 BFD. */
7235 bfd_byte *external_syms;
7236 /* And a buffer for symbol section indices. */
7237 Elf_External_Sym_Shndx *locsym_shndx;
7238 /* Buffer large enough to hold internal local symbols of any input
7239 BFD. */
7240 Elf_Internal_Sym *internal_syms;
7241 /* Array large enough to hold a symbol index for each local symbol
7242 of any input BFD. */
7243 long *indices;
7244 /* Array large enough to hold a section pointer for each local
7245 symbol of any input BFD. */
7246 asection **sections;
7247 /* Buffer to hold swapped out symbols. */
7248 bfd_byte *symbuf;
7249 /* And one for symbol section indices. */
7250 Elf_External_Sym_Shndx *symshndxbuf;
7251 /* Number of swapped out symbols in buffer. */
7252 size_t symbuf_count;
7253 /* Number of symbols which fit in symbuf. */
7254 size_t symbuf_size;
7255 /* And same for symshndxbuf. */
7256 size_t shndxbuf_size;
7257};
7258
7259/* This struct is used to pass information to elf_link_output_extsym. */
7260
7261struct elf_outext_info
7262{
7263 bfd_boolean failed;
7264 bfd_boolean localsyms;
7265 struct elf_final_link_info *finfo;
7266};
7267
d9352518
DB
7268
7269/* Support for evaluating a complex relocation.
7270
7271 Complex relocations are generalized, self-describing relocations. The
7272 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7273 relocations themselves.
d9352518
DB
7274
7275 The relocations are use a reserved elf-wide relocation type code (R_RELC
7276 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7277 information (start bit, end bit, word width, etc) into the addend. This
7278 information is extracted from CGEN-generated operand tables within gas.
7279
7280 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7281 internal) representing prefix-notation expressions, including but not
7282 limited to those sorts of expressions normally encoded as addends in the
7283 addend field. The symbol mangling format is:
7284
7285 <node> := <literal>
7286 | <unary-operator> ':' <node>
7287 | <binary-operator> ':' <node> ':' <node>
7288 ;
7289
7290 <literal> := 's' <digits=N> ':' <N character symbol name>
7291 | 'S' <digits=N> ':' <N character section name>
7292 | '#' <hexdigits>
7293 ;
7294
7295 <binary-operator> := as in C
7296 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7297
7298static void
a0c8462f
AM
7299set_symbol_value (bfd *bfd_with_globals,
7300 Elf_Internal_Sym *isymbuf,
7301 size_t locsymcount,
7302 size_t symidx,
7303 bfd_vma val)
d9352518 7304{
8977835c
AM
7305 struct elf_link_hash_entry **sym_hashes;
7306 struct elf_link_hash_entry *h;
7307 size_t extsymoff = locsymcount;
d9352518 7308
8977835c 7309 if (symidx < locsymcount)
d9352518 7310 {
8977835c
AM
7311 Elf_Internal_Sym *sym;
7312
7313 sym = isymbuf + symidx;
7314 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7315 {
7316 /* It is a local symbol: move it to the
7317 "absolute" section and give it a value. */
7318 sym->st_shndx = SHN_ABS;
7319 sym->st_value = val;
7320 return;
7321 }
7322 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7323 extsymoff = 0;
d9352518 7324 }
8977835c
AM
7325
7326 /* It is a global symbol: set its link type
7327 to "defined" and give it a value. */
7328
7329 sym_hashes = elf_sym_hashes (bfd_with_globals);
7330 h = sym_hashes [symidx - extsymoff];
7331 while (h->root.type == bfd_link_hash_indirect
7332 || h->root.type == bfd_link_hash_warning)
7333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7334 h->root.type = bfd_link_hash_defined;
7335 h->root.u.def.value = val;
7336 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7337}
7338
a0c8462f
AM
7339static bfd_boolean
7340resolve_symbol (const char *name,
7341 bfd *input_bfd,
7342 struct elf_final_link_info *finfo,
7343 bfd_vma *result,
7344 Elf_Internal_Sym *isymbuf,
7345 size_t locsymcount)
d9352518 7346{
a0c8462f
AM
7347 Elf_Internal_Sym *sym;
7348 struct bfd_link_hash_entry *global_entry;
7349 const char *candidate = NULL;
7350 Elf_Internal_Shdr *symtab_hdr;
7351 size_t i;
7352
d9352518
DB
7353 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7354
7355 for (i = 0; i < locsymcount; ++ i)
7356 {
8977835c 7357 sym = isymbuf + i;
d9352518
DB
7358
7359 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7360 continue;
7361
7362 candidate = bfd_elf_string_from_elf_section (input_bfd,
7363 symtab_hdr->sh_link,
7364 sym->st_name);
7365#ifdef DEBUG
0f02bbd9
AM
7366 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7367 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7368#endif
7369 if (candidate && strcmp (candidate, name) == 0)
7370 {
0f02bbd9 7371 asection *sec = finfo->sections [i];
d9352518 7372
0f02bbd9
AM
7373 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7374 *result += sec->output_offset + sec->output_section->vma;
d9352518 7375#ifdef DEBUG
0f02bbd9
AM
7376 printf ("Found symbol with value %8.8lx\n",
7377 (unsigned long) *result);
d9352518
DB
7378#endif
7379 return TRUE;
7380 }
7381 }
7382
7383 /* Hmm, haven't found it yet. perhaps it is a global. */
a0c8462f
AM
7384 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7385 FALSE, FALSE, TRUE);
d9352518
DB
7386 if (!global_entry)
7387 return FALSE;
a0c8462f 7388
d9352518
DB
7389 if (global_entry->type == bfd_link_hash_defined
7390 || global_entry->type == bfd_link_hash_defweak)
7391 {
a0c8462f
AM
7392 *result = (global_entry->u.def.value
7393 + global_entry->u.def.section->output_section->vma
7394 + global_entry->u.def.section->output_offset);
d9352518 7395#ifdef DEBUG
0f02bbd9
AM
7396 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7397 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7398#endif
7399 return TRUE;
a0c8462f 7400 }
d9352518 7401
d9352518
DB
7402 return FALSE;
7403}
7404
7405static bfd_boolean
a0c8462f
AM
7406resolve_section (const char *name,
7407 asection *sections,
7408 bfd_vma *result)
d9352518 7409{
a0c8462f
AM
7410 asection *curr;
7411 unsigned int len;
d9352518 7412
a0c8462f 7413 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7414 if (strcmp (curr->name, name) == 0)
7415 {
7416 *result = curr->vma;
7417 return TRUE;
7418 }
7419
7420 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7421 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7422 {
7423 len = strlen (curr->name);
a0c8462f 7424 if (len > strlen (name))
d9352518
DB
7425 continue;
7426
7427 if (strncmp (curr->name, name, len) == 0)
7428 {
7429 if (strncmp (".end", name + len, 4) == 0)
7430 {
7431 *result = curr->vma + curr->size;
7432 return TRUE;
7433 }
7434
7435 /* Insert more pseudo-section names here, if you like. */
7436 }
7437 }
a0c8462f 7438
d9352518
DB
7439 return FALSE;
7440}
7441
7442static void
a0c8462f 7443undefined_reference (const char *reftype, const char *name)
d9352518 7444{
a0c8462f
AM
7445 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7446 reftype, name);
d9352518
DB
7447}
7448
7449static bfd_boolean
a0c8462f
AM
7450eval_symbol (bfd_vma *result,
7451 const char **symp,
7452 bfd *input_bfd,
7453 struct elf_final_link_info *finfo,
7454 bfd_vma dot,
7455 Elf_Internal_Sym *isymbuf,
7456 size_t locsymcount,
7457 int signed_p)
d9352518 7458{
4b93929b
NC
7459 size_t len;
7460 size_t symlen;
a0c8462f
AM
7461 bfd_vma a;
7462 bfd_vma b;
4b93929b 7463 char symbuf[4096];
0f02bbd9 7464 const char *sym = *symp;
a0c8462f
AM
7465 const char *symend;
7466 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7467
7468 len = strlen (sym);
7469 symend = sym + len;
7470
4b93929b 7471 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7472 {
7473 bfd_set_error (bfd_error_invalid_operation);
7474 return FALSE;
7475 }
a0c8462f 7476
d9352518
DB
7477 switch (* sym)
7478 {
7479 case '.':
0f02bbd9
AM
7480 *result = dot;
7481 *symp = sym + 1;
d9352518
DB
7482 return TRUE;
7483
7484 case '#':
0f02bbd9
AM
7485 ++sym;
7486 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7487 return TRUE;
7488
7489 case 'S':
7490 symbol_is_section = TRUE;
a0c8462f 7491 case 's':
0f02bbd9
AM
7492 ++sym;
7493 symlen = strtol (sym, (char **) symp, 10);
7494 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7495
4b93929b 7496 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7497 {
7498 bfd_set_error (bfd_error_invalid_operation);
7499 return FALSE;
7500 }
7501
7502 memcpy (symbuf, sym, symlen);
a0c8462f 7503 symbuf[symlen] = '\0';
0f02bbd9 7504 *symp = sym + symlen;
a0c8462f
AM
7505
7506 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7507 the symbol as a section, or vice-versa. so we're pretty liberal in our
7508 interpretation here; section means "try section first", not "must be a
7509 section", and likewise with symbol. */
7510
a0c8462f 7511 if (symbol_is_section)
d9352518 7512 {
8977835c
AM
7513 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7514 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7515 isymbuf, locsymcount))
d9352518
DB
7516 {
7517 undefined_reference ("section", symbuf);
7518 return FALSE;
7519 }
a0c8462f
AM
7520 }
7521 else
d9352518 7522 {
8977835c
AM
7523 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7524 isymbuf, locsymcount)
7525 && !resolve_section (symbuf, finfo->output_bfd->sections,
7526 result))
d9352518
DB
7527 {
7528 undefined_reference ("symbol", symbuf);
7529 return FALSE;
7530 }
7531 }
7532
7533 return TRUE;
a0c8462f 7534
d9352518
DB
7535 /* All that remains are operators. */
7536
7537#define UNARY_OP(op) \
7538 if (strncmp (sym, #op, strlen (#op)) == 0) \
7539 { \
7540 sym += strlen (#op); \
a0c8462f
AM
7541 if (*sym == ':') \
7542 ++sym; \
0f02bbd9
AM
7543 *symp = sym; \
7544 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7545 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7546 return FALSE; \
7547 if (signed_p) \
0f02bbd9 7548 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7549 else \
7550 *result = op a; \
d9352518
DB
7551 return TRUE; \
7552 }
7553
7554#define BINARY_OP(op) \
7555 if (strncmp (sym, #op, strlen (#op)) == 0) \
7556 { \
7557 sym += strlen (#op); \
a0c8462f
AM
7558 if (*sym == ':') \
7559 ++sym; \
0f02bbd9
AM
7560 *symp = sym; \
7561 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7562 isymbuf, locsymcount, signed_p)) \
a0c8462f 7563 return FALSE; \
0f02bbd9
AM
7564 ++*symp; \
7565 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7566 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7567 return FALSE; \
7568 if (signed_p) \
0f02bbd9 7569 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7570 else \
7571 *result = a op b; \
d9352518
DB
7572 return TRUE; \
7573 }
7574
7575 default:
7576 UNARY_OP (0-);
7577 BINARY_OP (<<);
7578 BINARY_OP (>>);
7579 BINARY_OP (==);
7580 BINARY_OP (!=);
7581 BINARY_OP (<=);
7582 BINARY_OP (>=);
7583 BINARY_OP (&&);
7584 BINARY_OP (||);
7585 UNARY_OP (~);
7586 UNARY_OP (!);
7587 BINARY_OP (*);
7588 BINARY_OP (/);
7589 BINARY_OP (%);
7590 BINARY_OP (^);
7591 BINARY_OP (|);
7592 BINARY_OP (&);
7593 BINARY_OP (+);
7594 BINARY_OP (-);
7595 BINARY_OP (<);
7596 BINARY_OP (>);
7597#undef UNARY_OP
7598#undef BINARY_OP
7599 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7600 bfd_set_error (bfd_error_invalid_operation);
7601 return FALSE;
7602 }
7603}
7604
d9352518 7605static void
a0c8462f
AM
7606put_value (bfd_vma size,
7607 unsigned long chunksz,
7608 bfd *input_bfd,
7609 bfd_vma x,
7610 bfd_byte *location)
d9352518
DB
7611{
7612 location += (size - chunksz);
7613
a0c8462f 7614 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
d9352518
DB
7615 {
7616 switch (chunksz)
7617 {
7618 default:
7619 case 0:
7620 abort ();
7621 case 1:
7622 bfd_put_8 (input_bfd, x, location);
7623 break;
7624 case 2:
7625 bfd_put_16 (input_bfd, x, location);
7626 break;
7627 case 4:
7628 bfd_put_32 (input_bfd, x, location);
7629 break;
7630 case 8:
7631#ifdef BFD64
7632 bfd_put_64 (input_bfd, x, location);
7633#else
7634 abort ();
7635#endif
7636 break;
7637 }
7638 }
7639}
7640
a0c8462f
AM
7641static bfd_vma
7642get_value (bfd_vma size,
7643 unsigned long chunksz,
7644 bfd *input_bfd,
7645 bfd_byte *location)
d9352518
DB
7646{
7647 bfd_vma x = 0;
7648
a0c8462f 7649 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
7650 {
7651 switch (chunksz)
7652 {
7653 default:
7654 case 0:
7655 abort ();
7656 case 1:
7657 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7658 break;
7659 case 2:
7660 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7661 break;
7662 case 4:
7663 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7664 break;
7665 case 8:
7666#ifdef BFD64
7667 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7668#else
7669 abort ();
7670#endif
7671 break;
7672 }
7673 }
7674 return x;
7675}
7676
a0c8462f
AM
7677static void
7678decode_complex_addend (unsigned long *start, /* in bits */
7679 unsigned long *oplen, /* in bits */
7680 unsigned long *len, /* in bits */
7681 unsigned long *wordsz, /* in bytes */
7682 unsigned long *chunksz, /* in bytes */
7683 unsigned long *lsb0_p,
7684 unsigned long *signed_p,
7685 unsigned long *trunc_p,
7686 unsigned long encoded)
d9352518
DB
7687{
7688 * start = encoded & 0x3F;
7689 * len = (encoded >> 6) & 0x3F;
7690 * oplen = (encoded >> 12) & 0x3F;
7691 * wordsz = (encoded >> 18) & 0xF;
7692 * chunksz = (encoded >> 22) & 0xF;
7693 * lsb0_p = (encoded >> 27) & 1;
7694 * signed_p = (encoded >> 28) & 1;
7695 * trunc_p = (encoded >> 29) & 1;
7696}
7697
cdfeee4f 7698bfd_reloc_status_type
0f02bbd9 7699bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 7700 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
7701 bfd_byte *contents,
7702 Elf_Internal_Rela *rel,
7703 bfd_vma relocation)
d9352518 7704{
0f02bbd9
AM
7705 bfd_vma shift, x, mask;
7706 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 7707 bfd_reloc_status_type r;
d9352518
DB
7708
7709 /* Perform this reloc, since it is complex.
7710 (this is not to say that it necessarily refers to a complex
7711 symbol; merely that it is a self-describing CGEN based reloc.
7712 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 7713 word size, etc) encoded within it.). */
d9352518 7714
a0c8462f
AM
7715 decode_complex_addend (&start, &oplen, &len, &wordsz,
7716 &chunksz, &lsb0_p, &signed_p,
7717 &trunc_p, rel->r_addend);
d9352518
DB
7718
7719 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7720
7721 if (lsb0_p)
7722 shift = (start + 1) - len;
7723 else
7724 shift = (8 * wordsz) - (start + len);
7725
a0c8462f 7726 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
7727
7728#ifdef DEBUG
7729 printf ("Doing complex reloc: "
7730 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7731 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7732 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7733 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7734 oplen, x, mask, relocation);
7735#endif
7736
cdfeee4f 7737 r = bfd_reloc_ok;
d9352518 7738 if (! trunc_p)
cdfeee4f
AM
7739 /* Now do an overflow check. */
7740 r = bfd_check_overflow ((signed_p
7741 ? complain_overflow_signed
7742 : complain_overflow_unsigned),
7743 len, 0, (8 * wordsz),
7744 relocation);
a0c8462f 7745
d9352518
DB
7746 /* Do the deed. */
7747 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7748
7749#ifdef DEBUG
7750 printf (" relocation: %8.8lx\n"
7751 " shifted mask: %8.8lx\n"
7752 " shifted/masked reloc: %8.8lx\n"
7753 " result: %8.8lx\n",
a0c8462f 7754 relocation, (mask << shift),
d9352518
DB
7755 ((relocation & mask) << shift), x);
7756#endif
7757 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 7758 return r;
d9352518
DB
7759}
7760
c152c796
AM
7761/* When performing a relocatable link, the input relocations are
7762 preserved. But, if they reference global symbols, the indices
7763 referenced must be updated. Update all the relocations in
7764 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7765
7766static void
7767elf_link_adjust_relocs (bfd *abfd,
7768 Elf_Internal_Shdr *rel_hdr,
7769 unsigned int count,
7770 struct elf_link_hash_entry **rel_hash)
7771{
7772 unsigned int i;
7773 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7774 bfd_byte *erela;
7775 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7776 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7777 bfd_vma r_type_mask;
7778 int r_sym_shift;
7779
7780 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7781 {
7782 swap_in = bed->s->swap_reloc_in;
7783 swap_out = bed->s->swap_reloc_out;
7784 }
7785 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7786 {
7787 swap_in = bed->s->swap_reloca_in;
7788 swap_out = bed->s->swap_reloca_out;
7789 }
7790 else
7791 abort ();
7792
7793 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7794 abort ();
7795
7796 if (bed->s->arch_size == 32)
7797 {
7798 r_type_mask = 0xff;
7799 r_sym_shift = 8;
7800 }
7801 else
7802 {
7803 r_type_mask = 0xffffffff;
7804 r_sym_shift = 32;
7805 }
7806
7807 erela = rel_hdr->contents;
7808 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7809 {
7810 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7811 unsigned int j;
7812
7813 if (*rel_hash == NULL)
7814 continue;
7815
7816 BFD_ASSERT ((*rel_hash)->indx >= 0);
7817
7818 (*swap_in) (abfd, erela, irela);
7819 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7820 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7821 | (irela[j].r_info & r_type_mask));
7822 (*swap_out) (abfd, irela, erela);
7823 }
7824}
7825
7826struct elf_link_sort_rela
7827{
7828 union {
7829 bfd_vma offset;
7830 bfd_vma sym_mask;
7831 } u;
7832 enum elf_reloc_type_class type;
7833 /* We use this as an array of size int_rels_per_ext_rel. */
7834 Elf_Internal_Rela rela[1];
7835};
7836
7837static int
7838elf_link_sort_cmp1 (const void *A, const void *B)
7839{
7840 const struct elf_link_sort_rela *a = A;
7841 const struct elf_link_sort_rela *b = B;
7842 int relativea, relativeb;
7843
7844 relativea = a->type == reloc_class_relative;
7845 relativeb = b->type == reloc_class_relative;
7846
7847 if (relativea < relativeb)
7848 return 1;
7849 if (relativea > relativeb)
7850 return -1;
7851 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7852 return -1;
7853 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7854 return 1;
7855 if (a->rela->r_offset < b->rela->r_offset)
7856 return -1;
7857 if (a->rela->r_offset > b->rela->r_offset)
7858 return 1;
7859 return 0;
7860}
7861
7862static int
7863elf_link_sort_cmp2 (const void *A, const void *B)
7864{
7865 const struct elf_link_sort_rela *a = A;
7866 const struct elf_link_sort_rela *b = B;
7867 int copya, copyb;
7868
7869 if (a->u.offset < b->u.offset)
7870 return -1;
7871 if (a->u.offset > b->u.offset)
7872 return 1;
7873 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
7874 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
7875 if (copya < copyb)
7876 return -1;
7877 if (copya > copyb)
7878 return 1;
7879 if (a->rela->r_offset < b->rela->r_offset)
7880 return -1;
7881 if (a->rela->r_offset > b->rela->r_offset)
7882 return 1;
7883 return 0;
7884}
7885
7886static size_t
7887elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
7888{
3410fea8 7889 asection *dynamic_relocs;
fc66a176
L
7890 asection *rela_dyn;
7891 asection *rel_dyn;
c152c796
AM
7892 bfd_size_type count, size;
7893 size_t i, ret, sort_elt, ext_size;
7894 bfd_byte *sort, *s_non_relative, *p;
7895 struct elf_link_sort_rela *sq;
7896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7897 int i2e = bed->s->int_rels_per_ext_rel;
7898 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7899 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7900 struct bfd_link_order *lo;
7901 bfd_vma r_sym_mask;
3410fea8 7902 bfd_boolean use_rela;
c152c796 7903
3410fea8
NC
7904 /* Find a dynamic reloc section. */
7905 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
7906 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
7907 if (rela_dyn != NULL && rela_dyn->size > 0
7908 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 7909 {
3410fea8
NC
7910 bfd_boolean use_rela_initialised = FALSE;
7911
7912 /* This is just here to stop gcc from complaining.
7913 It's initialization checking code is not perfect. */
7914 use_rela = TRUE;
7915
7916 /* Both sections are present. Examine the sizes
7917 of the indirect sections to help us choose. */
7918 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7919 if (lo->type == bfd_indirect_link_order)
7920 {
7921 asection *o = lo->u.indirect.section;
7922
7923 if ((o->size % bed->s->sizeof_rela) == 0)
7924 {
7925 if ((o->size % bed->s->sizeof_rel) == 0)
7926 /* Section size is divisible by both rel and rela sizes.
7927 It is of no help to us. */
7928 ;
7929 else
7930 {
7931 /* Section size is only divisible by rela. */
7932 if (use_rela_initialised && (use_rela == FALSE))
7933 {
7934 _bfd_error_handler
7935 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7936 bfd_set_error (bfd_error_invalid_operation);
7937 return 0;
7938 }
7939 else
7940 {
7941 use_rela = TRUE;
7942 use_rela_initialised = TRUE;
7943 }
7944 }
7945 }
7946 else if ((o->size % bed->s->sizeof_rel) == 0)
7947 {
7948 /* Section size is only divisible by rel. */
7949 if (use_rela_initialised && (use_rela == TRUE))
7950 {
7951 _bfd_error_handler
7952 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7953 bfd_set_error (bfd_error_invalid_operation);
7954 return 0;
7955 }
7956 else
7957 {
7958 use_rela = FALSE;
7959 use_rela_initialised = TRUE;
7960 }
7961 }
7962 else
7963 {
7964 /* The section size is not divisible by either - something is wrong. */
7965 _bfd_error_handler
7966 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
7967 bfd_set_error (bfd_error_invalid_operation);
7968 return 0;
7969 }
7970 }
7971
7972 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
7973 if (lo->type == bfd_indirect_link_order)
7974 {
7975 asection *o = lo->u.indirect.section;
7976
7977 if ((o->size % bed->s->sizeof_rela) == 0)
7978 {
7979 if ((o->size % bed->s->sizeof_rel) == 0)
7980 /* Section size is divisible by both rel and rela sizes.
7981 It is of no help to us. */
7982 ;
7983 else
7984 {
7985 /* Section size is only divisible by rela. */
7986 if (use_rela_initialised && (use_rela == FALSE))
7987 {
7988 _bfd_error_handler
7989 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
7990 bfd_set_error (bfd_error_invalid_operation);
7991 return 0;
7992 }
7993 else
7994 {
7995 use_rela = TRUE;
7996 use_rela_initialised = TRUE;
7997 }
7998 }
7999 }
8000 else if ((o->size % bed->s->sizeof_rel) == 0)
8001 {
8002 /* Section size is only divisible by rel. */
8003 if (use_rela_initialised && (use_rela == TRUE))
8004 {
8005 _bfd_error_handler
8006 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8007 bfd_set_error (bfd_error_invalid_operation);
8008 return 0;
8009 }
8010 else
8011 {
8012 use_rela = FALSE;
8013 use_rela_initialised = TRUE;
8014 }
8015 }
8016 else
8017 {
8018 /* The section size is not divisible by either - something is wrong. */
8019 _bfd_error_handler
8020 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8021 bfd_set_error (bfd_error_invalid_operation);
8022 return 0;
8023 }
8024 }
8025
8026 if (! use_rela_initialised)
8027 /* Make a guess. */
8028 use_rela = TRUE;
c152c796 8029 }
fc66a176
L
8030 else if (rela_dyn != NULL && rela_dyn->size > 0)
8031 use_rela = TRUE;
8032 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8033 use_rela = FALSE;
c152c796 8034 else
fc66a176 8035 return 0;
3410fea8
NC
8036
8037 if (use_rela)
c152c796 8038 {
3410fea8 8039 dynamic_relocs = rela_dyn;
c152c796
AM
8040 ext_size = bed->s->sizeof_rela;
8041 swap_in = bed->s->swap_reloca_in;
8042 swap_out = bed->s->swap_reloca_out;
8043 }
3410fea8
NC
8044 else
8045 {
8046 dynamic_relocs = rel_dyn;
8047 ext_size = bed->s->sizeof_rel;
8048 swap_in = bed->s->swap_reloc_in;
8049 swap_out = bed->s->swap_reloc_out;
8050 }
c152c796
AM
8051
8052 size = 0;
3410fea8 8053 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8054 if (lo->type == bfd_indirect_link_order)
3410fea8 8055 size += lo->u.indirect.section->size;
c152c796 8056
3410fea8 8057 if (size != dynamic_relocs->size)
c152c796
AM
8058 return 0;
8059
8060 sort_elt = (sizeof (struct elf_link_sort_rela)
8061 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8062
8063 count = dynamic_relocs->size / ext_size;
c152c796 8064 sort = bfd_zmalloc (sort_elt * count);
3410fea8 8065
c152c796
AM
8066 if (sort == NULL)
8067 {
8068 (*info->callbacks->warning)
8069 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8070 return 0;
8071 }
8072
8073 if (bed->s->arch_size == 32)
8074 r_sym_mask = ~(bfd_vma) 0xff;
8075 else
8076 r_sym_mask = ~(bfd_vma) 0xffffffff;
8077
3410fea8 8078 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8079 if (lo->type == bfd_indirect_link_order)
8080 {
8081 bfd_byte *erel, *erelend;
8082 asection *o = lo->u.indirect.section;
8083
1da212d6
AM
8084 if (o->contents == NULL && o->size != 0)
8085 {
8086 /* This is a reloc section that is being handled as a normal
8087 section. See bfd_section_from_shdr. We can't combine
8088 relocs in this case. */
8089 free (sort);
8090 return 0;
8091 }
c152c796 8092 erel = o->contents;
eea6121a 8093 erelend = o->contents + o->size;
c152c796 8094 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8095
c152c796
AM
8096 while (erel < erelend)
8097 {
8098 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8099
c152c796
AM
8100 (*swap_in) (abfd, erel, s->rela);
8101 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8102 s->u.sym_mask = r_sym_mask;
8103 p += sort_elt;
8104 erel += ext_size;
8105 }
8106 }
8107
8108 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8109
8110 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8111 {
8112 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8113 if (s->type != reloc_class_relative)
8114 break;
8115 }
8116 ret = i;
8117 s_non_relative = p;
8118
8119 sq = (struct elf_link_sort_rela *) s_non_relative;
8120 for (; i < count; i++, p += sort_elt)
8121 {
8122 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8123 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8124 sq = sp;
8125 sp->u.offset = sq->rela->r_offset;
8126 }
8127
8128 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8129
3410fea8 8130 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8131 if (lo->type == bfd_indirect_link_order)
8132 {
8133 bfd_byte *erel, *erelend;
8134 asection *o = lo->u.indirect.section;
8135
8136 erel = o->contents;
eea6121a 8137 erelend = o->contents + o->size;
c152c796
AM
8138 p = sort + o->output_offset / ext_size * sort_elt;
8139 while (erel < erelend)
8140 {
8141 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8142 (*swap_out) (abfd, s->rela, erel);
8143 p += sort_elt;
8144 erel += ext_size;
8145 }
8146 }
8147
8148 free (sort);
3410fea8 8149 *psec = dynamic_relocs;
c152c796
AM
8150 return ret;
8151}
8152
8153/* Flush the output symbols to the file. */
8154
8155static bfd_boolean
8156elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8157 const struct elf_backend_data *bed)
8158{
8159 if (finfo->symbuf_count > 0)
8160 {
8161 Elf_Internal_Shdr *hdr;
8162 file_ptr pos;
8163 bfd_size_type amt;
8164
8165 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8166 pos = hdr->sh_offset + hdr->sh_size;
8167 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8168 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8169 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8170 return FALSE;
8171
8172 hdr->sh_size += amt;
8173 finfo->symbuf_count = 0;
8174 }
8175
8176 return TRUE;
8177}
8178
8179/* Add a symbol to the output symbol table. */
8180
8181static bfd_boolean
8182elf_link_output_sym (struct elf_final_link_info *finfo,
8183 const char *name,
8184 Elf_Internal_Sym *elfsym,
8185 asection *input_sec,
8186 struct elf_link_hash_entry *h)
8187{
8188 bfd_byte *dest;
8189 Elf_External_Sym_Shndx *destshndx;
8190 bfd_boolean (*output_symbol_hook)
8191 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8192 struct elf_link_hash_entry *);
8193 const struct elf_backend_data *bed;
8194
8195 bed = get_elf_backend_data (finfo->output_bfd);
8196 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8197 if (output_symbol_hook != NULL)
8198 {
8199 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8200 return FALSE;
8201 }
8202
8203 if (name == NULL || *name == '\0')
8204 elfsym->st_name = 0;
8205 else if (input_sec->flags & SEC_EXCLUDE)
8206 elfsym->st_name = 0;
8207 else
8208 {
8209 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8210 name, TRUE, FALSE);
8211 if (elfsym->st_name == (unsigned long) -1)
8212 return FALSE;
8213 }
8214
8215 if (finfo->symbuf_count >= finfo->symbuf_size)
8216 {
8217 if (! elf_link_flush_output_syms (finfo, bed))
8218 return FALSE;
8219 }
8220
8221 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8222 destshndx = finfo->symshndxbuf;
8223 if (destshndx != NULL)
8224 {
8225 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8226 {
8227 bfd_size_type amt;
8228
8229 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
515ef31d 8230 destshndx = bfd_realloc (destshndx, amt * 2);
c152c796
AM
8231 if (destshndx == NULL)
8232 return FALSE;
515ef31d 8233 finfo->symshndxbuf = destshndx;
c152c796
AM
8234 memset ((char *) destshndx + amt, 0, amt);
8235 finfo->shndxbuf_size *= 2;
8236 }
8237 destshndx += bfd_get_symcount (finfo->output_bfd);
8238 }
8239
8240 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8241 finfo->symbuf_count += 1;
8242 bfd_get_symcount (finfo->output_bfd) += 1;
8243
8244 return TRUE;
8245}
8246
c0d5a53d
L
8247/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8248
8249static bfd_boolean
8250check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8251{
4fbb74a6
AM
8252 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8253 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8254 {
8255 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8256 beyond 64k. */
c0d5a53d
L
8257 (*_bfd_error_handler)
8258 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8259 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8260 bfd_set_error (bfd_error_nonrepresentable_section);
8261 return FALSE;
8262 }
8263 return TRUE;
8264}
8265
c152c796
AM
8266/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8267 allowing an unsatisfied unversioned symbol in the DSO to match a
8268 versioned symbol that would normally require an explicit version.
8269 We also handle the case that a DSO references a hidden symbol
8270 which may be satisfied by a versioned symbol in another DSO. */
8271
8272static bfd_boolean
8273elf_link_check_versioned_symbol (struct bfd_link_info *info,
8274 const struct elf_backend_data *bed,
8275 struct elf_link_hash_entry *h)
8276{
8277 bfd *abfd;
8278 struct elf_link_loaded_list *loaded;
8279
8280 if (!is_elf_hash_table (info->hash))
8281 return FALSE;
8282
8283 switch (h->root.type)
8284 {
8285 default:
8286 abfd = NULL;
8287 break;
8288
8289 case bfd_link_hash_undefined:
8290 case bfd_link_hash_undefweak:
8291 abfd = h->root.u.undef.abfd;
8292 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8293 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8294 return FALSE;
8295 break;
8296
8297 case bfd_link_hash_defined:
8298 case bfd_link_hash_defweak:
8299 abfd = h->root.u.def.section->owner;
8300 break;
8301
8302 case bfd_link_hash_common:
8303 abfd = h->root.u.c.p->section->owner;
8304 break;
8305 }
8306 BFD_ASSERT (abfd != NULL);
8307
8308 for (loaded = elf_hash_table (info)->loaded;
8309 loaded != NULL;
8310 loaded = loaded->next)
8311 {
8312 bfd *input;
8313 Elf_Internal_Shdr *hdr;
8314 bfd_size_type symcount;
8315 bfd_size_type extsymcount;
8316 bfd_size_type extsymoff;
8317 Elf_Internal_Shdr *versymhdr;
8318 Elf_Internal_Sym *isym;
8319 Elf_Internal_Sym *isymend;
8320 Elf_Internal_Sym *isymbuf;
8321 Elf_External_Versym *ever;
8322 Elf_External_Versym *extversym;
8323
8324 input = loaded->abfd;
8325
8326 /* We check each DSO for a possible hidden versioned definition. */
8327 if (input == abfd
8328 || (input->flags & DYNAMIC) == 0
8329 || elf_dynversym (input) == 0)
8330 continue;
8331
8332 hdr = &elf_tdata (input)->dynsymtab_hdr;
8333
8334 symcount = hdr->sh_size / bed->s->sizeof_sym;
8335 if (elf_bad_symtab (input))
8336 {
8337 extsymcount = symcount;
8338 extsymoff = 0;
8339 }
8340 else
8341 {
8342 extsymcount = symcount - hdr->sh_info;
8343 extsymoff = hdr->sh_info;
8344 }
8345
8346 if (extsymcount == 0)
8347 continue;
8348
8349 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8350 NULL, NULL, NULL);
8351 if (isymbuf == NULL)
8352 return FALSE;
8353
8354 /* Read in any version definitions. */
8355 versymhdr = &elf_tdata (input)->dynversym_hdr;
8356 extversym = bfd_malloc (versymhdr->sh_size);
8357 if (extversym == NULL)
8358 goto error_ret;
8359
8360 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8361 || (bfd_bread (extversym, versymhdr->sh_size, input)
8362 != versymhdr->sh_size))
8363 {
8364 free (extversym);
8365 error_ret:
8366 free (isymbuf);
8367 return FALSE;
8368 }
8369
8370 ever = extversym + extsymoff;
8371 isymend = isymbuf + extsymcount;
8372 for (isym = isymbuf; isym < isymend; isym++, ever++)
8373 {
8374 const char *name;
8375 Elf_Internal_Versym iver;
8376 unsigned short version_index;
8377
8378 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8379 || isym->st_shndx == SHN_UNDEF)
8380 continue;
8381
8382 name = bfd_elf_string_from_elf_section (input,
8383 hdr->sh_link,
8384 isym->st_name);
8385 if (strcmp (name, h->root.root.string) != 0)
8386 continue;
8387
8388 _bfd_elf_swap_versym_in (input, ever, &iver);
8389
8390 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8391 {
8392 /* If we have a non-hidden versioned sym, then it should
8393 have provided a definition for the undefined sym. */
8394 abort ();
8395 }
8396
8397 version_index = iver.vs_vers & VERSYM_VERSION;
8398 if (version_index == 1 || version_index == 2)
8399 {
8400 /* This is the base or first version. We can use it. */
8401 free (extversym);
8402 free (isymbuf);
8403 return TRUE;
8404 }
8405 }
8406
8407 free (extversym);
8408 free (isymbuf);
8409 }
8410
8411 return FALSE;
8412}
8413
8414/* Add an external symbol to the symbol table. This is called from
8415 the hash table traversal routine. When generating a shared object,
8416 we go through the symbol table twice. The first time we output
8417 anything that might have been forced to local scope in a version
8418 script. The second time we output the symbols that are still
8419 global symbols. */
8420
8421static bfd_boolean
8422elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8423{
8424 struct elf_outext_info *eoinfo = data;
8425 struct elf_final_link_info *finfo = eoinfo->finfo;
8426 bfd_boolean strip;
8427 Elf_Internal_Sym sym;
8428 asection *input_sec;
8429 const struct elf_backend_data *bed;
8430
8431 if (h->root.type == bfd_link_hash_warning)
8432 {
8433 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8434 if (h->root.type == bfd_link_hash_new)
8435 return TRUE;
8436 }
8437
8438 /* Decide whether to output this symbol in this pass. */
8439 if (eoinfo->localsyms)
8440 {
f5385ebf 8441 if (!h->forced_local)
c152c796
AM
8442 return TRUE;
8443 }
8444 else
8445 {
f5385ebf 8446 if (h->forced_local)
c152c796
AM
8447 return TRUE;
8448 }
8449
8450 bed = get_elf_backend_data (finfo->output_bfd);
8451
12ac1cf5 8452 if (h->root.type == bfd_link_hash_undefined)
c152c796 8453 {
12ac1cf5
NC
8454 /* If we have an undefined symbol reference here then it must have
8455 come from a shared library that is being linked in. (Undefined
8456 references in regular files have already been handled). */
8457 bfd_boolean ignore_undef = FALSE;
8458
8459 /* Some symbols may be special in that the fact that they're
8460 undefined can be safely ignored - let backend determine that. */
8461 if (bed->elf_backend_ignore_undef_symbol)
8462 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8463
8464 /* If we are reporting errors for this situation then do so now. */
8465 if (ignore_undef == FALSE
8466 && h->ref_dynamic
8467 && ! h->ref_regular
8468 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8469 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8470 {
12ac1cf5
NC
8471 if (! (finfo->info->callbacks->undefined_symbol
8472 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8473 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8474 {
8475 eoinfo->failed = TRUE;
8476 return FALSE;
8477 }
c152c796
AM
8478 }
8479 }
8480
8481 /* We should also warn if a forced local symbol is referenced from
8482 shared libraries. */
8483 if (! finfo->info->relocatable
8484 && (! finfo->info->shared)
f5385ebf
AM
8485 && h->forced_local
8486 && h->ref_dynamic
8487 && !h->dynamic_def
8488 && !h->dynamic_weak
c152c796
AM
8489 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8490 {
8491 (*_bfd_error_handler)
d003868e 8492 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
cfca085c
L
8493 finfo->output_bfd,
8494 h->root.u.def.section == bfd_abs_section_ptr
8495 ? finfo->output_bfd : h->root.u.def.section->owner,
c152c796
AM
8496 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8497 ? "internal"
8498 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
d003868e
AM
8499 ? "hidden" : "local",
8500 h->root.root.string);
c152c796
AM
8501 eoinfo->failed = TRUE;
8502 return FALSE;
8503 }
8504
8505 /* We don't want to output symbols that have never been mentioned by
8506 a regular file, or that we have been told to strip. However, if
8507 h->indx is set to -2, the symbol is used by a reloc and we must
8508 output it. */
8509 if (h->indx == -2)
8510 strip = FALSE;
f5385ebf 8511 else if ((h->def_dynamic
77cfaee6
AM
8512 || h->ref_dynamic
8513 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8514 && !h->def_regular
8515 && !h->ref_regular)
c152c796
AM
8516 strip = TRUE;
8517 else if (finfo->info->strip == strip_all)
8518 strip = TRUE;
8519 else if (finfo->info->strip == strip_some
8520 && bfd_hash_lookup (finfo->info->keep_hash,
8521 h->root.root.string, FALSE, FALSE) == NULL)
8522 strip = TRUE;
8523 else if (finfo->info->strip_discarded
8524 && (h->root.type == bfd_link_hash_defined
8525 || h->root.type == bfd_link_hash_defweak)
8526 && elf_discarded_section (h->root.u.def.section))
8527 strip = TRUE;
8528 else
8529 strip = FALSE;
8530
8531 /* If we're stripping it, and it's not a dynamic symbol, there's
8532 nothing else to do unless it is a forced local symbol. */
8533 if (strip
8534 && h->dynindx == -1
f5385ebf 8535 && !h->forced_local)
c152c796
AM
8536 return TRUE;
8537
8538 sym.st_value = 0;
8539 sym.st_size = h->size;
8540 sym.st_other = h->other;
f5385ebf 8541 if (h->forced_local)
c152c796
AM
8542 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8543 else if (h->root.type == bfd_link_hash_undefweak
8544 || h->root.type == bfd_link_hash_defweak)
8545 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8546 else
8547 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8548
8549 switch (h->root.type)
8550 {
8551 default:
8552 case bfd_link_hash_new:
8553 case bfd_link_hash_warning:
8554 abort ();
8555 return FALSE;
8556
8557 case bfd_link_hash_undefined:
8558 case bfd_link_hash_undefweak:
8559 input_sec = bfd_und_section_ptr;
8560 sym.st_shndx = SHN_UNDEF;
8561 break;
8562
8563 case bfd_link_hash_defined:
8564 case bfd_link_hash_defweak:
8565 {
8566 input_sec = h->root.u.def.section;
8567 if (input_sec->output_section != NULL)
8568 {
8569 sym.st_shndx =
8570 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8571 input_sec->output_section);
8572 if (sym.st_shndx == SHN_BAD)
8573 {
8574 (*_bfd_error_handler)
d003868e
AM
8575 (_("%B: could not find output section %A for input section %A"),
8576 finfo->output_bfd, input_sec->output_section, input_sec);
c152c796
AM
8577 eoinfo->failed = TRUE;
8578 return FALSE;
8579 }
8580
8581 /* ELF symbols in relocatable files are section relative,
8582 but in nonrelocatable files they are virtual
8583 addresses. */
8584 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8585 if (! finfo->info->relocatable)
8586 {
8587 sym.st_value += input_sec->output_section->vma;
8588 if (h->type == STT_TLS)
8589 {
430a16a5
NC
8590 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8591 if (tls_sec != NULL)
8592 sym.st_value -= tls_sec->vma;
8593 else
8594 {
8595 /* The TLS section may have been garbage collected. */
8596 BFD_ASSERT (finfo->info->gc_sections
8597 && !input_sec->gc_mark);
8598 }
c152c796
AM
8599 }
8600 }
8601 }
8602 else
8603 {
8604 BFD_ASSERT (input_sec->owner == NULL
8605 || (input_sec->owner->flags & DYNAMIC) != 0);
8606 sym.st_shndx = SHN_UNDEF;
8607 input_sec = bfd_und_section_ptr;
8608 }
8609 }
8610 break;
8611
8612 case bfd_link_hash_common:
8613 input_sec = h->root.u.c.p->section;
a4d8e49b 8614 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8615 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8616 break;
8617
8618 case bfd_link_hash_indirect:
8619 /* These symbols are created by symbol versioning. They point
8620 to the decorated version of the name. For example, if the
8621 symbol foo@@GNU_1.2 is the default, which should be used when
8622 foo is used with no version, then we add an indirect symbol
8623 foo which points to foo@@GNU_1.2. We ignore these symbols,
8624 since the indirected symbol is already in the hash table. */
8625 return TRUE;
8626 }
8627
8628 /* Give the processor backend a chance to tweak the symbol value,
8629 and also to finish up anything that needs to be done for this
8630 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8631 forced local syms when non-shared is due to a historical quirk. */
8632 if ((h->dynindx != -1
f5385ebf 8633 || h->forced_local)
c152c796
AM
8634 && ((finfo->info->shared
8635 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8636 || h->root.type != bfd_link_hash_undefweak))
f5385ebf 8637 || !h->forced_local)
c152c796
AM
8638 && elf_hash_table (finfo->info)->dynamic_sections_created)
8639 {
8640 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8641 (finfo->output_bfd, finfo->info, h, &sym)))
8642 {
8643 eoinfo->failed = TRUE;
8644 return FALSE;
8645 }
8646 }
8647
8648 /* If we are marking the symbol as undefined, and there are no
8649 non-weak references to this symbol from a regular object, then
8650 mark the symbol as weak undefined; if there are non-weak
8651 references, mark the symbol as strong. We can't do this earlier,
8652 because it might not be marked as undefined until the
8653 finish_dynamic_symbol routine gets through with it. */
8654 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8655 && h->ref_regular
c152c796
AM
8656 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8657 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8658 {
8659 int bindtype;
8660
f5385ebf 8661 if (h->ref_regular_nonweak)
c152c796
AM
8662 bindtype = STB_GLOBAL;
8663 else
8664 bindtype = STB_WEAK;
8665 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8666 }
8667
bda987c2
CD
8668 /* If this is a symbol defined in a dynamic library, don't use the
8669 symbol size from the dynamic library. Relinking an executable
8670 against a new library may introduce gratuitous changes in the
8671 executable's symbols if we keep the size. */
8672 if (sym.st_shndx == SHN_UNDEF
8673 && !h->def_regular
8674 && h->def_dynamic)
8675 sym.st_size = 0;
8676
c152c796
AM
8677 /* If a non-weak symbol with non-default visibility is not defined
8678 locally, it is a fatal error. */
8679 if (! finfo->info->relocatable
8680 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8681 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8682 && h->root.type == bfd_link_hash_undefined
f5385ebf 8683 && !h->def_regular)
c152c796
AM
8684 {
8685 (*_bfd_error_handler)
d003868e
AM
8686 (_("%B: %s symbol `%s' isn't defined"),
8687 finfo->output_bfd,
8688 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8689 ? "protected"
8690 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8691 ? "internal" : "hidden",
8692 h->root.root.string);
c152c796
AM
8693 eoinfo->failed = TRUE;
8694 return FALSE;
8695 }
8696
8697 /* If this symbol should be put in the .dynsym section, then put it
8698 there now. We already know the symbol index. We also fill in
8699 the entry in the .hash section. */
8700 if (h->dynindx != -1
8701 && elf_hash_table (finfo->info)->dynamic_sections_created)
8702 {
c152c796
AM
8703 bfd_byte *esym;
8704
8705 sym.st_name = h->dynstr_index;
8706 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8707 if (! check_dynsym (finfo->output_bfd, &sym))
8708 {
8709 eoinfo->failed = TRUE;
8710 return FALSE;
8711 }
c152c796
AM
8712 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8713
fdc90cb4
JJ
8714 if (finfo->hash_sec != NULL)
8715 {
8716 size_t hash_entry_size;
8717 bfd_byte *bucketpos;
8718 bfd_vma chain;
41198d0c
L
8719 size_t bucketcount;
8720 size_t bucket;
8721
8722 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8723 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8724
8725 hash_entry_size
8726 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8727 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8728 + (bucket + 2) * hash_entry_size);
8729 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8730 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8731 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8732 ((bfd_byte *) finfo->hash_sec->contents
8733 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8734 }
c152c796
AM
8735
8736 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8737 {
8738 Elf_Internal_Versym iversym;
8739 Elf_External_Versym *eversym;
8740
f5385ebf 8741 if (!h->def_regular)
c152c796
AM
8742 {
8743 if (h->verinfo.verdef == NULL)
8744 iversym.vs_vers = 0;
8745 else
8746 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8747 }
8748 else
8749 {
8750 if (h->verinfo.vertree == NULL)
8751 iversym.vs_vers = 1;
8752 else
8753 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8754 if (finfo->info->create_default_symver)
8755 iversym.vs_vers++;
c152c796
AM
8756 }
8757
f5385ebf 8758 if (h->hidden)
c152c796
AM
8759 iversym.vs_vers |= VERSYM_HIDDEN;
8760
8761 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8762 eversym += h->dynindx;
8763 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8764 }
8765 }
8766
8767 /* If we're stripping it, then it was just a dynamic symbol, and
8768 there's nothing else to do. */
8769 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8770 return TRUE;
8771
8772 h->indx = bfd_get_symcount (finfo->output_bfd);
8773
8774 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8775 {
8776 eoinfo->failed = TRUE;
8777 return FALSE;
8778 }
8779
8780 return TRUE;
8781}
8782
cdd3575c
AM
8783/* Return TRUE if special handling is done for relocs in SEC against
8784 symbols defined in discarded sections. */
8785
c152c796
AM
8786static bfd_boolean
8787elf_section_ignore_discarded_relocs (asection *sec)
8788{
8789 const struct elf_backend_data *bed;
8790
cdd3575c
AM
8791 switch (sec->sec_info_type)
8792 {
8793 case ELF_INFO_TYPE_STABS:
8794 case ELF_INFO_TYPE_EH_FRAME:
8795 return TRUE;
8796 default:
8797 break;
8798 }
c152c796
AM
8799
8800 bed = get_elf_backend_data (sec->owner);
8801 if (bed->elf_backend_ignore_discarded_relocs != NULL
8802 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8803 return TRUE;
8804
8805 return FALSE;
8806}
8807
9e66c942
AM
8808/* Return a mask saying how ld should treat relocations in SEC against
8809 symbols defined in discarded sections. If this function returns
8810 COMPLAIN set, ld will issue a warning message. If this function
8811 returns PRETEND set, and the discarded section was link-once and the
8812 same size as the kept link-once section, ld will pretend that the
8813 symbol was actually defined in the kept section. Otherwise ld will
8814 zero the reloc (at least that is the intent, but some cooperation by
8815 the target dependent code is needed, particularly for REL targets). */
8816
8a696751
AM
8817unsigned int
8818_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 8819{
9e66c942 8820 if (sec->flags & SEC_DEBUGGING)
69d54b1b 8821 return PRETEND;
cdd3575c
AM
8822
8823 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 8824 return 0;
cdd3575c
AM
8825
8826 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 8827 return 0;
cdd3575c 8828
9e66c942 8829 return COMPLAIN | PRETEND;
cdd3575c
AM
8830}
8831
3d7f7666
L
8832/* Find a match between a section and a member of a section group. */
8833
8834static asection *
c0f00686
L
8835match_group_member (asection *sec, asection *group,
8836 struct bfd_link_info *info)
3d7f7666
L
8837{
8838 asection *first = elf_next_in_group (group);
8839 asection *s = first;
8840
8841 while (s != NULL)
8842 {
c0f00686 8843 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
8844 return s;
8845
83180ade 8846 s = elf_next_in_group (s);
3d7f7666
L
8847 if (s == first)
8848 break;
8849 }
8850
8851 return NULL;
8852}
8853
01b3c8ab 8854/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
8855 to replace it. Return the replacement if it is OK. Otherwise return
8856 NULL. */
01b3c8ab
L
8857
8858asection *
c0f00686 8859_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
8860{
8861 asection *kept;
8862
8863 kept = sec->kept_section;
8864 if (kept != NULL)
8865 {
c2370991 8866 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 8867 kept = match_group_member (sec, kept, info);
1dd2625f
BW
8868 if (kept != NULL
8869 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
8870 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 8871 kept = NULL;
c2370991 8872 sec->kept_section = kept;
01b3c8ab
L
8873 }
8874 return kept;
8875}
8876
c152c796
AM
8877/* Link an input file into the linker output file. This function
8878 handles all the sections and relocations of the input file at once.
8879 This is so that we only have to read the local symbols once, and
8880 don't have to keep them in memory. */
8881
8882static bfd_boolean
8883elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
8884{
ece5ef60 8885 int (*relocate_section)
c152c796
AM
8886 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
8887 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
8888 bfd *output_bfd;
8889 Elf_Internal_Shdr *symtab_hdr;
8890 size_t locsymcount;
8891 size_t extsymoff;
8892 Elf_Internal_Sym *isymbuf;
8893 Elf_Internal_Sym *isym;
8894 Elf_Internal_Sym *isymend;
8895 long *pindex;
8896 asection **ppsection;
8897 asection *o;
8898 const struct elf_backend_data *bed;
c152c796
AM
8899 struct elf_link_hash_entry **sym_hashes;
8900
8901 output_bfd = finfo->output_bfd;
8902 bed = get_elf_backend_data (output_bfd);
8903 relocate_section = bed->elf_backend_relocate_section;
8904
8905 /* If this is a dynamic object, we don't want to do anything here:
8906 we don't want the local symbols, and we don't want the section
8907 contents. */
8908 if ((input_bfd->flags & DYNAMIC) != 0)
8909 return TRUE;
8910
c152c796
AM
8911 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8912 if (elf_bad_symtab (input_bfd))
8913 {
8914 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8915 extsymoff = 0;
8916 }
8917 else
8918 {
8919 locsymcount = symtab_hdr->sh_info;
8920 extsymoff = symtab_hdr->sh_info;
8921 }
8922
8923 /* Read the local symbols. */
8924 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8925 if (isymbuf == NULL && locsymcount != 0)
8926 {
8927 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8928 finfo->internal_syms,
8929 finfo->external_syms,
8930 finfo->locsym_shndx);
8931 if (isymbuf == NULL)
8932 return FALSE;
8933 }
8934
8935 /* Find local symbol sections and adjust values of symbols in
8936 SEC_MERGE sections. Write out those local symbols we know are
8937 going into the output file. */
8938 isymend = isymbuf + locsymcount;
8939 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
8940 isym < isymend;
8941 isym++, pindex++, ppsection++)
8942 {
8943 asection *isec;
8944 const char *name;
8945 Elf_Internal_Sym osym;
8946
8947 *pindex = -1;
8948
8949 if (elf_bad_symtab (input_bfd))
8950 {
8951 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
8952 {
8953 *ppsection = NULL;
8954 continue;
8955 }
8956 }
8957
8958 if (isym->st_shndx == SHN_UNDEF)
8959 isec = bfd_und_section_ptr;
c152c796
AM
8960 else if (isym->st_shndx == SHN_ABS)
8961 isec = bfd_abs_section_ptr;
8962 else if (isym->st_shndx == SHN_COMMON)
8963 isec = bfd_com_section_ptr;
8964 else
8965 {
cb33740c
AM
8966 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
8967 if (isec == NULL)
8968 {
8969 /* Don't attempt to output symbols with st_shnx in the
8970 reserved range other than SHN_ABS and SHN_COMMON. */
8971 *ppsection = NULL;
8972 continue;
8973 }
8974 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
8975 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
8976 isym->st_value =
8977 _bfd_merged_section_offset (output_bfd, &isec,
8978 elf_section_data (isec)->sec_info,
8979 isym->st_value);
c152c796
AM
8980 }
8981
8982 *ppsection = isec;
8983
8984 /* Don't output the first, undefined, symbol. */
8985 if (ppsection == finfo->sections)
8986 continue;
8987
8988 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
8989 {
8990 /* We never output section symbols. Instead, we use the
8991 section symbol of the corresponding section in the output
8992 file. */
8993 continue;
8994 }
8995
8996 /* If we are stripping all symbols, we don't want to output this
8997 one. */
8998 if (finfo->info->strip == strip_all)
8999 continue;
9000
9001 /* If we are discarding all local symbols, we don't want to
9002 output this one. If we are generating a relocatable output
9003 file, then some of the local symbols may be required by
9004 relocs; we output them below as we discover that they are
9005 needed. */
9006 if (finfo->info->discard == discard_all)
9007 continue;
9008
9009 /* If this symbol is defined in a section which we are
f02571c5
AM
9010 discarding, we don't need to keep it. */
9011 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9012 && isym->st_shndx < SHN_LORESERVE
9013 && bfd_section_removed_from_list (output_bfd,
9014 isec->output_section))
e75a280b
L
9015 continue;
9016
c152c796
AM
9017 /* Get the name of the symbol. */
9018 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9019 isym->st_name);
9020 if (name == NULL)
9021 return FALSE;
9022
9023 /* See if we are discarding symbols with this name. */
9024 if ((finfo->info->strip == strip_some
9025 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9026 == NULL))
9027 || (((finfo->info->discard == discard_sec_merge
9028 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9029 || finfo->info->discard == discard_l)
9030 && bfd_is_local_label_name (input_bfd, name)))
9031 continue;
9032
9033 /* If we get here, we are going to output this symbol. */
9034
9035 osym = *isym;
9036
9037 /* Adjust the section index for the output file. */
9038 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9039 isec->output_section);
9040 if (osym.st_shndx == SHN_BAD)
9041 return FALSE;
9042
9043 *pindex = bfd_get_symcount (output_bfd);
9044
9045 /* ELF symbols in relocatable files are section relative, but
9046 in executable files they are virtual addresses. Note that
9047 this code assumes that all ELF sections have an associated
9048 BFD section with a reasonable value for output_offset; below
9049 we assume that they also have a reasonable value for
9050 output_section. Any special sections must be set up to meet
9051 these requirements. */
9052 osym.st_value += isec->output_offset;
9053 if (! finfo->info->relocatable)
9054 {
9055 osym.st_value += isec->output_section->vma;
9056 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9057 {
9058 /* STT_TLS symbols are relative to PT_TLS segment base. */
9059 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9060 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9061 }
9062 }
9063
9064 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9065 return FALSE;
9066 }
9067
9068 /* Relocate the contents of each section. */
9069 sym_hashes = elf_sym_hashes (input_bfd);
9070 for (o = input_bfd->sections; o != NULL; o = o->next)
9071 {
9072 bfd_byte *contents;
9073
9074 if (! o->linker_mark)
9075 {
9076 /* This section was omitted from the link. */
9077 continue;
9078 }
9079
bcacc0f5
AM
9080 if (finfo->info->relocatable
9081 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9082 {
9083 /* Deal with the group signature symbol. */
9084 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9085 unsigned long symndx = sec_data->this_hdr.sh_info;
9086 asection *osec = o->output_section;
9087
9088 if (symndx >= locsymcount
9089 || (elf_bad_symtab (input_bfd)
9090 && finfo->sections[symndx] == NULL))
9091 {
9092 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9093 while (h->root.type == bfd_link_hash_indirect
9094 || h->root.type == bfd_link_hash_warning)
9095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9096 /* Arrange for symbol to be output. */
9097 h->indx = -2;
9098 elf_section_data (osec)->this_hdr.sh_info = -2;
9099 }
9100 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9101 {
9102 /* We'll use the output section target_index. */
9103 asection *sec = finfo->sections[symndx]->output_section;
9104 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9105 }
9106 else
9107 {
9108 if (finfo->indices[symndx] == -1)
9109 {
9110 /* Otherwise output the local symbol now. */
9111 Elf_Internal_Sym sym = isymbuf[symndx];
9112 asection *sec = finfo->sections[symndx]->output_section;
9113 const char *name;
9114
9115 name = bfd_elf_string_from_elf_section (input_bfd,
9116 symtab_hdr->sh_link,
9117 sym.st_name);
9118 if (name == NULL)
9119 return FALSE;
9120
9121 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9122 sec);
9123 if (sym.st_shndx == SHN_BAD)
9124 return FALSE;
9125
9126 sym.st_value += o->output_offset;
9127
9128 finfo->indices[symndx] = bfd_get_symcount (output_bfd);
9129 if (! elf_link_output_sym (finfo, name, &sym, o, NULL))
9130 return FALSE;
9131 }
9132 elf_section_data (osec)->this_hdr.sh_info
9133 = finfo->indices[symndx];
9134 }
9135 }
9136
c152c796 9137 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9138 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9139 continue;
9140
9141 if ((o->flags & SEC_LINKER_CREATED) != 0)
9142 {
9143 /* Section was created by _bfd_elf_link_create_dynamic_sections
9144 or somesuch. */
9145 continue;
9146 }
9147
9148 /* Get the contents of the section. They have been cached by a
9149 relaxation routine. Note that o is a section in an input
9150 file, so the contents field will not have been set by any of
9151 the routines which work on output files. */
9152 if (elf_section_data (o)->this_hdr.contents != NULL)
9153 contents = elf_section_data (o)->this_hdr.contents;
9154 else
9155 {
eea6121a
AM
9156 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9157
c152c796 9158 contents = finfo->contents;
eea6121a 9159 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
c152c796
AM
9160 return FALSE;
9161 }
9162
9163 if ((o->flags & SEC_RELOC) != 0)
9164 {
9165 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9166 Elf_Internal_Rela *rel, *relend;
c152c796
AM
9167 bfd_vma r_type_mask;
9168 int r_sym_shift;
0f02bbd9 9169 int action_discarded;
ece5ef60 9170 int ret;
c152c796
AM
9171
9172 /* Get the swapped relocs. */
9173 internal_relocs
9174 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9175 finfo->internal_relocs, FALSE);
9176 if (internal_relocs == NULL
9177 && o->reloc_count > 0)
9178 return FALSE;
9179
9180 if (bed->s->arch_size == 32)
9181 {
9182 r_type_mask = 0xff;
9183 r_sym_shift = 8;
9184 }
9185 else
9186 {
9187 r_type_mask = 0xffffffff;
9188 r_sym_shift = 32;
9189 }
9190
0f02bbd9 9191 action_discarded = -1;
c152c796 9192 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
9193 action_discarded = (*bed->action_discarded) (o);
9194
9195 /* Run through the relocs evaluating complex reloc symbols and
9196 looking for relocs against symbols from discarded sections
9197 or section symbols from removed link-once sections.
9198 Complain about relocs against discarded sections. Zero
9199 relocs against removed link-once sections. */
9200
9201 rel = internal_relocs;
9202 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9203 for ( ; rel < relend; rel++)
c152c796 9204 {
0f02bbd9
AM
9205 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9206 unsigned int s_type;
9207 asection **ps, *sec;
9208 struct elf_link_hash_entry *h = NULL;
9209 const char *sym_name;
c152c796 9210
0f02bbd9
AM
9211 if (r_symndx == STN_UNDEF)
9212 continue;
c152c796 9213
0f02bbd9
AM
9214 if (r_symndx >= locsymcount
9215 || (elf_bad_symtab (input_bfd)
9216 && finfo->sections[r_symndx] == NULL))
9217 {
9218 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 9219
0f02bbd9
AM
9220 /* Badly formatted input files can contain relocs that
9221 reference non-existant symbols. Check here so that
9222 we do not seg fault. */
9223 if (h == NULL)
c152c796 9224 {
0f02bbd9 9225 char buffer [32];
dce669a1 9226
0f02bbd9
AM
9227 sprintf_vma (buffer, rel->r_info);
9228 (*_bfd_error_handler)
9229 (_("error: %B contains a reloc (0x%s) for section %A "
9230 "that references a non-existent global symbol"),
9231 input_bfd, o, buffer);
9232 bfd_set_error (bfd_error_bad_value);
9233 return FALSE;
9234 }
3b36f7e6 9235
0f02bbd9
AM
9236 while (h->root.type == bfd_link_hash_indirect
9237 || h->root.type == bfd_link_hash_warning)
9238 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 9239
0f02bbd9 9240 s_type = h->type;
cdd3575c 9241
0f02bbd9
AM
9242 ps = NULL;
9243 if (h->root.type == bfd_link_hash_defined
9244 || h->root.type == bfd_link_hash_defweak)
9245 ps = &h->root.u.def.section;
9246
9247 sym_name = h->root.root.string;
9248 }
9249 else
9250 {
9251 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9252
9253 s_type = ELF_ST_TYPE (sym->st_info);
9254 ps = &finfo->sections[r_symndx];
9255 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9256 sym, *ps);
9257 }
c152c796 9258
0f02bbd9
AM
9259 if (s_type == STT_RELC || s_type == STT_SRELC)
9260 {
9261 bfd_vma val;
9262 bfd_vma dot = (rel->r_offset
9263 + o->output_offset + o->output_section->vma);
9264#ifdef DEBUG
9265 printf ("Encountered a complex symbol!");
9266 printf (" (input_bfd %s, section %s, reloc %ld\n",
9267 input_bfd->filename, o->name, rel - internal_relocs);
9268 printf (" symbol: idx %8.8lx, name %s\n",
9269 r_symndx, sym_name);
9270 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9271 (unsigned long) rel->r_info,
9272 (unsigned long) rel->r_offset);
9273#endif
9274 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9275 isymbuf, locsymcount, s_type == STT_SRELC))
9276 return FALSE;
9277
9278 /* Symbol evaluated OK. Update to absolute value. */
9279 set_symbol_value (input_bfd, isymbuf, locsymcount,
9280 r_symndx, val);
9281 continue;
9282 }
9283
9284 if (action_discarded != -1 && ps != NULL)
9285 {
cdd3575c
AM
9286 /* Complain if the definition comes from a
9287 discarded section. */
9288 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9289 {
87e5235d 9290 BFD_ASSERT (r_symndx != 0);
0f02bbd9 9291 if (action_discarded & COMPLAIN)
e1fffbe6
AM
9292 (*finfo->info->callbacks->einfo)
9293 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9294 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9295 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9296
87e5235d 9297 /* Try to do the best we can to support buggy old
e0ae6d6f 9298 versions of gcc. Pretend that the symbol is
87e5235d
AM
9299 really defined in the kept linkonce section.
9300 FIXME: This is quite broken. Modifying the
9301 symbol here means we will be changing all later
e0ae6d6f 9302 uses of the symbol, not just in this section. */
0f02bbd9 9303 if (action_discarded & PRETEND)
87e5235d 9304 {
01b3c8ab
L
9305 asection *kept;
9306
c0f00686
L
9307 kept = _bfd_elf_check_kept_section (sec,
9308 finfo->info);
01b3c8ab 9309 if (kept != NULL)
87e5235d
AM
9310 {
9311 *ps = kept;
9312 continue;
9313 }
9314 }
c152c796
AM
9315 }
9316 }
9317 }
9318
9319 /* Relocate the section by invoking a back end routine.
9320
9321 The back end routine is responsible for adjusting the
9322 section contents as necessary, and (if using Rela relocs
9323 and generating a relocatable output file) adjusting the
9324 reloc addend as necessary.
9325
9326 The back end routine does not have to worry about setting
9327 the reloc address or the reloc symbol index.
9328
9329 The back end routine is given a pointer to the swapped in
9330 internal symbols, and can access the hash table entries
9331 for the external symbols via elf_sym_hashes (input_bfd).
9332
9333 When generating relocatable output, the back end routine
9334 must handle STB_LOCAL/STT_SECTION symbols specially. The
9335 output symbol is going to be a section symbol
9336 corresponding to the output section, which will require
9337 the addend to be adjusted. */
9338
ece5ef60 9339 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9340 input_bfd, o, contents,
9341 internal_relocs,
9342 isymbuf,
ece5ef60
AM
9343 finfo->sections);
9344 if (!ret)
c152c796
AM
9345 return FALSE;
9346
ece5ef60
AM
9347 if (ret == 2
9348 || finfo->info->relocatable
9349 || finfo->info->emitrelocations)
c152c796
AM
9350 {
9351 Elf_Internal_Rela *irela;
9352 Elf_Internal_Rela *irelaend;
9353 bfd_vma last_offset;
9354 struct elf_link_hash_entry **rel_hash;
eac338cf 9355 struct elf_link_hash_entry **rel_hash_list;
c152c796
AM
9356 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9357 unsigned int next_erel;
c152c796
AM
9358 bfd_boolean rela_normal;
9359
9360 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9361 rela_normal = (bed->rela_normal
9362 && (input_rel_hdr->sh_entsize
9363 == bed->s->sizeof_rela));
9364
9365 /* Adjust the reloc addresses and symbol indices. */
9366
9367 irela = internal_relocs;
9368 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9369 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9370 + elf_section_data (o->output_section)->rel_count
9371 + elf_section_data (o->output_section)->rel_count2);
eac338cf 9372 rel_hash_list = rel_hash;
c152c796
AM
9373 last_offset = o->output_offset;
9374 if (!finfo->info->relocatable)
9375 last_offset += o->output_section->vma;
9376 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9377 {
9378 unsigned long r_symndx;
9379 asection *sec;
9380 Elf_Internal_Sym sym;
9381
9382 if (next_erel == bed->s->int_rels_per_ext_rel)
9383 {
9384 rel_hash++;
9385 next_erel = 0;
9386 }
9387
9388 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9389 finfo->info, o,
9390 irela->r_offset);
9391 if (irela->r_offset >= (bfd_vma) -2)
9392 {
9393 /* This is a reloc for a deleted entry or somesuch.
9394 Turn it into an R_*_NONE reloc, at the same
9395 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9396 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9397 being ordered. */
9398 irela->r_offset = last_offset;
9399 irela->r_info = 0;
9400 irela->r_addend = 0;
9401 continue;
9402 }
9403
9404 irela->r_offset += o->output_offset;
9405
9406 /* Relocs in an executable have to be virtual addresses. */
9407 if (!finfo->info->relocatable)
9408 irela->r_offset += o->output_section->vma;
9409
9410 last_offset = irela->r_offset;
9411
9412 r_symndx = irela->r_info >> r_sym_shift;
9413 if (r_symndx == STN_UNDEF)
9414 continue;
9415
9416 if (r_symndx >= locsymcount
9417 || (elf_bad_symtab (input_bfd)
9418 && finfo->sections[r_symndx] == NULL))
9419 {
9420 struct elf_link_hash_entry *rh;
9421 unsigned long indx;
9422
9423 /* This is a reloc against a global symbol. We
9424 have not yet output all the local symbols, so
9425 we do not know the symbol index of any global
9426 symbol. We set the rel_hash entry for this
9427 reloc to point to the global hash table entry
9428 for this symbol. The symbol index is then
ee75fd95 9429 set at the end of bfd_elf_final_link. */
c152c796
AM
9430 indx = r_symndx - extsymoff;
9431 rh = elf_sym_hashes (input_bfd)[indx];
9432 while (rh->root.type == bfd_link_hash_indirect
9433 || rh->root.type == bfd_link_hash_warning)
9434 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9435
9436 /* Setting the index to -2 tells
9437 elf_link_output_extsym that this symbol is
9438 used by a reloc. */
9439 BFD_ASSERT (rh->indx < 0);
9440 rh->indx = -2;
9441
9442 *rel_hash = rh;
9443
9444 continue;
9445 }
9446
9447 /* This is a reloc against a local symbol. */
9448
9449 *rel_hash = NULL;
9450 sym = isymbuf[r_symndx];
9451 sec = finfo->sections[r_symndx];
9452 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9453 {
9454 /* I suppose the backend ought to fill in the
9455 section of any STT_SECTION symbol against a
6a8d1586
AM
9456 processor specific section. */
9457 r_symndx = 0;
9458 if (bfd_is_abs_section (sec))
9459 ;
c152c796
AM
9460 else if (sec == NULL || sec->owner == NULL)
9461 {
9462 bfd_set_error (bfd_error_bad_value);
9463 return FALSE;
9464 }
9465 else
9466 {
6a8d1586
AM
9467 asection *osec = sec->output_section;
9468
9469 /* If we have discarded a section, the output
9470 section will be the absolute section. In
ab96bf03
AM
9471 case of discarded SEC_MERGE sections, use
9472 the kept section. relocate_section should
9473 have already handled discarded linkonce
9474 sections. */
6a8d1586
AM
9475 if (bfd_is_abs_section (osec)
9476 && sec->kept_section != NULL
9477 && sec->kept_section->output_section != NULL)
9478 {
9479 osec = sec->kept_section->output_section;
9480 irela->r_addend -= osec->vma;
9481 }
9482
9483 if (!bfd_is_abs_section (osec))
9484 {
9485 r_symndx = osec->target_index;
74541ad4
AM
9486 if (r_symndx == 0)
9487 {
9488 struct elf_link_hash_table *htab;
9489 asection *oi;
9490
9491 htab = elf_hash_table (finfo->info);
9492 oi = htab->text_index_section;
9493 if ((osec->flags & SEC_READONLY) == 0
9494 && htab->data_index_section != NULL)
9495 oi = htab->data_index_section;
9496
9497 if (oi != NULL)
9498 {
9499 irela->r_addend += osec->vma - oi->vma;
9500 r_symndx = oi->target_index;
9501 }
9502 }
9503
6a8d1586
AM
9504 BFD_ASSERT (r_symndx != 0);
9505 }
c152c796
AM
9506 }
9507
9508 /* Adjust the addend according to where the
9509 section winds up in the output section. */
9510 if (rela_normal)
9511 irela->r_addend += sec->output_offset;
9512 }
9513 else
9514 {
9515 if (finfo->indices[r_symndx] == -1)
9516 {
9517 unsigned long shlink;
9518 const char *name;
9519 asection *osec;
9520
9521 if (finfo->info->strip == strip_all)
9522 {
9523 /* You can't do ld -r -s. */
9524 bfd_set_error (bfd_error_invalid_operation);
9525 return FALSE;
9526 }
9527
9528 /* This symbol was skipped earlier, but
9529 since it is needed by a reloc, we
9530 must output it now. */
9531 shlink = symtab_hdr->sh_link;
9532 name = (bfd_elf_string_from_elf_section
9533 (input_bfd, shlink, sym.st_name));
9534 if (name == NULL)
9535 return FALSE;
9536
9537 osec = sec->output_section;
9538 sym.st_shndx =
9539 _bfd_elf_section_from_bfd_section (output_bfd,
9540 osec);
9541 if (sym.st_shndx == SHN_BAD)
9542 return FALSE;
9543
9544 sym.st_value += sec->output_offset;
9545 if (! finfo->info->relocatable)
9546 {
9547 sym.st_value += osec->vma;
9548 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9549 {
9550 /* STT_TLS symbols are relative to PT_TLS
9551 segment base. */
9552 BFD_ASSERT (elf_hash_table (finfo->info)
9553 ->tls_sec != NULL);
9554 sym.st_value -= (elf_hash_table (finfo->info)
9555 ->tls_sec->vma);
9556 }
9557 }
9558
9559 finfo->indices[r_symndx]
9560 = bfd_get_symcount (output_bfd);
9561
9562 if (! elf_link_output_sym (finfo, name, &sym, sec,
9563 NULL))
9564 return FALSE;
9565 }
9566
9567 r_symndx = finfo->indices[r_symndx];
9568 }
9569
9570 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9571 | (irela->r_info & r_type_mask));
9572 }
9573
9574 /* Swap out the relocs. */
c152c796 9575 if (input_rel_hdr->sh_size != 0
eac338cf
PB
9576 && !bed->elf_backend_emit_relocs (output_bfd, o,
9577 input_rel_hdr,
9578 internal_relocs,
9579 rel_hash_list))
c152c796
AM
9580 return FALSE;
9581
9582 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9583 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9584 {
9585 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9586 * bed->s->int_rels_per_ext_rel);
eac338cf
PB
9587 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9588 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9589 input_rel_hdr2,
9590 internal_relocs,
9591 rel_hash_list))
c152c796
AM
9592 return FALSE;
9593 }
9594 }
9595 }
9596
9597 /* Write out the modified section contents. */
9598 if (bed->elf_backend_write_section
c7b8f16e
JB
9599 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9600 contents))
c152c796
AM
9601 {
9602 /* Section written out. */
9603 }
9604 else switch (o->sec_info_type)
9605 {
9606 case ELF_INFO_TYPE_STABS:
9607 if (! (_bfd_write_section_stabs
9608 (output_bfd,
9609 &elf_hash_table (finfo->info)->stab_info,
9610 o, &elf_section_data (o)->sec_info, contents)))
9611 return FALSE;
9612 break;
9613 case ELF_INFO_TYPE_MERGE:
9614 if (! _bfd_write_merged_section (output_bfd, o,
9615 elf_section_data (o)->sec_info))
9616 return FALSE;
9617 break;
9618 case ELF_INFO_TYPE_EH_FRAME:
9619 {
9620 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9621 o, contents))
9622 return FALSE;
9623 }
9624 break;
9625 default:
9626 {
c152c796 9627 if (! (o->flags & SEC_EXCLUDE)
ace79388 9628 && ! (o->output_section->flags & SEC_NEVER_LOAD)
c152c796
AM
9629 && ! bfd_set_section_contents (output_bfd, o->output_section,
9630 contents,
9631 (file_ptr) o->output_offset,
eea6121a 9632 o->size))
c152c796
AM
9633 return FALSE;
9634 }
9635 break;
9636 }
9637 }
9638
9639 return TRUE;
9640}
9641
9642/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9643 requested by the linker, and does not come from any input file. This
c152c796
AM
9644 is used to build constructor and destructor tables when linking
9645 with -Ur. */
9646
9647static bfd_boolean
9648elf_reloc_link_order (bfd *output_bfd,
9649 struct bfd_link_info *info,
9650 asection *output_section,
9651 struct bfd_link_order *link_order)
9652{
9653 reloc_howto_type *howto;
9654 long indx;
9655 bfd_vma offset;
9656 bfd_vma addend;
9657 struct elf_link_hash_entry **rel_hash_ptr;
9658 Elf_Internal_Shdr *rel_hdr;
9659 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9660 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9661 bfd_byte *erel;
9662 unsigned int i;
9663
9664 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9665 if (howto == NULL)
9666 {
9667 bfd_set_error (bfd_error_bad_value);
9668 return FALSE;
9669 }
9670
9671 addend = link_order->u.reloc.p->addend;
9672
9673 /* Figure out the symbol index. */
9674 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9675 + elf_section_data (output_section)->rel_count
9676 + elf_section_data (output_section)->rel_count2);
9677 if (link_order->type == bfd_section_reloc_link_order)
9678 {
9679 indx = link_order->u.reloc.p->u.section->target_index;
9680 BFD_ASSERT (indx != 0);
9681 *rel_hash_ptr = NULL;
9682 }
9683 else
9684 {
9685 struct elf_link_hash_entry *h;
9686
9687 /* Treat a reloc against a defined symbol as though it were
9688 actually against the section. */
9689 h = ((struct elf_link_hash_entry *)
9690 bfd_wrapped_link_hash_lookup (output_bfd, info,
9691 link_order->u.reloc.p->u.name,
9692 FALSE, FALSE, TRUE));
9693 if (h != NULL
9694 && (h->root.type == bfd_link_hash_defined
9695 || h->root.type == bfd_link_hash_defweak))
9696 {
9697 asection *section;
9698
9699 section = h->root.u.def.section;
9700 indx = section->output_section->target_index;
9701 *rel_hash_ptr = NULL;
9702 /* It seems that we ought to add the symbol value to the
9703 addend here, but in practice it has already been added
9704 because it was passed to constructor_callback. */
9705 addend += section->output_section->vma + section->output_offset;
9706 }
9707 else if (h != NULL)
9708 {
9709 /* Setting the index to -2 tells elf_link_output_extsym that
9710 this symbol is used by a reloc. */
9711 h->indx = -2;
9712 *rel_hash_ptr = h;
9713 indx = 0;
9714 }
9715 else
9716 {
9717 if (! ((*info->callbacks->unattached_reloc)
9718 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9719 return FALSE;
9720 indx = 0;
9721 }
9722 }
9723
9724 /* If this is an inplace reloc, we must write the addend into the
9725 object file. */
9726 if (howto->partial_inplace && addend != 0)
9727 {
9728 bfd_size_type size;
9729 bfd_reloc_status_type rstat;
9730 bfd_byte *buf;
9731 bfd_boolean ok;
9732 const char *sym_name;
9733
9734 size = bfd_get_reloc_size (howto);
9735 buf = bfd_zmalloc (size);
9736 if (buf == NULL)
9737 return FALSE;
9738 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9739 switch (rstat)
9740 {
9741 case bfd_reloc_ok:
9742 break;
9743
9744 default:
9745 case bfd_reloc_outofrange:
9746 abort ();
9747
9748 case bfd_reloc_overflow:
9749 if (link_order->type == bfd_section_reloc_link_order)
9750 sym_name = bfd_section_name (output_bfd,
9751 link_order->u.reloc.p->u.section);
9752 else
9753 sym_name = link_order->u.reloc.p->u.name;
9754 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
9755 (info, NULL, sym_name, howto->name, addend, NULL,
9756 NULL, (bfd_vma) 0)))
c152c796
AM
9757 {
9758 free (buf);
9759 return FALSE;
9760 }
9761 break;
9762 }
9763 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9764 link_order->offset, size);
9765 free (buf);
9766 if (! ok)
9767 return FALSE;
9768 }
9769
9770 /* The address of a reloc is relative to the section in a
9771 relocatable file, and is a virtual address in an executable
9772 file. */
9773 offset = link_order->offset;
9774 if (! info->relocatable)
9775 offset += output_section->vma;
9776
9777 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9778 {
9779 irel[i].r_offset = offset;
9780 irel[i].r_info = 0;
9781 irel[i].r_addend = 0;
9782 }
9783 if (bed->s->arch_size == 32)
9784 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9785 else
9786 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9787
9788 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9789 erel = rel_hdr->contents;
9790 if (rel_hdr->sh_type == SHT_REL)
9791 {
9792 erel += (elf_section_data (output_section)->rel_count
9793 * bed->s->sizeof_rel);
9794 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9795 }
9796 else
9797 {
9798 irel[0].r_addend = addend;
9799 erel += (elf_section_data (output_section)->rel_count
9800 * bed->s->sizeof_rela);
9801 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9802 }
9803
9804 ++elf_section_data (output_section)->rel_count;
9805
9806 return TRUE;
9807}
9808
0b52efa6
PB
9809
9810/* Get the output vma of the section pointed to by the sh_link field. */
9811
9812static bfd_vma
9813elf_get_linked_section_vma (struct bfd_link_order *p)
9814{
9815 Elf_Internal_Shdr **elf_shdrp;
9816 asection *s;
9817 int elfsec;
9818
9819 s = p->u.indirect.section;
9820 elf_shdrp = elf_elfsections (s->owner);
9821 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9822 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
9823 /* PR 290:
9824 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 9825 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
9826 sh_info fields. Hence we could get the situation
9827 where elfsec is 0. */
9828 if (elfsec == 0)
9829 {
9830 const struct elf_backend_data *bed
9831 = get_elf_backend_data (s->owner);
9832 if (bed->link_order_error_handler)
d003868e
AM
9833 bed->link_order_error_handler
9834 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
9835 return 0;
9836 }
9837 else
9838 {
9839 s = elf_shdrp[elfsec]->bfd_section;
9840 return s->output_section->vma + s->output_offset;
9841 }
0b52efa6
PB
9842}
9843
9844
9845/* Compare two sections based on the locations of the sections they are
9846 linked to. Used by elf_fixup_link_order. */
9847
9848static int
9849compare_link_order (const void * a, const void * b)
9850{
9851 bfd_vma apos;
9852 bfd_vma bpos;
9853
9854 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9855 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9856 if (apos < bpos)
9857 return -1;
9858 return apos > bpos;
9859}
9860
9861
9862/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9863 order as their linked sections. Returns false if this could not be done
9864 because an output section includes both ordered and unordered
9865 sections. Ideally we'd do this in the linker proper. */
9866
9867static bfd_boolean
9868elf_fixup_link_order (bfd *abfd, asection *o)
9869{
9870 int seen_linkorder;
9871 int seen_other;
9872 int n;
9873 struct bfd_link_order *p;
9874 bfd *sub;
9875 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 9876 unsigned elfsec;
0b52efa6 9877 struct bfd_link_order **sections;
d33cdfe3 9878 asection *s, *other_sec, *linkorder_sec;
0b52efa6 9879 bfd_vma offset;
3b36f7e6 9880
d33cdfe3
L
9881 other_sec = NULL;
9882 linkorder_sec = NULL;
0b52efa6
PB
9883 seen_other = 0;
9884 seen_linkorder = 0;
8423293d 9885 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 9886 {
d33cdfe3 9887 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
9888 {
9889 s = p->u.indirect.section;
d33cdfe3
L
9890 sub = s->owner;
9891 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9892 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
9893 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9894 && elfsec < elf_numsections (sub)
4fbb74a6
AM
9895 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
9896 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
9897 {
9898 seen_linkorder++;
9899 linkorder_sec = s;
9900 }
0b52efa6 9901 else
d33cdfe3
L
9902 {
9903 seen_other++;
9904 other_sec = s;
9905 }
0b52efa6
PB
9906 }
9907 else
9908 seen_other++;
d33cdfe3
L
9909
9910 if (seen_other && seen_linkorder)
9911 {
9912 if (other_sec && linkorder_sec)
9913 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9914 o, linkorder_sec,
9915 linkorder_sec->owner, other_sec,
9916 other_sec->owner);
9917 else
9918 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9919 o);
9920 bfd_set_error (bfd_error_bad_value);
9921 return FALSE;
9922 }
0b52efa6
PB
9923 }
9924
9925 if (!seen_linkorder)
9926 return TRUE;
9927
0b52efa6 9928 sections = (struct bfd_link_order **)
14b1c01e
AM
9929 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9930 if (sections == NULL)
9931 return FALSE;
0b52efa6 9932 seen_linkorder = 0;
3b36f7e6 9933
8423293d 9934 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
9935 {
9936 sections[seen_linkorder++] = p;
9937 }
9938 /* Sort the input sections in the order of their linked section. */
9939 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9940 compare_link_order);
9941
9942 /* Change the offsets of the sections. */
9943 offset = 0;
9944 for (n = 0; n < seen_linkorder; n++)
9945 {
9946 s = sections[n]->u.indirect.section;
461686a3 9947 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
9948 s->output_offset = offset;
9949 sections[n]->offset = offset;
9950 offset += sections[n]->size;
9951 }
9952
4dd07732 9953 free (sections);
0b52efa6
PB
9954 return TRUE;
9955}
9956
9957
c152c796
AM
9958/* Do the final step of an ELF link. */
9959
9960bfd_boolean
9961bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
9962{
9963 bfd_boolean dynamic;
9964 bfd_boolean emit_relocs;
9965 bfd *dynobj;
9966 struct elf_final_link_info finfo;
9967 register asection *o;
9968 register struct bfd_link_order *p;
9969 register bfd *sub;
9970 bfd_size_type max_contents_size;
9971 bfd_size_type max_external_reloc_size;
9972 bfd_size_type max_internal_reloc_count;
9973 bfd_size_type max_sym_count;
9974 bfd_size_type max_sym_shndx_count;
9975 file_ptr off;
9976 Elf_Internal_Sym elfsym;
9977 unsigned int i;
9978 Elf_Internal_Shdr *symtab_hdr;
9979 Elf_Internal_Shdr *symtab_shndx_hdr;
9980 Elf_Internal_Shdr *symstrtab_hdr;
9981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9982 struct elf_outext_info eoinfo;
9983 bfd_boolean merged;
9984 size_t relativecount = 0;
9985 asection *reldyn = 0;
9986 bfd_size_type amt;
104d59d1
JM
9987 asection *attr_section = NULL;
9988 bfd_vma attr_size = 0;
9989 const char *std_attrs_section;
c152c796
AM
9990
9991 if (! is_elf_hash_table (info->hash))
9992 return FALSE;
9993
9994 if (info->shared)
9995 abfd->flags |= DYNAMIC;
9996
9997 dynamic = elf_hash_table (info)->dynamic_sections_created;
9998 dynobj = elf_hash_table (info)->dynobj;
9999
10000 emit_relocs = (info->relocatable
a4676736 10001 || info->emitrelocations);
c152c796
AM
10002
10003 finfo.info = info;
10004 finfo.output_bfd = abfd;
10005 finfo.symstrtab = _bfd_elf_stringtab_init ();
10006 if (finfo.symstrtab == NULL)
10007 return FALSE;
10008
10009 if (! dynamic)
10010 {
10011 finfo.dynsym_sec = NULL;
10012 finfo.hash_sec = NULL;
10013 finfo.symver_sec = NULL;
10014 }
10015 else
10016 {
10017 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10018 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10019 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10020 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10021 /* Note that it is OK if symver_sec is NULL. */
10022 }
10023
10024 finfo.contents = NULL;
10025 finfo.external_relocs = NULL;
10026 finfo.internal_relocs = NULL;
10027 finfo.external_syms = NULL;
10028 finfo.locsym_shndx = NULL;
10029 finfo.internal_syms = NULL;
10030 finfo.indices = NULL;
10031 finfo.sections = NULL;
10032 finfo.symbuf = NULL;
10033 finfo.symshndxbuf = NULL;
10034 finfo.symbuf_count = 0;
10035 finfo.shndxbuf_size = 0;
10036
104d59d1
JM
10037 /* The object attributes have been merged. Remove the input
10038 sections from the link, and set the contents of the output
10039 secton. */
10040 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10041 for (o = abfd->sections; o != NULL; o = o->next)
10042 {
10043 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10044 || strcmp (o->name, ".gnu.attributes") == 0)
10045 {
10046 for (p = o->map_head.link_order; p != NULL; p = p->next)
10047 {
10048 asection *input_section;
10049
10050 if (p->type != bfd_indirect_link_order)
10051 continue;
10052 input_section = p->u.indirect.section;
10053 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10054 elf_link_input_bfd ignores this section. */
10055 input_section->flags &= ~SEC_HAS_CONTENTS;
10056 }
a0c8462f 10057
104d59d1
JM
10058 attr_size = bfd_elf_obj_attr_size (abfd);
10059 if (attr_size)
10060 {
10061 bfd_set_section_size (abfd, o, attr_size);
10062 attr_section = o;
10063 /* Skip this section later on. */
10064 o->map_head.link_order = NULL;
10065 }
10066 else
10067 o->flags |= SEC_EXCLUDE;
10068 }
10069 }
10070
c152c796
AM
10071 /* Count up the number of relocations we will output for each output
10072 section, so that we know the sizes of the reloc sections. We
10073 also figure out some maximum sizes. */
10074 max_contents_size = 0;
10075 max_external_reloc_size = 0;
10076 max_internal_reloc_count = 0;
10077 max_sym_count = 0;
10078 max_sym_shndx_count = 0;
10079 merged = FALSE;
10080 for (o = abfd->sections; o != NULL; o = o->next)
10081 {
10082 struct bfd_elf_section_data *esdo = elf_section_data (o);
10083 o->reloc_count = 0;
10084
8423293d 10085 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10086 {
10087 unsigned int reloc_count = 0;
10088 struct bfd_elf_section_data *esdi = NULL;
10089 unsigned int *rel_count1;
10090
10091 if (p->type == bfd_section_reloc_link_order
10092 || p->type == bfd_symbol_reloc_link_order)
10093 reloc_count = 1;
10094 else if (p->type == bfd_indirect_link_order)
10095 {
10096 asection *sec;
10097
10098 sec = p->u.indirect.section;
10099 esdi = elf_section_data (sec);
10100
10101 /* Mark all sections which are to be included in the
10102 link. This will normally be every section. We need
10103 to do this so that we can identify any sections which
10104 the linker has decided to not include. */
10105 sec->linker_mark = TRUE;
10106
10107 if (sec->flags & SEC_MERGE)
10108 merged = TRUE;
10109
10110 if (info->relocatable || info->emitrelocations)
10111 reloc_count = sec->reloc_count;
10112 else if (bed->elf_backend_count_relocs)
58217f29 10113 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 10114
eea6121a
AM
10115 if (sec->rawsize > max_contents_size)
10116 max_contents_size = sec->rawsize;
10117 if (sec->size > max_contents_size)
10118 max_contents_size = sec->size;
c152c796
AM
10119
10120 /* We are interested in just local symbols, not all
10121 symbols. */
10122 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10123 && (sec->owner->flags & DYNAMIC) == 0)
10124 {
10125 size_t sym_count;
10126
10127 if (elf_bad_symtab (sec->owner))
10128 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10129 / bed->s->sizeof_sym);
10130 else
10131 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10132
10133 if (sym_count > max_sym_count)
10134 max_sym_count = sym_count;
10135
10136 if (sym_count > max_sym_shndx_count
10137 && elf_symtab_shndx (sec->owner) != 0)
10138 max_sym_shndx_count = sym_count;
10139
10140 if ((sec->flags & SEC_RELOC) != 0)
10141 {
10142 size_t ext_size;
10143
10144 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10145 if (ext_size > max_external_reloc_size)
10146 max_external_reloc_size = ext_size;
10147 if (sec->reloc_count > max_internal_reloc_count)
10148 max_internal_reloc_count = sec->reloc_count;
10149 }
10150 }
10151 }
10152
10153 if (reloc_count == 0)
10154 continue;
10155
10156 o->reloc_count += reloc_count;
10157
10158 /* MIPS may have a mix of REL and RELA relocs on sections.
10159 To support this curious ABI we keep reloc counts in
10160 elf_section_data too. We must be careful to add the
10161 relocations from the input section to the right output
10162 count. FIXME: Get rid of one count. We have
10163 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10164 rel_count1 = &esdo->rel_count;
10165 if (esdi != NULL)
10166 {
10167 bfd_boolean same_size;
10168 bfd_size_type entsize1;
10169
10170 entsize1 = esdi->rel_hdr.sh_entsize;
10171 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10172 || entsize1 == bed->s->sizeof_rela);
10173 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10174
10175 if (!same_size)
10176 rel_count1 = &esdo->rel_count2;
10177
10178 if (esdi->rel_hdr2 != NULL)
10179 {
10180 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10181 unsigned int alt_count;
10182 unsigned int *rel_count2;
10183
10184 BFD_ASSERT (entsize2 != entsize1
10185 && (entsize2 == bed->s->sizeof_rel
10186 || entsize2 == bed->s->sizeof_rela));
10187
10188 rel_count2 = &esdo->rel_count2;
10189 if (!same_size)
10190 rel_count2 = &esdo->rel_count;
10191
10192 /* The following is probably too simplistic if the
10193 backend counts output relocs unusually. */
10194 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10195 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10196 *rel_count2 += alt_count;
10197 reloc_count -= alt_count;
10198 }
10199 }
10200 *rel_count1 += reloc_count;
10201 }
10202
10203 if (o->reloc_count > 0)
10204 o->flags |= SEC_RELOC;
10205 else
10206 {
10207 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10208 set it (this is probably a bug) and if it is set
10209 assign_section_numbers will create a reloc section. */
10210 o->flags &=~ SEC_RELOC;
10211 }
10212
10213 /* If the SEC_ALLOC flag is not set, force the section VMA to
10214 zero. This is done in elf_fake_sections as well, but forcing
10215 the VMA to 0 here will ensure that relocs against these
10216 sections are handled correctly. */
10217 if ((o->flags & SEC_ALLOC) == 0
10218 && ! o->user_set_vma)
10219 o->vma = 0;
10220 }
10221
10222 if (! info->relocatable && merged)
10223 elf_link_hash_traverse (elf_hash_table (info),
10224 _bfd_elf_link_sec_merge_syms, abfd);
10225
10226 /* Figure out the file positions for everything but the symbol table
10227 and the relocs. We set symcount to force assign_section_numbers
10228 to create a symbol table. */
10229 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10230 BFD_ASSERT (! abfd->output_has_begun);
10231 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10232 goto error_return;
10233
ee75fd95 10234 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10235 for (o = abfd->sections; o != NULL; o = o->next)
10236 {
10237 if ((o->flags & SEC_RELOC) != 0)
10238 {
10239 if (!(_bfd_elf_link_size_reloc_section
10240 (abfd, &elf_section_data (o)->rel_hdr, o)))
10241 goto error_return;
10242
10243 if (elf_section_data (o)->rel_hdr2
10244 && !(_bfd_elf_link_size_reloc_section
10245 (abfd, elf_section_data (o)->rel_hdr2, o)))
10246 goto error_return;
10247 }
10248
10249 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10250 to count upwards while actually outputting the relocations. */
10251 elf_section_data (o)->rel_count = 0;
10252 elf_section_data (o)->rel_count2 = 0;
10253 }
10254
10255 _bfd_elf_assign_file_positions_for_relocs (abfd);
10256
10257 /* We have now assigned file positions for all the sections except
10258 .symtab and .strtab. We start the .symtab section at the current
10259 file position, and write directly to it. We build the .strtab
10260 section in memory. */
10261 bfd_get_symcount (abfd) = 0;
10262 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10263 /* sh_name is set in prep_headers. */
10264 symtab_hdr->sh_type = SHT_SYMTAB;
10265 /* sh_flags, sh_addr and sh_size all start off zero. */
10266 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10267 /* sh_link is set in assign_section_numbers. */
10268 /* sh_info is set below. */
10269 /* sh_offset is set just below. */
72de5009 10270 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796
AM
10271
10272 off = elf_tdata (abfd)->next_file_pos;
10273 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10274
10275 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10276 incorrect. We do not yet know the size of the .symtab section.
10277 We correct next_file_pos below, after we do know the size. */
10278
10279 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10280 continuously seeking to the right position in the file. */
10281 if (! info->keep_memory || max_sym_count < 20)
10282 finfo.symbuf_size = 20;
10283 else
10284 finfo.symbuf_size = max_sym_count;
10285 amt = finfo.symbuf_size;
10286 amt *= bed->s->sizeof_sym;
10287 finfo.symbuf = bfd_malloc (amt);
10288 if (finfo.symbuf == NULL)
10289 goto error_return;
4fbb74a6 10290 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
c152c796
AM
10291 {
10292 /* Wild guess at number of output symbols. realloc'd as needed. */
10293 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10294 finfo.shndxbuf_size = amt;
10295 amt *= sizeof (Elf_External_Sym_Shndx);
10296 finfo.symshndxbuf = bfd_zmalloc (amt);
10297 if (finfo.symshndxbuf == NULL)
10298 goto error_return;
10299 }
10300
10301 /* Start writing out the symbol table. The first symbol is always a
10302 dummy symbol. */
10303 if (info->strip != strip_all
10304 || emit_relocs)
10305 {
10306 elfsym.st_value = 0;
10307 elfsym.st_size = 0;
10308 elfsym.st_info = 0;
10309 elfsym.st_other = 0;
10310 elfsym.st_shndx = SHN_UNDEF;
10311 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10312 NULL))
10313 goto error_return;
10314 }
10315
c152c796
AM
10316 /* Output a symbol for each section. We output these even if we are
10317 discarding local symbols, since they are used for relocs. These
10318 symbols have no names. We store the index of each one in the
10319 index field of the section, so that we can find it again when
10320 outputting relocs. */
10321 if (info->strip != strip_all
10322 || emit_relocs)
10323 {
10324 elfsym.st_size = 0;
10325 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10326 elfsym.st_other = 0;
f0b5bb34 10327 elfsym.st_value = 0;
c152c796
AM
10328 for (i = 1; i < elf_numsections (abfd); i++)
10329 {
10330 o = bfd_section_from_elf_index (abfd, i);
10331 if (o != NULL)
f0b5bb34
AM
10332 {
10333 o->target_index = bfd_get_symcount (abfd);
10334 elfsym.st_shndx = i;
10335 if (!info->relocatable)
10336 elfsym.st_value = o->vma;
10337 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10338 goto error_return;
10339 }
c152c796
AM
10340 }
10341 }
10342
10343 /* Allocate some memory to hold information read in from the input
10344 files. */
10345 if (max_contents_size != 0)
10346 {
10347 finfo.contents = bfd_malloc (max_contents_size);
10348 if (finfo.contents == NULL)
10349 goto error_return;
10350 }
10351
10352 if (max_external_reloc_size != 0)
10353 {
10354 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10355 if (finfo.external_relocs == NULL)
10356 goto error_return;
10357 }
10358
10359 if (max_internal_reloc_count != 0)
10360 {
10361 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10362 amt *= sizeof (Elf_Internal_Rela);
10363 finfo.internal_relocs = bfd_malloc (amt);
10364 if (finfo.internal_relocs == NULL)
10365 goto error_return;
10366 }
10367
10368 if (max_sym_count != 0)
10369 {
10370 amt = max_sym_count * bed->s->sizeof_sym;
10371 finfo.external_syms = bfd_malloc (amt);
10372 if (finfo.external_syms == NULL)
10373 goto error_return;
10374
10375 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10376 finfo.internal_syms = bfd_malloc (amt);
10377 if (finfo.internal_syms == NULL)
10378 goto error_return;
10379
10380 amt = max_sym_count * sizeof (long);
10381 finfo.indices = bfd_malloc (amt);
10382 if (finfo.indices == NULL)
10383 goto error_return;
10384
10385 amt = max_sym_count * sizeof (asection *);
10386 finfo.sections = bfd_malloc (amt);
10387 if (finfo.sections == NULL)
10388 goto error_return;
10389 }
10390
10391 if (max_sym_shndx_count != 0)
10392 {
10393 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10394 finfo.locsym_shndx = bfd_malloc (amt);
10395 if (finfo.locsym_shndx == NULL)
10396 goto error_return;
10397 }
10398
10399 if (elf_hash_table (info)->tls_sec)
10400 {
10401 bfd_vma base, end = 0;
10402 asection *sec;
10403
10404 for (sec = elf_hash_table (info)->tls_sec;
10405 sec && (sec->flags & SEC_THREAD_LOCAL);
10406 sec = sec->next)
10407 {
3a800eb9 10408 bfd_size_type size = sec->size;
c152c796 10409
3a800eb9
AM
10410 if (size == 0
10411 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10412 {
3a800eb9
AM
10413 struct bfd_link_order *o = sec->map_tail.link_order;
10414 if (o != NULL)
10415 size = o->offset + o->size;
c152c796
AM
10416 }
10417 end = sec->vma + size;
10418 }
10419 base = elf_hash_table (info)->tls_sec->vma;
10420 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10421 elf_hash_table (info)->tls_size = end - base;
10422 }
10423
0b52efa6
PB
10424 /* Reorder SHF_LINK_ORDER sections. */
10425 for (o = abfd->sections; o != NULL; o = o->next)
10426 {
10427 if (!elf_fixup_link_order (abfd, o))
10428 return FALSE;
10429 }
10430
c152c796
AM
10431 /* Since ELF permits relocations to be against local symbols, we
10432 must have the local symbols available when we do the relocations.
10433 Since we would rather only read the local symbols once, and we
10434 would rather not keep them in memory, we handle all the
10435 relocations for a single input file at the same time.
10436
10437 Unfortunately, there is no way to know the total number of local
10438 symbols until we have seen all of them, and the local symbol
10439 indices precede the global symbol indices. This means that when
10440 we are generating relocatable output, and we see a reloc against
10441 a global symbol, we can not know the symbol index until we have
10442 finished examining all the local symbols to see which ones we are
10443 going to output. To deal with this, we keep the relocations in
10444 memory, and don't output them until the end of the link. This is
10445 an unfortunate waste of memory, but I don't see a good way around
10446 it. Fortunately, it only happens when performing a relocatable
10447 link, which is not the common case. FIXME: If keep_memory is set
10448 we could write the relocs out and then read them again; I don't
10449 know how bad the memory loss will be. */
10450
10451 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10452 sub->output_has_begun = FALSE;
10453 for (o = abfd->sections; o != NULL; o = o->next)
10454 {
8423293d 10455 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10456 {
10457 if (p->type == bfd_indirect_link_order
10458 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10459 == bfd_target_elf_flavour)
10460 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10461 {
10462 if (! sub->output_has_begun)
10463 {
10464 if (! elf_link_input_bfd (&finfo, sub))
10465 goto error_return;
10466 sub->output_has_begun = TRUE;
10467 }
10468 }
10469 else if (p->type == bfd_section_reloc_link_order
10470 || p->type == bfd_symbol_reloc_link_order)
10471 {
10472 if (! elf_reloc_link_order (abfd, info, o, p))
10473 goto error_return;
10474 }
10475 else
10476 {
10477 if (! _bfd_default_link_order (abfd, info, o, p))
10478 goto error_return;
10479 }
10480 }
10481 }
10482
c0f00686
L
10483 /* Free symbol buffer if needed. */
10484 if (!info->reduce_memory_overheads)
10485 {
10486 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10487 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10488 && elf_tdata (sub)->symbuf)
c0f00686
L
10489 {
10490 free (elf_tdata (sub)->symbuf);
10491 elf_tdata (sub)->symbuf = NULL;
10492 }
10493 }
10494
c152c796
AM
10495 /* Output any global symbols that got converted to local in a
10496 version script or due to symbol visibility. We do this in a
10497 separate step since ELF requires all local symbols to appear
10498 prior to any global symbols. FIXME: We should only do this if
10499 some global symbols were, in fact, converted to become local.
10500 FIXME: Will this work correctly with the Irix 5 linker? */
10501 eoinfo.failed = FALSE;
10502 eoinfo.finfo = &finfo;
10503 eoinfo.localsyms = TRUE;
10504 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10505 &eoinfo);
10506 if (eoinfo.failed)
10507 return FALSE;
10508
4e617b1e
PB
10509 /* If backend needs to output some local symbols not present in the hash
10510 table, do it now. */
10511 if (bed->elf_backend_output_arch_local_syms)
10512 {
10513 typedef bfd_boolean (*out_sym_func)
10514 (void *, const char *, Elf_Internal_Sym *, asection *,
10515 struct elf_link_hash_entry *);
10516
10517 if (! ((*bed->elf_backend_output_arch_local_syms)
10518 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10519 return FALSE;
10520 }
10521
c152c796
AM
10522 /* That wrote out all the local symbols. Finish up the symbol table
10523 with the global symbols. Even if we want to strip everything we
10524 can, we still need to deal with those global symbols that got
10525 converted to local in a version script. */
10526
10527 /* The sh_info field records the index of the first non local symbol. */
10528 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10529
10530 if (dynamic
10531 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10532 {
10533 Elf_Internal_Sym sym;
10534 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10535 long last_local = 0;
10536
10537 /* Write out the section symbols for the output sections. */
67687978 10538 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10539 {
10540 asection *s;
10541
10542 sym.st_size = 0;
10543 sym.st_name = 0;
10544 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10545 sym.st_other = 0;
10546
10547 for (s = abfd->sections; s != NULL; s = s->next)
10548 {
10549 int indx;
10550 bfd_byte *dest;
10551 long dynindx;
10552
c152c796 10553 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10554 if (dynindx <= 0)
10555 continue;
10556 indx = elf_section_data (s)->this_idx;
c152c796
AM
10557 BFD_ASSERT (indx > 0);
10558 sym.st_shndx = indx;
c0d5a53d
L
10559 if (! check_dynsym (abfd, &sym))
10560 return FALSE;
c152c796
AM
10561 sym.st_value = s->vma;
10562 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10563 if (last_local < dynindx)
10564 last_local = dynindx;
c152c796
AM
10565 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10566 }
c152c796
AM
10567 }
10568
10569 /* Write out the local dynsyms. */
10570 if (elf_hash_table (info)->dynlocal)
10571 {
10572 struct elf_link_local_dynamic_entry *e;
10573 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10574 {
10575 asection *s;
10576 bfd_byte *dest;
10577
10578 sym.st_size = e->isym.st_size;
10579 sym.st_other = e->isym.st_other;
10580
10581 /* Copy the internal symbol as is.
10582 Note that we saved a word of storage and overwrote
10583 the original st_name with the dynstr_index. */
10584 sym = e->isym;
10585
cb33740c
AM
10586 s = bfd_section_from_elf_index (e->input_bfd,
10587 e->isym.st_shndx);
10588 if (s != NULL)
c152c796 10589 {
c152c796
AM
10590 sym.st_shndx =
10591 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10592 if (! check_dynsym (abfd, &sym))
10593 return FALSE;
c152c796
AM
10594 sym.st_value = (s->output_section->vma
10595 + s->output_offset
10596 + e->isym.st_value);
10597 }
10598
10599 if (last_local < e->dynindx)
10600 last_local = e->dynindx;
10601
10602 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10603 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10604 }
10605 }
10606
10607 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10608 last_local + 1;
10609 }
10610
10611 /* We get the global symbols from the hash table. */
10612 eoinfo.failed = FALSE;
10613 eoinfo.localsyms = FALSE;
10614 eoinfo.finfo = &finfo;
10615 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10616 &eoinfo);
10617 if (eoinfo.failed)
10618 return FALSE;
10619
10620 /* If backend needs to output some symbols not present in the hash
10621 table, do it now. */
10622 if (bed->elf_backend_output_arch_syms)
10623 {
10624 typedef bfd_boolean (*out_sym_func)
10625 (void *, const char *, Elf_Internal_Sym *, asection *,
10626 struct elf_link_hash_entry *);
10627
10628 if (! ((*bed->elf_backend_output_arch_syms)
10629 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10630 return FALSE;
10631 }
10632
10633 /* Flush all symbols to the file. */
10634 if (! elf_link_flush_output_syms (&finfo, bed))
10635 return FALSE;
10636
10637 /* Now we know the size of the symtab section. */
10638 off += symtab_hdr->sh_size;
10639
10640 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10641 if (symtab_shndx_hdr->sh_name != 0)
10642 {
10643 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10644 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10645 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10646 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10647 symtab_shndx_hdr->sh_size = amt;
10648
10649 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10650 off, TRUE);
10651
10652 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10653 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10654 return FALSE;
10655 }
10656
10657
10658 /* Finish up and write out the symbol string table (.strtab)
10659 section. */
10660 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10661 /* sh_name was set in prep_headers. */
10662 symstrtab_hdr->sh_type = SHT_STRTAB;
10663 symstrtab_hdr->sh_flags = 0;
10664 symstrtab_hdr->sh_addr = 0;
10665 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10666 symstrtab_hdr->sh_entsize = 0;
10667 symstrtab_hdr->sh_link = 0;
10668 symstrtab_hdr->sh_info = 0;
10669 /* sh_offset is set just below. */
10670 symstrtab_hdr->sh_addralign = 1;
10671
10672 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10673 elf_tdata (abfd)->next_file_pos = off;
10674
10675 if (bfd_get_symcount (abfd) > 0)
10676 {
10677 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10678 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10679 return FALSE;
10680 }
10681
10682 /* Adjust the relocs to have the correct symbol indices. */
10683 for (o = abfd->sections; o != NULL; o = o->next)
10684 {
10685 if ((o->flags & SEC_RELOC) == 0)
10686 continue;
10687
10688 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10689 elf_section_data (o)->rel_count,
10690 elf_section_data (o)->rel_hashes);
10691 if (elf_section_data (o)->rel_hdr2 != NULL)
10692 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10693 elf_section_data (o)->rel_count2,
10694 (elf_section_data (o)->rel_hashes
10695 + elf_section_data (o)->rel_count));
10696
10697 /* Set the reloc_count field to 0 to prevent write_relocs from
10698 trying to swap the relocs out itself. */
10699 o->reloc_count = 0;
10700 }
10701
10702 if (dynamic && info->combreloc && dynobj != NULL)
10703 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10704
10705 /* If we are linking against a dynamic object, or generating a
10706 shared library, finish up the dynamic linking information. */
10707 if (dynamic)
10708 {
10709 bfd_byte *dyncon, *dynconend;
10710
10711 /* Fix up .dynamic entries. */
10712 o = bfd_get_section_by_name (dynobj, ".dynamic");
10713 BFD_ASSERT (o != NULL);
10714
10715 dyncon = o->contents;
eea6121a 10716 dynconend = o->contents + o->size;
c152c796
AM
10717 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10718 {
10719 Elf_Internal_Dyn dyn;
10720 const char *name;
10721 unsigned int type;
10722
10723 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10724
10725 switch (dyn.d_tag)
10726 {
10727 default:
10728 continue;
10729 case DT_NULL:
10730 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10731 {
10732 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10733 {
10734 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10735 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10736 default: continue;
10737 }
10738 dyn.d_un.d_val = relativecount;
10739 relativecount = 0;
10740 break;
10741 }
10742 continue;
10743
10744 case DT_INIT:
10745 name = info->init_function;
10746 goto get_sym;
10747 case DT_FINI:
10748 name = info->fini_function;
10749 get_sym:
10750 {
10751 struct elf_link_hash_entry *h;
10752
10753 h = elf_link_hash_lookup (elf_hash_table (info), name,
10754 FALSE, FALSE, TRUE);
10755 if (h != NULL
10756 && (h->root.type == bfd_link_hash_defined
10757 || h->root.type == bfd_link_hash_defweak))
10758 {
bef26483 10759 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
10760 o = h->root.u.def.section;
10761 if (o->output_section != NULL)
bef26483 10762 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
10763 + o->output_offset);
10764 else
10765 {
10766 /* The symbol is imported from another shared
10767 library and does not apply to this one. */
bef26483 10768 dyn.d_un.d_ptr = 0;
c152c796
AM
10769 }
10770 break;
10771 }
10772 }
10773 continue;
10774
10775 case DT_PREINIT_ARRAYSZ:
10776 name = ".preinit_array";
10777 goto get_size;
10778 case DT_INIT_ARRAYSZ:
10779 name = ".init_array";
10780 goto get_size;
10781 case DT_FINI_ARRAYSZ:
10782 name = ".fini_array";
10783 get_size:
10784 o = bfd_get_section_by_name (abfd, name);
10785 if (o == NULL)
10786 {
10787 (*_bfd_error_handler)
d003868e 10788 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10789 goto error_return;
10790 }
eea6121a 10791 if (o->size == 0)
c152c796
AM
10792 (*_bfd_error_handler)
10793 (_("warning: %s section has zero size"), name);
eea6121a 10794 dyn.d_un.d_val = o->size;
c152c796
AM
10795 break;
10796
10797 case DT_PREINIT_ARRAY:
10798 name = ".preinit_array";
10799 goto get_vma;
10800 case DT_INIT_ARRAY:
10801 name = ".init_array";
10802 goto get_vma;
10803 case DT_FINI_ARRAY:
10804 name = ".fini_array";
10805 goto get_vma;
10806
10807 case DT_HASH:
10808 name = ".hash";
10809 goto get_vma;
fdc90cb4
JJ
10810 case DT_GNU_HASH:
10811 name = ".gnu.hash";
10812 goto get_vma;
c152c796
AM
10813 case DT_STRTAB:
10814 name = ".dynstr";
10815 goto get_vma;
10816 case DT_SYMTAB:
10817 name = ".dynsym";
10818 goto get_vma;
10819 case DT_VERDEF:
10820 name = ".gnu.version_d";
10821 goto get_vma;
10822 case DT_VERNEED:
10823 name = ".gnu.version_r";
10824 goto get_vma;
10825 case DT_VERSYM:
10826 name = ".gnu.version";
10827 get_vma:
10828 o = bfd_get_section_by_name (abfd, name);
10829 if (o == NULL)
10830 {
10831 (*_bfd_error_handler)
d003868e 10832 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10833 goto error_return;
10834 }
10835 dyn.d_un.d_ptr = o->vma;
10836 break;
10837
10838 case DT_REL:
10839 case DT_RELA:
10840 case DT_RELSZ:
10841 case DT_RELASZ:
10842 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10843 type = SHT_REL;
10844 else
10845 type = SHT_RELA;
10846 dyn.d_un.d_val = 0;
bef26483 10847 dyn.d_un.d_ptr = 0;
c152c796
AM
10848 for (i = 1; i < elf_numsections (abfd); i++)
10849 {
10850 Elf_Internal_Shdr *hdr;
10851
10852 hdr = elf_elfsections (abfd)[i];
10853 if (hdr->sh_type == type
10854 && (hdr->sh_flags & SHF_ALLOC) != 0)
10855 {
10856 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10857 dyn.d_un.d_val += hdr->sh_size;
10858 else
10859 {
bef26483
AM
10860 if (dyn.d_un.d_ptr == 0
10861 || hdr->sh_addr < dyn.d_un.d_ptr)
10862 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
10863 }
10864 }
10865 }
10866 break;
10867 }
10868 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10869 }
10870 }
10871
10872 /* If we have created any dynamic sections, then output them. */
10873 if (dynobj != NULL)
10874 {
10875 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10876 goto error_return;
10877
943284cc
DJ
10878 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10879 if (info->warn_shared_textrel && info->shared)
10880 {
10881 bfd_byte *dyncon, *dynconend;
10882
10883 /* Fix up .dynamic entries. */
10884 o = bfd_get_section_by_name (dynobj, ".dynamic");
10885 BFD_ASSERT (o != NULL);
10886
10887 dyncon = o->contents;
10888 dynconend = o->contents + o->size;
10889 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10890 {
10891 Elf_Internal_Dyn dyn;
10892
10893 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10894
10895 if (dyn.d_tag == DT_TEXTREL)
10896 {
a0c8462f 10897 info->callbacks->einfo
9267588c 10898 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
10899 break;
10900 }
10901 }
10902 }
10903
c152c796
AM
10904 for (o = dynobj->sections; o != NULL; o = o->next)
10905 {
10906 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10907 || o->size == 0
c152c796
AM
10908 || o->output_section == bfd_abs_section_ptr)
10909 continue;
10910 if ((o->flags & SEC_LINKER_CREATED) == 0)
10911 {
10912 /* At this point, we are only interested in sections
10913 created by _bfd_elf_link_create_dynamic_sections. */
10914 continue;
10915 }
3722b82f
AM
10916 if (elf_hash_table (info)->stab_info.stabstr == o)
10917 continue;
eea6121a
AM
10918 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10919 continue;
c152c796
AM
10920 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10921 != SHT_STRTAB)
10922 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10923 {
10924 if (! bfd_set_section_contents (abfd, o->output_section,
10925 o->contents,
10926 (file_ptr) o->output_offset,
eea6121a 10927 o->size))
c152c796
AM
10928 goto error_return;
10929 }
10930 else
10931 {
10932 /* The contents of the .dynstr section are actually in a
10933 stringtab. */
10934 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
10935 if (bfd_seek (abfd, off, SEEK_SET) != 0
10936 || ! _bfd_elf_strtab_emit (abfd,
10937 elf_hash_table (info)->dynstr))
10938 goto error_return;
10939 }
10940 }
10941 }
10942
10943 if (info->relocatable)
10944 {
10945 bfd_boolean failed = FALSE;
10946
10947 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
10948 if (failed)
10949 goto error_return;
10950 }
10951
10952 /* If we have optimized stabs strings, output them. */
3722b82f 10953 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
10954 {
10955 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
10956 goto error_return;
10957 }
10958
10959 if (info->eh_frame_hdr)
10960 {
10961 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
10962 goto error_return;
10963 }
10964
10965 if (finfo.symstrtab != NULL)
10966 _bfd_stringtab_free (finfo.symstrtab);
10967 if (finfo.contents != NULL)
10968 free (finfo.contents);
10969 if (finfo.external_relocs != NULL)
10970 free (finfo.external_relocs);
10971 if (finfo.internal_relocs != NULL)
10972 free (finfo.internal_relocs);
10973 if (finfo.external_syms != NULL)
10974 free (finfo.external_syms);
10975 if (finfo.locsym_shndx != NULL)
10976 free (finfo.locsym_shndx);
10977 if (finfo.internal_syms != NULL)
10978 free (finfo.internal_syms);
10979 if (finfo.indices != NULL)
10980 free (finfo.indices);
10981 if (finfo.sections != NULL)
10982 free (finfo.sections);
10983 if (finfo.symbuf != NULL)
10984 free (finfo.symbuf);
10985 if (finfo.symshndxbuf != NULL)
10986 free (finfo.symshndxbuf);
10987 for (o = abfd->sections; o != NULL; o = o->next)
10988 {
10989 if ((o->flags & SEC_RELOC) != 0
10990 && elf_section_data (o)->rel_hashes != NULL)
10991 free (elf_section_data (o)->rel_hashes);
10992 }
10993
10994 elf_tdata (abfd)->linker = TRUE;
10995
104d59d1
JM
10996 if (attr_section)
10997 {
10998 bfd_byte *contents = bfd_malloc (attr_size);
10999 if (contents == NULL)
d0f16d5e 11000 return FALSE; /* Bail out and fail. */
104d59d1
JM
11001 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11002 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11003 free (contents);
11004 }
11005
c152c796
AM
11006 return TRUE;
11007
11008 error_return:
11009 if (finfo.symstrtab != NULL)
11010 _bfd_stringtab_free (finfo.symstrtab);
11011 if (finfo.contents != NULL)
11012 free (finfo.contents);
11013 if (finfo.external_relocs != NULL)
11014 free (finfo.external_relocs);
11015 if (finfo.internal_relocs != NULL)
11016 free (finfo.internal_relocs);
11017 if (finfo.external_syms != NULL)
11018 free (finfo.external_syms);
11019 if (finfo.locsym_shndx != NULL)
11020 free (finfo.locsym_shndx);
11021 if (finfo.internal_syms != NULL)
11022 free (finfo.internal_syms);
11023 if (finfo.indices != NULL)
11024 free (finfo.indices);
11025 if (finfo.sections != NULL)
11026 free (finfo.sections);
11027 if (finfo.symbuf != NULL)
11028 free (finfo.symbuf);
11029 if (finfo.symshndxbuf != NULL)
11030 free (finfo.symshndxbuf);
11031 for (o = abfd->sections; o != NULL; o = o->next)
11032 {
11033 if ((o->flags & SEC_RELOC) != 0
11034 && elf_section_data (o)->rel_hashes != NULL)
11035 free (elf_section_data (o)->rel_hashes);
11036 }
11037
11038 return FALSE;
11039}
11040\f
5241d853
RS
11041/* Initialize COOKIE for input bfd ABFD. */
11042
11043static bfd_boolean
11044init_reloc_cookie (struct elf_reloc_cookie *cookie,
11045 struct bfd_link_info *info, bfd *abfd)
11046{
11047 Elf_Internal_Shdr *symtab_hdr;
11048 const struct elf_backend_data *bed;
11049
11050 bed = get_elf_backend_data (abfd);
11051 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11052
11053 cookie->abfd = abfd;
11054 cookie->sym_hashes = elf_sym_hashes (abfd);
11055 cookie->bad_symtab = elf_bad_symtab (abfd);
11056 if (cookie->bad_symtab)
11057 {
11058 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11059 cookie->extsymoff = 0;
11060 }
11061 else
11062 {
11063 cookie->locsymcount = symtab_hdr->sh_info;
11064 cookie->extsymoff = symtab_hdr->sh_info;
11065 }
11066
11067 if (bed->s->arch_size == 32)
11068 cookie->r_sym_shift = 8;
11069 else
11070 cookie->r_sym_shift = 32;
11071
11072 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11073 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11074 {
11075 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11076 cookie->locsymcount, 0,
11077 NULL, NULL, NULL);
11078 if (cookie->locsyms == NULL)
11079 {
11080 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11081 return FALSE;
11082 }
11083 if (info->keep_memory)
11084 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11085 }
11086 return TRUE;
11087}
11088
11089/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11090
11091static void
11092fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11093{
11094 Elf_Internal_Shdr *symtab_hdr;
11095
11096 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11097 if (cookie->locsyms != NULL
11098 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11099 free (cookie->locsyms);
11100}
11101
11102/* Initialize the relocation information in COOKIE for input section SEC
11103 of input bfd ABFD. */
11104
11105static bfd_boolean
11106init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11107 struct bfd_link_info *info, bfd *abfd,
11108 asection *sec)
11109{
11110 const struct elf_backend_data *bed;
11111
11112 if (sec->reloc_count == 0)
11113 {
11114 cookie->rels = NULL;
11115 cookie->relend = NULL;
11116 }
11117 else
11118 {
11119 bed = get_elf_backend_data (abfd);
11120
11121 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11122 info->keep_memory);
11123 if (cookie->rels == NULL)
11124 return FALSE;
11125 cookie->rel = cookie->rels;
11126 cookie->relend = (cookie->rels
11127 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11128 }
11129 cookie->rel = cookie->rels;
11130 return TRUE;
11131}
11132
11133/* Free the memory allocated by init_reloc_cookie_rels,
11134 if appropriate. */
11135
11136static void
11137fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11138 asection *sec)
11139{
11140 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11141 free (cookie->rels);
11142}
11143
11144/* Initialize the whole of COOKIE for input section SEC. */
11145
11146static bfd_boolean
11147init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11148 struct bfd_link_info *info,
11149 asection *sec)
11150{
11151 if (!init_reloc_cookie (cookie, info, sec->owner))
11152 goto error1;
11153 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11154 goto error2;
11155 return TRUE;
11156
11157 error2:
11158 fini_reloc_cookie (cookie, sec->owner);
11159 error1:
11160 return FALSE;
11161}
11162
11163/* Free the memory allocated by init_reloc_cookie_for_section,
11164 if appropriate. */
11165
11166static void
11167fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11168 asection *sec)
11169{
11170 fini_reloc_cookie_rels (cookie, sec);
11171 fini_reloc_cookie (cookie, sec->owner);
11172}
11173\f
c152c796
AM
11174/* Garbage collect unused sections. */
11175
07adf181
AM
11176/* Default gc_mark_hook. */
11177
11178asection *
11179_bfd_elf_gc_mark_hook (asection *sec,
11180 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11181 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11182 struct elf_link_hash_entry *h,
11183 Elf_Internal_Sym *sym)
11184{
11185 if (h != NULL)
11186 {
11187 switch (h->root.type)
11188 {
11189 case bfd_link_hash_defined:
11190 case bfd_link_hash_defweak:
11191 return h->root.u.def.section;
11192
11193 case bfd_link_hash_common:
11194 return h->root.u.c.p->section;
11195
11196 default:
11197 break;
11198 }
11199 }
11200 else
11201 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11202
11203 return NULL;
11204}
11205
5241d853
RS
11206/* COOKIE->rel describes a relocation against section SEC, which is
11207 a section we've decided to keep. Return the section that contains
11208 the relocation symbol, or NULL if no section contains it. */
11209
11210asection *
11211_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11212 elf_gc_mark_hook_fn gc_mark_hook,
11213 struct elf_reloc_cookie *cookie)
11214{
11215 unsigned long r_symndx;
11216 struct elf_link_hash_entry *h;
11217
11218 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11219 if (r_symndx == 0)
11220 return NULL;
11221
11222 if (r_symndx >= cookie->locsymcount
11223 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11224 {
11225 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11226 while (h->root.type == bfd_link_hash_indirect
11227 || h->root.type == bfd_link_hash_warning)
11228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11229 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11230 }
11231
11232 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11233 &cookie->locsyms[r_symndx]);
11234}
11235
11236/* COOKIE->rel describes a relocation against section SEC, which is
11237 a section we've decided to keep. Mark the section that contains
9d0a14d3 11238 the relocation symbol. */
5241d853
RS
11239
11240bfd_boolean
11241_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11242 asection *sec,
11243 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 11244 struct elf_reloc_cookie *cookie)
5241d853
RS
11245{
11246 asection *rsec;
11247
11248 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11249 if (rsec && !rsec->gc_mark)
11250 {
11251 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11252 rsec->gc_mark = 1;
5241d853
RS
11253 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11254 return FALSE;
11255 }
11256 return TRUE;
11257}
11258
07adf181
AM
11259/* The mark phase of garbage collection. For a given section, mark
11260 it and any sections in this section's group, and all the sections
11261 which define symbols to which it refers. */
11262
ccfa59ea
AM
11263bfd_boolean
11264_bfd_elf_gc_mark (struct bfd_link_info *info,
11265 asection *sec,
6a5bb875 11266 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11267{
11268 bfd_boolean ret;
9d0a14d3 11269 asection *group_sec, *eh_frame;
c152c796
AM
11270
11271 sec->gc_mark = 1;
11272
11273 /* Mark all the sections in the group. */
11274 group_sec = elf_section_data (sec)->next_in_group;
11275 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11276 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11277 return FALSE;
11278
11279 /* Look through the section relocs. */
11280 ret = TRUE;
9d0a14d3
RS
11281 eh_frame = elf_eh_frame_section (sec->owner);
11282 if ((sec->flags & SEC_RELOC) != 0
11283 && sec->reloc_count > 0
11284 && sec != eh_frame)
c152c796 11285 {
5241d853 11286 struct elf_reloc_cookie cookie;
c152c796 11287
5241d853
RS
11288 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11289 ret = FALSE;
c152c796 11290 else
c152c796 11291 {
5241d853 11292 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 11293 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
11294 {
11295 ret = FALSE;
11296 break;
11297 }
11298 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
11299 }
11300 }
9d0a14d3
RS
11301
11302 if (ret && eh_frame && elf_fde_list (sec))
11303 {
11304 struct elf_reloc_cookie cookie;
11305
11306 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11307 ret = FALSE;
11308 else
11309 {
11310 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11311 gc_mark_hook, &cookie))
11312 ret = FALSE;
11313 fini_reloc_cookie_for_section (&cookie, eh_frame);
11314 }
11315 }
11316
c152c796
AM
11317 return ret;
11318}
11319
11320/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11321
c17d87de
NC
11322struct elf_gc_sweep_symbol_info
11323{
ccabcbe5
AM
11324 struct bfd_link_info *info;
11325 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11326 bfd_boolean);
11327};
11328
c152c796 11329static bfd_boolean
ccabcbe5 11330elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11331{
c152c796
AM
11332 if (h->root.type == bfd_link_hash_warning)
11333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11334
ccabcbe5
AM
11335 if ((h->root.type == bfd_link_hash_defined
11336 || h->root.type == bfd_link_hash_defweak)
11337 && !h->root.u.def.section->gc_mark
11338 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11339 {
11340 struct elf_gc_sweep_symbol_info *inf = data;
11341 (*inf->hide_symbol) (inf->info, h, TRUE);
11342 }
c152c796
AM
11343
11344 return TRUE;
11345}
11346
11347/* The sweep phase of garbage collection. Remove all garbage sections. */
11348
11349typedef bfd_boolean (*gc_sweep_hook_fn)
11350 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11351
11352static bfd_boolean
ccabcbe5 11353elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11354{
11355 bfd *sub;
ccabcbe5
AM
11356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11357 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11358 unsigned long section_sym_count;
11359 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11360
11361 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11362 {
11363 asection *o;
11364
11365 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11366 continue;
11367
11368 for (o = sub->sections; o != NULL; o = o->next)
11369 {
7c2c8505
AM
11370 /* Keep debug and special sections. */
11371 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
dea5f36a 11372 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
c152c796
AM
11373 o->gc_mark = 1;
11374
11375 if (o->gc_mark)
11376 continue;
11377
11378 /* Skip sweeping sections already excluded. */
11379 if (o->flags & SEC_EXCLUDE)
11380 continue;
11381
11382 /* Since this is early in the link process, it is simple
11383 to remove a section from the output. */
11384 o->flags |= SEC_EXCLUDE;
11385
c55fe096 11386 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11387 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11388
c152c796
AM
11389 /* But we also have to update some of the relocation
11390 info we collected before. */
11391 if (gc_sweep_hook
e8aaee2a
AM
11392 && (o->flags & SEC_RELOC) != 0
11393 && o->reloc_count > 0
11394 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11395 {
11396 Elf_Internal_Rela *internal_relocs;
11397 bfd_boolean r;
11398
11399 internal_relocs
11400 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11401 info->keep_memory);
11402 if (internal_relocs == NULL)
11403 return FALSE;
11404
11405 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11406
11407 if (elf_section_data (o)->relocs != internal_relocs)
11408 free (internal_relocs);
11409
11410 if (!r)
11411 return FALSE;
11412 }
11413 }
11414 }
11415
11416 /* Remove the symbols that were in the swept sections from the dynamic
11417 symbol table. GCFIXME: Anyone know how to get them out of the
11418 static symbol table as well? */
ccabcbe5
AM
11419 sweep_info.info = info;
11420 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11421 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11422 &sweep_info);
c152c796 11423
ccabcbe5 11424 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11425 return TRUE;
11426}
11427
11428/* Propagate collected vtable information. This is called through
11429 elf_link_hash_traverse. */
11430
11431static bfd_boolean
11432elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11433{
11434 if (h->root.type == bfd_link_hash_warning)
11435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11436
11437 /* Those that are not vtables. */
f6e332e6 11438 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11439 return TRUE;
11440
11441 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11442 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11443 return TRUE;
11444
11445 /* If we've already been done, exit. */
f6e332e6 11446 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11447 return TRUE;
11448
11449 /* Make sure the parent's table is up to date. */
f6e332e6 11450 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11451
f6e332e6 11452 if (h->vtable->used == NULL)
c152c796
AM
11453 {
11454 /* None of this table's entries were referenced. Re-use the
11455 parent's table. */
f6e332e6
AM
11456 h->vtable->used = h->vtable->parent->vtable->used;
11457 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11458 }
11459 else
11460 {
11461 size_t n;
11462 bfd_boolean *cu, *pu;
11463
11464 /* Or the parent's entries into ours. */
f6e332e6 11465 cu = h->vtable->used;
c152c796 11466 cu[-1] = TRUE;
f6e332e6 11467 pu = h->vtable->parent->vtable->used;
c152c796
AM
11468 if (pu != NULL)
11469 {
11470 const struct elf_backend_data *bed;
11471 unsigned int log_file_align;
11472
11473 bed = get_elf_backend_data (h->root.u.def.section->owner);
11474 log_file_align = bed->s->log_file_align;
f6e332e6 11475 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11476 while (n--)
11477 {
11478 if (*pu)
11479 *cu = TRUE;
11480 pu++;
11481 cu++;
11482 }
11483 }
11484 }
11485
11486 return TRUE;
11487}
11488
11489static bfd_boolean
11490elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11491{
11492 asection *sec;
11493 bfd_vma hstart, hend;
11494 Elf_Internal_Rela *relstart, *relend, *rel;
11495 const struct elf_backend_data *bed;
11496 unsigned int log_file_align;
11497
11498 if (h->root.type == bfd_link_hash_warning)
11499 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11500
11501 /* Take care of both those symbols that do not describe vtables as
11502 well as those that are not loaded. */
f6e332e6 11503 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11504 return TRUE;
11505
11506 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11507 || h->root.type == bfd_link_hash_defweak);
11508
11509 sec = h->root.u.def.section;
11510 hstart = h->root.u.def.value;
11511 hend = hstart + h->size;
11512
11513 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11514 if (!relstart)
11515 return *(bfd_boolean *) okp = FALSE;
11516 bed = get_elf_backend_data (sec->owner);
11517 log_file_align = bed->s->log_file_align;
11518
11519 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11520
11521 for (rel = relstart; rel < relend; ++rel)
11522 if (rel->r_offset >= hstart && rel->r_offset < hend)
11523 {
11524 /* If the entry is in use, do nothing. */
f6e332e6
AM
11525 if (h->vtable->used
11526 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11527 {
11528 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11529 if (h->vtable->used[entry])
c152c796
AM
11530 continue;
11531 }
11532 /* Otherwise, kill it. */
11533 rel->r_offset = rel->r_info = rel->r_addend = 0;
11534 }
11535
11536 return TRUE;
11537}
11538
87538722
AM
11539/* Mark sections containing dynamically referenced symbols. When
11540 building shared libraries, we must assume that any visible symbol is
11541 referenced. */
715df9b8 11542
64d03ab5
AM
11543bfd_boolean
11544bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11545{
87538722
AM
11546 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11547
715df9b8
EB
11548 if (h->root.type == bfd_link_hash_warning)
11549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11550
11551 if ((h->root.type == bfd_link_hash_defined
11552 || h->root.type == bfd_link_hash_defweak)
87538722 11553 && (h->ref_dynamic
5adcfd8b 11554 || (!info->executable
87538722
AM
11555 && h->def_regular
11556 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11557 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11558 h->root.u.def.section->flags |= SEC_KEEP;
11559
11560 return TRUE;
11561}
3b36f7e6 11562
74f0fb50
AM
11563/* Keep all sections containing symbols undefined on the command-line,
11564 and the section containing the entry symbol. */
11565
11566void
11567_bfd_elf_gc_keep (struct bfd_link_info *info)
11568{
11569 struct bfd_sym_chain *sym;
11570
11571 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11572 {
11573 struct elf_link_hash_entry *h;
11574
11575 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11576 FALSE, FALSE, FALSE);
11577
11578 if (h != NULL
11579 && (h->root.type == bfd_link_hash_defined
11580 || h->root.type == bfd_link_hash_defweak)
11581 && !bfd_is_abs_section (h->root.u.def.section))
11582 h->root.u.def.section->flags |= SEC_KEEP;
11583 }
11584}
11585
c152c796
AM
11586/* Do mark and sweep of unused sections. */
11587
11588bfd_boolean
11589bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11590{
11591 bfd_boolean ok = TRUE;
11592 bfd *sub;
6a5bb875 11593 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11594 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11595
64d03ab5 11596 if (!bed->can_gc_sections
715df9b8 11597 || !is_elf_hash_table (info->hash))
c152c796
AM
11598 {
11599 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11600 return TRUE;
11601 }
11602
74f0fb50
AM
11603 bed->gc_keep (info);
11604
9d0a14d3
RS
11605 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11606 at the .eh_frame section if we can mark the FDEs individually. */
11607 _bfd_elf_begin_eh_frame_parsing (info);
11608 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11609 {
11610 asection *sec;
11611 struct elf_reloc_cookie cookie;
11612
11613 sec = bfd_get_section_by_name (sub, ".eh_frame");
11614 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11615 {
11616 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11617 if (elf_section_data (sec)->sec_info)
11618 elf_eh_frame_section (sub) = sec;
11619 fini_reloc_cookie_for_section (&cookie, sec);
11620 }
11621 }
11622 _bfd_elf_end_eh_frame_parsing (info);
11623
c152c796
AM
11624 /* Apply transitive closure to the vtable entry usage info. */
11625 elf_link_hash_traverse (elf_hash_table (info),
11626 elf_gc_propagate_vtable_entries_used,
11627 &ok);
11628 if (!ok)
11629 return FALSE;
11630
11631 /* Kill the vtable relocations that were not used. */
11632 elf_link_hash_traverse (elf_hash_table (info),
11633 elf_gc_smash_unused_vtentry_relocs,
11634 &ok);
11635 if (!ok)
11636 return FALSE;
11637
715df9b8
EB
11638 /* Mark dynamically referenced symbols. */
11639 if (elf_hash_table (info)->dynamic_sections_created)
11640 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 11641 bed->gc_mark_dynamic_ref,
87538722 11642 info);
c152c796 11643
715df9b8 11644 /* Grovel through relocs to find out who stays ... */
64d03ab5 11645 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
11646 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11647 {
11648 asection *o;
11649
11650 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11651 continue;
11652
11653 for (o = sub->sections; o != NULL; o = o->next)
a14a5de3 11654 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
39c2f51b
AM
11655 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11656 return FALSE;
c152c796
AM
11657 }
11658
6a5bb875
PB
11659 /* Allow the backend to mark additional target specific sections. */
11660 if (bed->gc_mark_extra_sections)
74f0fb50 11661 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 11662
c152c796 11663 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 11664 return elf_gc_sweep (abfd, info);
c152c796
AM
11665}
11666\f
11667/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11668
11669bfd_boolean
11670bfd_elf_gc_record_vtinherit (bfd *abfd,
11671 asection *sec,
11672 struct elf_link_hash_entry *h,
11673 bfd_vma offset)
11674{
11675 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11676 struct elf_link_hash_entry **search, *child;
11677 bfd_size_type extsymcount;
11678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11679
11680 /* The sh_info field of the symtab header tells us where the
11681 external symbols start. We don't care about the local symbols at
11682 this point. */
11683 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11684 if (!elf_bad_symtab (abfd))
11685 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11686
11687 sym_hashes = elf_sym_hashes (abfd);
11688 sym_hashes_end = sym_hashes + extsymcount;
11689
11690 /* Hunt down the child symbol, which is in this section at the same
11691 offset as the relocation. */
11692 for (search = sym_hashes; search != sym_hashes_end; ++search)
11693 {
11694 if ((child = *search) != NULL
11695 && (child->root.type == bfd_link_hash_defined
11696 || child->root.type == bfd_link_hash_defweak)
11697 && child->root.u.def.section == sec
11698 && child->root.u.def.value == offset)
11699 goto win;
11700 }
11701
d003868e
AM
11702 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11703 abfd, sec, (unsigned long) offset);
c152c796
AM
11704 bfd_set_error (bfd_error_invalid_operation);
11705 return FALSE;
11706
11707 win:
f6e332e6
AM
11708 if (!child->vtable)
11709 {
11710 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11711 if (!child->vtable)
11712 return FALSE;
11713 }
c152c796
AM
11714 if (!h)
11715 {
11716 /* This *should* only be the absolute section. It could potentially
11717 be that someone has defined a non-global vtable though, which
11718 would be bad. It isn't worth paging in the local symbols to be
11719 sure though; that case should simply be handled by the assembler. */
11720
f6e332e6 11721 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
11722 }
11723 else
f6e332e6 11724 child->vtable->parent = h;
c152c796
AM
11725
11726 return TRUE;
11727}
11728
11729/* Called from check_relocs to record the existence of a VTENTRY reloc. */
11730
11731bfd_boolean
11732bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11733 asection *sec ATTRIBUTE_UNUSED,
11734 struct elf_link_hash_entry *h,
11735 bfd_vma addend)
11736{
11737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11738 unsigned int log_file_align = bed->s->log_file_align;
11739
f6e332e6
AM
11740 if (!h->vtable)
11741 {
11742 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11743 if (!h->vtable)
11744 return FALSE;
11745 }
11746
11747 if (addend >= h->vtable->size)
c152c796
AM
11748 {
11749 size_t size, bytes, file_align;
f6e332e6 11750 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
11751
11752 /* While the symbol is undefined, we have to be prepared to handle
11753 a zero size. */
11754 file_align = 1 << log_file_align;
11755 if (h->root.type == bfd_link_hash_undefined)
11756 size = addend + file_align;
11757 else
11758 {
11759 size = h->size;
11760 if (addend >= size)
11761 {
11762 /* Oops! We've got a reference past the defined end of
11763 the table. This is probably a bug -- shall we warn? */
11764 size = addend + file_align;
11765 }
11766 }
11767 size = (size + file_align - 1) & -file_align;
11768
11769 /* Allocate one extra entry for use as a "done" flag for the
11770 consolidation pass. */
11771 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11772
11773 if (ptr)
11774 {
11775 ptr = bfd_realloc (ptr - 1, bytes);
11776
11777 if (ptr != NULL)
11778 {
11779 size_t oldbytes;
11780
f6e332e6 11781 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
11782 * sizeof (bfd_boolean));
11783 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11784 }
11785 }
11786 else
11787 ptr = bfd_zmalloc (bytes);
11788
11789 if (ptr == NULL)
11790 return FALSE;
11791
11792 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
11793 h->vtable->used = ptr + 1;
11794 h->vtable->size = size;
c152c796
AM
11795 }
11796
f6e332e6 11797 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
11798
11799 return TRUE;
11800}
11801
11802struct alloc_got_off_arg {
11803 bfd_vma gotoff;
10455f89 11804 struct bfd_link_info *info;
c152c796
AM
11805};
11806
11807/* We need a special top-level link routine to convert got reference counts
11808 to real got offsets. */
11809
11810static bfd_boolean
11811elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11812{
11813 struct alloc_got_off_arg *gofarg = arg;
10455f89
HPN
11814 bfd *obfd = gofarg->info->output_bfd;
11815 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796
AM
11816
11817 if (h->root.type == bfd_link_hash_warning)
11818 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11819
11820 if (h->got.refcount > 0)
11821 {
11822 h->got.offset = gofarg->gotoff;
10455f89 11823 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
11824 }
11825 else
11826 h->got.offset = (bfd_vma) -1;
11827
11828 return TRUE;
11829}
11830
11831/* And an accompanying bit to work out final got entry offsets once
11832 we're done. Should be called from final_link. */
11833
11834bfd_boolean
11835bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11836 struct bfd_link_info *info)
11837{
11838 bfd *i;
11839 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11840 bfd_vma gotoff;
c152c796
AM
11841 struct alloc_got_off_arg gofarg;
11842
10455f89
HPN
11843 BFD_ASSERT (abfd == info->output_bfd);
11844
c152c796
AM
11845 if (! is_elf_hash_table (info->hash))
11846 return FALSE;
11847
11848 /* The GOT offset is relative to the .got section, but the GOT header is
11849 put into the .got.plt section, if the backend uses it. */
11850 if (bed->want_got_plt)
11851 gotoff = 0;
11852 else
11853 gotoff = bed->got_header_size;
11854
11855 /* Do the local .got entries first. */
11856 for (i = info->input_bfds; i; i = i->link_next)
11857 {
11858 bfd_signed_vma *local_got;
11859 bfd_size_type j, locsymcount;
11860 Elf_Internal_Shdr *symtab_hdr;
11861
11862 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11863 continue;
11864
11865 local_got = elf_local_got_refcounts (i);
11866 if (!local_got)
11867 continue;
11868
11869 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11870 if (elf_bad_symtab (i))
11871 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11872 else
11873 locsymcount = symtab_hdr->sh_info;
11874
11875 for (j = 0; j < locsymcount; ++j)
11876 {
11877 if (local_got[j] > 0)
11878 {
11879 local_got[j] = gotoff;
10455f89 11880 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
11881 }
11882 else
11883 local_got[j] = (bfd_vma) -1;
11884 }
11885 }
11886
11887 /* Then the global .got entries. .plt refcounts are handled by
11888 adjust_dynamic_symbol */
11889 gofarg.gotoff = gotoff;
10455f89 11890 gofarg.info = info;
c152c796
AM
11891 elf_link_hash_traverse (elf_hash_table (info),
11892 elf_gc_allocate_got_offsets,
11893 &gofarg);
11894 return TRUE;
11895}
11896
11897/* Many folk need no more in the way of final link than this, once
11898 got entry reference counting is enabled. */
11899
11900bfd_boolean
11901bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11902{
11903 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11904 return FALSE;
11905
11906 /* Invoke the regular ELF backend linker to do all the work. */
11907 return bfd_elf_final_link (abfd, info);
11908}
11909
11910bfd_boolean
11911bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11912{
11913 struct elf_reloc_cookie *rcookie = cookie;
11914
11915 if (rcookie->bad_symtab)
11916 rcookie->rel = rcookie->rels;
11917
11918 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11919 {
11920 unsigned long r_symndx;
11921
11922 if (! rcookie->bad_symtab)
11923 if (rcookie->rel->r_offset > offset)
11924 return FALSE;
11925 if (rcookie->rel->r_offset != offset)
11926 continue;
11927
11928 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11929 if (r_symndx == SHN_UNDEF)
11930 return TRUE;
11931
11932 if (r_symndx >= rcookie->locsymcount
11933 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11934 {
11935 struct elf_link_hash_entry *h;
11936
11937 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11938
11939 while (h->root.type == bfd_link_hash_indirect
11940 || h->root.type == bfd_link_hash_warning)
11941 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11942
11943 if ((h->root.type == bfd_link_hash_defined
11944 || h->root.type == bfd_link_hash_defweak)
11945 && elf_discarded_section (h->root.u.def.section))
11946 return TRUE;
11947 else
11948 return FALSE;
11949 }
11950 else
11951 {
11952 /* It's not a relocation against a global symbol,
11953 but it could be a relocation against a local
11954 symbol for a discarded section. */
11955 asection *isec;
11956 Elf_Internal_Sym *isym;
11957
11958 /* Need to: get the symbol; get the section. */
11959 isym = &rcookie->locsyms[r_symndx];
cb33740c
AM
11960 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11961 if (isec != NULL && elf_discarded_section (isec))
11962 return TRUE;
c152c796
AM
11963 }
11964 return FALSE;
11965 }
11966 return FALSE;
11967}
11968
11969/* Discard unneeded references to discarded sections.
11970 Returns TRUE if any section's size was changed. */
11971/* This function assumes that the relocations are in sorted order,
11972 which is true for all known assemblers. */
11973
11974bfd_boolean
11975bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11976{
11977 struct elf_reloc_cookie cookie;
11978 asection *stab, *eh;
c152c796
AM
11979 const struct elf_backend_data *bed;
11980 bfd *abfd;
c152c796
AM
11981 bfd_boolean ret = FALSE;
11982
11983 if (info->traditional_format
11984 || !is_elf_hash_table (info->hash))
11985 return FALSE;
11986
ca92cecb 11987 _bfd_elf_begin_eh_frame_parsing (info);
c152c796
AM
11988 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11989 {
11990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11991 continue;
11992
11993 bed = get_elf_backend_data (abfd);
11994
11995 if ((abfd->flags & DYNAMIC) != 0)
11996 continue;
11997
8da3dbc5
AM
11998 eh = NULL;
11999 if (!info->relocatable)
12000 {
12001 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12002 if (eh != NULL
eea6121a 12003 && (eh->size == 0
8da3dbc5
AM
12004 || bfd_is_abs_section (eh->output_section)))
12005 eh = NULL;
12006 }
c152c796
AM
12007
12008 stab = bfd_get_section_by_name (abfd, ".stab");
12009 if (stab != NULL
eea6121a 12010 && (stab->size == 0
c152c796
AM
12011 || bfd_is_abs_section (stab->output_section)
12012 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12013 stab = NULL;
12014
12015 if (stab == NULL
12016 && eh == NULL
12017 && bed->elf_backend_discard_info == NULL)
12018 continue;
12019
5241d853
RS
12020 if (!init_reloc_cookie (&cookie, info, abfd))
12021 return FALSE;
c152c796 12022
5241d853
RS
12023 if (stab != NULL
12024 && stab->reloc_count > 0
12025 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
c152c796 12026 {
5241d853
RS
12027 if (_bfd_discard_section_stabs (abfd, stab,
12028 elf_section_data (stab)->sec_info,
12029 bfd_elf_reloc_symbol_deleted_p,
12030 &cookie))
12031 ret = TRUE;
12032 fini_reloc_cookie_rels (&cookie, stab);
c152c796
AM
12033 }
12034
5241d853
RS
12035 if (eh != NULL
12036 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
c152c796 12037 {
ca92cecb 12038 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
c152c796
AM
12039 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12040 bfd_elf_reloc_symbol_deleted_p,
12041 &cookie))
12042 ret = TRUE;
5241d853 12043 fini_reloc_cookie_rels (&cookie, eh);
c152c796
AM
12044 }
12045
12046 if (bed->elf_backend_discard_info != NULL
12047 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12048 ret = TRUE;
12049
5241d853 12050 fini_reloc_cookie (&cookie, abfd);
c152c796 12051 }
ca92cecb 12052 _bfd_elf_end_eh_frame_parsing (info);
c152c796
AM
12053
12054 if (info->eh_frame_hdr
12055 && !info->relocatable
12056 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12057 ret = TRUE;
12058
12059 return ret;
12060}
082b7297 12061
9659de1c
AM
12062/* For a SHT_GROUP section, return the group signature. For other
12063 sections, return the normal section name. */
12064
12065static const char *
12066section_signature (asection *sec)
12067{
12068 if ((sec->flags & SEC_GROUP) != 0
12069 && elf_next_in_group (sec) != NULL
12070 && elf_group_name (elf_next_in_group (sec)) != NULL)
12071 return elf_group_name (elf_next_in_group (sec));
12072 return sec->name;
12073}
12074
082b7297 12075void
9659de1c 12076_bfd_elf_section_already_linked (bfd *abfd, asection *sec,
c0f00686 12077 struct bfd_link_info *info)
082b7297
L
12078{
12079 flagword flags;
6d2cd210 12080 const char *name, *p;
082b7297
L
12081 struct bfd_section_already_linked *l;
12082 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 12083
3d7f7666
L
12084 if (sec->output_section == bfd_abs_section_ptr)
12085 return;
082b7297
L
12086
12087 flags = sec->flags;
3d7f7666 12088
c2370991
AM
12089 /* Return if it isn't a linkonce section. A comdat group section
12090 also has SEC_LINK_ONCE set. */
12091 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12092 return;
12093
c2370991
AM
12094 /* Don't put group member sections on our list of already linked
12095 sections. They are handled as a group via their group section. */
12096 if (elf_sec_group (sec) != NULL)
12097 return;
3d7f7666 12098
082b7297
L
12099 /* FIXME: When doing a relocatable link, we may have trouble
12100 copying relocations in other sections that refer to local symbols
12101 in the section being discarded. Those relocations will have to
12102 be converted somehow; as of this writing I'm not sure that any of
12103 the backends handle that correctly.
12104
12105 It is tempting to instead not discard link once sections when
12106 doing a relocatable link (technically, they should be discarded
12107 whenever we are building constructors). However, that fails,
12108 because the linker winds up combining all the link once sections
12109 into a single large link once section, which defeats the purpose
12110 of having link once sections in the first place.
12111
12112 Also, not merging link once sections in a relocatable link
12113 causes trouble for MIPS ELF, which relies on link once semantics
12114 to handle the .reginfo section correctly. */
12115
9659de1c 12116 name = section_signature (sec);
082b7297 12117
0112cd26 12118 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12119 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12120 p++;
12121 else
12122 p = name;
12123
12124 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12125
12126 for (l = already_linked_list->entry; l != NULL; l = l->next)
12127 {
c2370991
AM
12128 /* We may have 2 different types of sections on the list: group
12129 sections and linkonce sections. Match like sections. */
3d7f7666 12130 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9659de1c 12131 && strcmp (name, section_signature (l->sec)) == 0
082b7297
L
12132 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12133 {
12134 /* The section has already been linked. See if we should
6d2cd210 12135 issue a warning. */
082b7297
L
12136 switch (flags & SEC_LINK_DUPLICATES)
12137 {
12138 default:
12139 abort ();
12140
12141 case SEC_LINK_DUPLICATES_DISCARD:
12142 break;
12143
12144 case SEC_LINK_DUPLICATES_ONE_ONLY:
12145 (*_bfd_error_handler)
c93625e2 12146 (_("%B: ignoring duplicate section `%A'"),
d003868e 12147 abfd, sec);
082b7297
L
12148 break;
12149
12150 case SEC_LINK_DUPLICATES_SAME_SIZE:
12151 if (sec->size != l->sec->size)
12152 (*_bfd_error_handler)
c93625e2 12153 (_("%B: duplicate section `%A' has different size"),
d003868e 12154 abfd, sec);
082b7297 12155 break;
ea5158d8
DJ
12156
12157 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12158 if (sec->size != l->sec->size)
12159 (*_bfd_error_handler)
c93625e2 12160 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12161 abfd, sec);
12162 else if (sec->size != 0)
12163 {
12164 bfd_byte *sec_contents, *l_sec_contents;
12165
12166 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12167 (*_bfd_error_handler)
c93625e2 12168 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12169 abfd, sec);
12170 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12171 &l_sec_contents))
12172 (*_bfd_error_handler)
c93625e2 12173 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12174 l->sec->owner, l->sec);
12175 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12176 (*_bfd_error_handler)
c93625e2 12177 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12178 abfd, sec);
12179
12180 if (sec_contents)
12181 free (sec_contents);
12182 if (l_sec_contents)
12183 free (l_sec_contents);
12184 }
12185 break;
082b7297
L
12186 }
12187
12188 /* Set the output_section field so that lang_add_section
12189 does not create a lang_input_section structure for this
12190 section. Since there might be a symbol in the section
12191 being discarded, we must retain a pointer to the section
12192 which we are really going to use. */
12193 sec->output_section = bfd_abs_section_ptr;
12194 sec->kept_section = l->sec;
3b36f7e6 12195
082b7297 12196 if (flags & SEC_GROUP)
3d7f7666
L
12197 {
12198 asection *first = elf_next_in_group (sec);
12199 asection *s = first;
12200
12201 while (s != NULL)
12202 {
12203 s->output_section = bfd_abs_section_ptr;
12204 /* Record which group discards it. */
12205 s->kept_section = l->sec;
12206 s = elf_next_in_group (s);
12207 /* These lists are circular. */
12208 if (s == first)
12209 break;
12210 }
12211 }
082b7297
L
12212
12213 return;
12214 }
12215 }
12216
c2370991
AM
12217 /* A single member comdat group section may be discarded by a
12218 linkonce section and vice versa. */
12219
12220 if ((flags & SEC_GROUP) != 0)
3d7f7666 12221 {
c2370991
AM
12222 asection *first = elf_next_in_group (sec);
12223
12224 if (first != NULL && elf_next_in_group (first) == first)
12225 /* Check this single member group against linkonce sections. */
12226 for (l = already_linked_list->entry; l != NULL; l = l->next)
12227 if ((l->sec->flags & SEC_GROUP) == 0
12228 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12229 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12230 {
12231 first->output_section = bfd_abs_section_ptr;
12232 first->kept_section = l->sec;
12233 sec->output_section = bfd_abs_section_ptr;
12234 break;
12235 }
3d7f7666
L
12236 }
12237 else
c2370991 12238 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12239 for (l = already_linked_list->entry; l != NULL; l = l->next)
12240 if (l->sec->flags & SEC_GROUP)
12241 {
12242 asection *first = elf_next_in_group (l->sec);
12243
12244 if (first != NULL
12245 && elf_next_in_group (first) == first
c0f00686 12246 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12247 {
12248 sec->output_section = bfd_abs_section_ptr;
c2370991 12249 sec->kept_section = first;
6d2cd210
JJ
12250 break;
12251 }
12252 }
12253
082b7297 12254 /* This is the first section with this name. Record it. */
a6626e8c
MS
12255 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12256 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
082b7297 12257}
81e1b023 12258
a4d8e49b
L
12259bfd_boolean
12260_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12261{
12262 return sym->st_shndx == SHN_COMMON;
12263}
12264
12265unsigned int
12266_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12267{
12268 return SHN_COMMON;
12269}
12270
12271asection *
12272_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12273{
12274 return bfd_com_section_ptr;
12275}
10455f89
HPN
12276
12277bfd_vma
12278_bfd_elf_default_got_elt_size (bfd *abfd,
12279 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12280 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12281 bfd *ibfd ATTRIBUTE_UNUSED,
12282 unsigned long symndx ATTRIBUTE_UNUSED)
12283{
12284 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12285 return bed->s->arch_size / 8;
12286}
83bac4b0
NC
12287
12288/* Routines to support the creation of dynamic relocs. */
12289
12290/* Return true if NAME is a name of a relocation
12291 section associated with section S. */
12292
12293static bfd_boolean
12294is_reloc_section (bfd_boolean rela, const char * name, asection * s)
12295{
12296 if (rela)
12297 return CONST_STRNEQ (name, ".rela")
12298 && strcmp (bfd_get_section_name (NULL, s), name + 5) == 0;
12299
12300 return CONST_STRNEQ (name, ".rel")
12301 && strcmp (bfd_get_section_name (NULL, s), name + 4) == 0;
12302}
12303
12304/* Returns the name of the dynamic reloc section associated with SEC. */
12305
12306static const char *
12307get_dynamic_reloc_section_name (bfd * abfd,
12308 asection * sec,
12309 bfd_boolean is_rela)
12310{
12311 const char * name;
12312 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
12313 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
12314
12315 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
12316 if (name == NULL)
12317 return NULL;
12318
12319 if (! is_reloc_section (is_rela, name, sec))
12320 {
12321 static bfd_boolean complained = FALSE;
12322
12323 if (! complained)
12324 {
12325 (*_bfd_error_handler)
12326 (_("%B: bad relocation section name `%s\'"), abfd, name);
12327 complained = TRUE;
12328 }
12329 name = NULL;
12330 }
12331
12332 return name;
12333}
12334
12335/* Returns the dynamic reloc section associated with SEC.
12336 If necessary compute the name of the dynamic reloc section based
12337 on SEC's name (looked up in ABFD's string table) and the setting
12338 of IS_RELA. */
12339
12340asection *
12341_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12342 asection * sec,
12343 bfd_boolean is_rela)
12344{
12345 asection * reloc_sec = elf_section_data (sec)->sreloc;
12346
12347 if (reloc_sec == NULL)
12348 {
12349 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12350
12351 if (name != NULL)
12352 {
12353 reloc_sec = bfd_get_section_by_name (abfd, name);
12354
12355 if (reloc_sec != NULL)
12356 elf_section_data (sec)->sreloc = reloc_sec;
12357 }
12358 }
12359
12360 return reloc_sec;
12361}
12362
12363/* Returns the dynamic reloc section associated with SEC. If the
12364 section does not exist it is created and attached to the DYNOBJ
12365 bfd and stored in the SRELOC field of SEC's elf_section_data
12366 structure.
12367
12368 ALIGNMENT is the alignment for the newly created section and
12369 IS_RELA defines whether the name should be .rela.<SEC's name>
12370 or .rel.<SEC's name>. The section name is looked up in the
12371 string table associated with ABFD. */
12372
12373asection *
12374_bfd_elf_make_dynamic_reloc_section (asection * sec,
12375 bfd * dynobj,
12376 unsigned int alignment,
12377 bfd * abfd,
12378 bfd_boolean is_rela)
12379{
12380 asection * reloc_sec = elf_section_data (sec)->sreloc;
12381
12382 if (reloc_sec == NULL)
12383 {
12384 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12385
12386 if (name == NULL)
12387 return NULL;
12388
12389 reloc_sec = bfd_get_section_by_name (dynobj, name);
12390
12391 if (reloc_sec == NULL)
12392 {
12393 flagword flags;
12394
12395 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12396 if ((sec->flags & SEC_ALLOC) != 0)
12397 flags |= SEC_ALLOC | SEC_LOAD;
12398
12399 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12400 if (reloc_sec != NULL)
12401 {
12402 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12403 reloc_sec = NULL;
12404 }
12405 }
12406
12407 elf_section_data (sec)->sreloc = reloc_sec;
12408 }
12409
12410 return reloc_sec;
12411}
This page took 1.59305 seconds and 4 git commands to generate.