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