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