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