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