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