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