* config/tc-arm.c (do_t_ldmstm): Change BFD_RELOC_NONE to
[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;
4ea42fb7 443 struct elf_link_hash_table *htab;
45d6a902 444
0eddce27 445 if (!is_elf_hash_table (info->hash))
45d6a902
AM
446 return TRUE;
447
4ea42fb7
AM
448 htab = elf_hash_table (info);
449 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 450 if (h == NULL)
4ea42fb7 451 return provide;
45d6a902 452
02bb6eae
AO
453 /* Since we're defining the symbol, don't let it seem to have not
454 been defined. record_dynamic_symbol and size_dynamic_sections
77cfaee6 455 may depend on this. */
02bb6eae
AO
456 if (h->root.type == bfd_link_hash_undefweak
457 || h->root.type == bfd_link_hash_undefined)
77cfaee6 458 {
4ea42fb7 459 h->root.type = bfd_link_hash_new;
77cfaee6
AM
460 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
461 bfd_link_repair_undef_list (&htab->root);
77cfaee6 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:
11f25ea6
AM
2845 if (h->root.u.undef.abfd != inf->not_needed)
2846 return TRUE;
4ea42fb7
AM
2847 if (h->root.u.undef.weak != NULL
2848 && h->root.u.undef.weak != inf->not_needed)
11f25ea6
AM
2849 {
2850 /* Symbol was undefweak in u.undef.weak bfd, and has become
2851 undefined in as-needed lib. Restore weak. */
2852 h->root.type = bfd_link_hash_undefweak;
2853 h->root.u.undef.abfd = h->root.u.undef.weak;
2854 if (h->root.u.undef.next != NULL
2855 || inf->htab->root.undefs_tail == &h->root)
2856 inf->twiddled = TRUE;
2857 return TRUE;
2858 }
2859 break;
2860
77cfaee6
AM
2861 case bfd_link_hash_undefweak:
2862 if (h->root.u.undef.abfd != inf->not_needed)
2863 return TRUE;
2864 break;
2865
2866 case bfd_link_hash_defined:
2867 case bfd_link_hash_defweak:
2868 if (h->root.u.def.section->owner != inf->not_needed)
2869 return TRUE;
2870 break;
2871
2872 case bfd_link_hash_common:
2873 if (h->root.u.c.p->section->owner != inf->not_needed)
2874 return TRUE;
2875 break;
2876
2877 case bfd_link_hash_warning:
2878 case bfd_link_hash_indirect:
2879 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2880 if (h->root.u.i.link->type != bfd_link_hash_new)
2881 return TRUE;
2882 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2883 return TRUE;
2884 break;
2885 }
2886
11f25ea6
AM
2887 /* There is no way we can undo symbol table state from defined or
2888 defweak back to undefined. */
2889 if (h->ref_regular)
2890 abort ();
2891
77cfaee6
AM
2892 /* Set sym back to newly created state, but keep undefs list pointer. */
2893 bh = h->root.u.undef.next;
2894 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2895 inf->twiddled = TRUE;
2896 (*inf->htab->root.table.newfunc) (&h->root.root,
2897 &inf->htab->root.table,
2898 h->root.root.string);
2899 h->root.u.undef.next = bh;
2900 h->root.u.undef.abfd = inf->not_needed;
2901 h->non_elf = 0;
2902 return TRUE;
2903}
2904
5a580b3a 2905/* Sort symbol by value and section. */
4ad4eba5
AM
2906static int
2907elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
2908{
2909 const struct elf_link_hash_entry *h1;
2910 const struct elf_link_hash_entry *h2;
10b7e05b 2911 bfd_signed_vma vdiff;
5a580b3a
AM
2912
2913 h1 = *(const struct elf_link_hash_entry **) arg1;
2914 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
2915 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2916 if (vdiff != 0)
2917 return vdiff > 0 ? 1 : -1;
2918 else
2919 {
2920 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2921 if (sdiff != 0)
2922 return sdiff > 0 ? 1 : -1;
2923 }
5a580b3a
AM
2924 return 0;
2925}
4ad4eba5 2926
5a580b3a
AM
2927/* This function is used to adjust offsets into .dynstr for
2928 dynamic symbols. This is called via elf_link_hash_traverse. */
2929
2930static bfd_boolean
2931elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2932{
2933 struct elf_strtab_hash *dynstr = data;
2934
2935 if (h->root.type == bfd_link_hash_warning)
2936 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2937
2938 if (h->dynindx != -1)
2939 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2940 return TRUE;
2941}
2942
2943/* Assign string offsets in .dynstr, update all structures referencing
2944 them. */
2945
4ad4eba5
AM
2946static bfd_boolean
2947elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
2948{
2949 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2950 struct elf_link_local_dynamic_entry *entry;
2951 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2952 bfd *dynobj = hash_table->dynobj;
2953 asection *sdyn;
2954 bfd_size_type size;
2955 const struct elf_backend_data *bed;
2956 bfd_byte *extdyn;
2957
2958 _bfd_elf_strtab_finalize (dynstr);
2959 size = _bfd_elf_strtab_size (dynstr);
2960
2961 bed = get_elf_backend_data (dynobj);
2962 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2963 BFD_ASSERT (sdyn != NULL);
2964
2965 /* Update all .dynamic entries referencing .dynstr strings. */
2966 for (extdyn = sdyn->contents;
eea6121a 2967 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
2968 extdyn += bed->s->sizeof_dyn)
2969 {
2970 Elf_Internal_Dyn dyn;
2971
2972 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2973 switch (dyn.d_tag)
2974 {
2975 case DT_STRSZ:
2976 dyn.d_un.d_val = size;
2977 break;
2978 case DT_NEEDED:
2979 case DT_SONAME:
2980 case DT_RPATH:
2981 case DT_RUNPATH:
2982 case DT_FILTER:
2983 case DT_AUXILIARY:
2984 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2985 break;
2986 default:
2987 continue;
2988 }
2989 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2990 }
2991
2992 /* Now update local dynamic symbols. */
2993 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2994 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2995 entry->isym.st_name);
2996
2997 /* And the rest of dynamic symbols. */
2998 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2999
3000 /* Adjust version definitions. */
3001 if (elf_tdata (output_bfd)->cverdefs)
3002 {
3003 asection *s;
3004 bfd_byte *p;
3005 bfd_size_type i;
3006 Elf_Internal_Verdef def;
3007 Elf_Internal_Verdaux defaux;
3008
3009 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3010 p = s->contents;
3011 do
3012 {
3013 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3014 &def);
3015 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3016 if (def.vd_aux != sizeof (Elf_External_Verdef))
3017 continue;
5a580b3a
AM
3018 for (i = 0; i < def.vd_cnt; ++i)
3019 {
3020 _bfd_elf_swap_verdaux_in (output_bfd,
3021 (Elf_External_Verdaux *) p, &defaux);
3022 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3023 defaux.vda_name);
3024 _bfd_elf_swap_verdaux_out (output_bfd,
3025 &defaux, (Elf_External_Verdaux *) p);
3026 p += sizeof (Elf_External_Verdaux);
3027 }
3028 }
3029 while (def.vd_next);
3030 }
3031
3032 /* Adjust version references. */
3033 if (elf_tdata (output_bfd)->verref)
3034 {
3035 asection *s;
3036 bfd_byte *p;
3037 bfd_size_type i;
3038 Elf_Internal_Verneed need;
3039 Elf_Internal_Vernaux needaux;
3040
3041 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3042 p = s->contents;
3043 do
3044 {
3045 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3046 &need);
3047 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3048 _bfd_elf_swap_verneed_out (output_bfd, &need,
3049 (Elf_External_Verneed *) p);
3050 p += sizeof (Elf_External_Verneed);
3051 for (i = 0; i < need.vn_cnt; ++i)
3052 {
3053 _bfd_elf_swap_vernaux_in (output_bfd,
3054 (Elf_External_Vernaux *) p, &needaux);
3055 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3056 needaux.vna_name);
3057 _bfd_elf_swap_vernaux_out (output_bfd,
3058 &needaux,
3059 (Elf_External_Vernaux *) p);
3060 p += sizeof (Elf_External_Vernaux);
3061 }
3062 }
3063 while (need.vn_next);
3064 }
3065
3066 return TRUE;
3067}
3068\f
4ad4eba5
AM
3069/* Add symbols from an ELF object file to the linker hash table. */
3070
3071static bfd_boolean
3072elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3073{
3074 bfd_boolean (*add_symbol_hook)
555cd476 3075 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
4ad4eba5
AM
3076 const char **, flagword *, asection **, bfd_vma *);
3077 bfd_boolean (*check_relocs)
3078 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
85fbca6a
NC
3079 bfd_boolean (*check_directives)
3080 (bfd *, struct bfd_link_info *);
4ad4eba5
AM
3081 bfd_boolean collect;
3082 Elf_Internal_Shdr *hdr;
3083 bfd_size_type symcount;
3084 bfd_size_type extsymcount;
3085 bfd_size_type extsymoff;
3086 struct elf_link_hash_entry **sym_hash;
3087 bfd_boolean dynamic;
3088 Elf_External_Versym *extversym = NULL;
3089 Elf_External_Versym *ever;
3090 struct elf_link_hash_entry *weaks;
3091 struct elf_link_hash_entry **nondeflt_vers = NULL;
3092 bfd_size_type nondeflt_vers_cnt = 0;
3093 Elf_Internal_Sym *isymbuf = NULL;
3094 Elf_Internal_Sym *isym;
3095 Elf_Internal_Sym *isymend;
3096 const struct elf_backend_data *bed;
3097 bfd_boolean add_needed;
3098 struct elf_link_hash_table * hash_table;
3099 bfd_size_type amt;
3100
3101 hash_table = elf_hash_table (info);
3102
3103 bed = get_elf_backend_data (abfd);
3104 add_symbol_hook = bed->elf_add_symbol_hook;
3105 collect = bed->collect;
3106
3107 if ((abfd->flags & DYNAMIC) == 0)
3108 dynamic = FALSE;
3109 else
3110 {
3111 dynamic = TRUE;
3112
3113 /* You can't use -r against a dynamic object. Also, there's no
3114 hope of using a dynamic object which does not exactly match
3115 the format of the output file. */
3116 if (info->relocatable
3117 || !is_elf_hash_table (hash_table)
3118 || hash_table->root.creator != abfd->xvec)
3119 {
9a0789ec
NC
3120 if (info->relocatable)
3121 bfd_set_error (bfd_error_invalid_operation);
3122 else
3123 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3124 goto error_return;
3125 }
3126 }
3127
3128 /* As a GNU extension, any input sections which are named
3129 .gnu.warning.SYMBOL are treated as warning symbols for the given
3130 symbol. This differs from .gnu.warning sections, which generate
3131 warnings when they are included in an output file. */
3132 if (info->executable)
3133 {
3134 asection *s;
3135
3136 for (s = abfd->sections; s != NULL; s = s->next)
3137 {
3138 const char *name;
3139
3140 name = bfd_get_section_name (abfd, s);
3141 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3142 {
3143 char *msg;
3144 bfd_size_type sz;
4ad4eba5
AM
3145
3146 name += sizeof ".gnu.warning." - 1;
3147
3148 /* If this is a shared object, then look up the symbol
3149 in the hash table. If it is there, and it is already
3150 been defined, then we will not be using the entry
3151 from this shared object, so we don't need to warn.
3152 FIXME: If we see the definition in a regular object
3153 later on, we will warn, but we shouldn't. The only
3154 fix is to keep track of what warnings we are supposed
3155 to emit, and then handle them all at the end of the
3156 link. */
3157 if (dynamic)
3158 {
3159 struct elf_link_hash_entry *h;
3160
3161 h = elf_link_hash_lookup (hash_table, name,
3162 FALSE, FALSE, TRUE);
3163
3164 /* FIXME: What about bfd_link_hash_common? */
3165 if (h != NULL
3166 && (h->root.type == bfd_link_hash_defined
3167 || h->root.type == bfd_link_hash_defweak))
3168 {
3169 /* We don't want to issue this warning. Clobber
3170 the section size so that the warning does not
3171 get copied into the output file. */
eea6121a 3172 s->size = 0;
4ad4eba5
AM
3173 continue;
3174 }
3175 }
3176
eea6121a 3177 sz = s->size;
370a0e1b 3178 msg = bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3179 if (msg == NULL)
3180 goto error_return;
3181
370a0e1b 3182 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3183 goto error_return;
3184
370a0e1b 3185 msg[sz] = '\0';
4ad4eba5
AM
3186
3187 if (! (_bfd_generic_link_add_one_symbol
3188 (info, abfd, name, BSF_WARNING, s, 0, msg,
3189 FALSE, collect, NULL)))
3190 goto error_return;
3191
3192 if (! info->relocatable)
3193 {
3194 /* Clobber the section size so that the warning does
3195 not get copied into the output file. */
eea6121a 3196 s->size = 0;
4ad4eba5
AM
3197 }
3198 }
3199 }
3200 }
3201
3202 add_needed = TRUE;
3203 if (! dynamic)
3204 {
3205 /* If we are creating a shared library, create all the dynamic
3206 sections immediately. We need to attach them to something,
3207 so we attach them to this BFD, provided it is the right
3208 format. FIXME: If there are no input BFD's of the same
3209 format as the output, we can't make a shared library. */
3210 if (info->shared
3211 && is_elf_hash_table (hash_table)
3212 && hash_table->root.creator == abfd->xvec
3213 && ! hash_table->dynamic_sections_created)
3214 {
3215 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3216 goto error_return;
3217 }
3218 }
3219 else if (!is_elf_hash_table (hash_table))
3220 goto error_return;
3221 else
3222 {
3223 asection *s;
3224 const char *soname = NULL;
3225 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3226 int ret;
3227
3228 /* ld --just-symbols and dynamic objects don't mix very well.
3229 Test for --just-symbols by looking at info set up by
3230 _bfd_elf_link_just_syms. */
3231 if ((s = abfd->sections) != NULL
3232 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3233 goto error_return;
3234
3235 /* If this dynamic lib was specified on the command line with
3236 --as-needed in effect, then we don't want to add a DT_NEEDED
3237 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3238 in by another lib's DT_NEEDED. When --no-add-needed is used
3239 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3240 any dynamic library in DT_NEEDED tags in the dynamic lib at
3241 all. */
3242 add_needed = (elf_dyn_lib_class (abfd)
3243 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3244 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3245
3246 s = bfd_get_section_by_name (abfd, ".dynamic");
3247 if (s != NULL)
3248 {
3249 bfd_byte *dynbuf;
3250 bfd_byte *extdyn;
3251 int elfsec;
3252 unsigned long shlink;
3253
eea6121a 3254 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4ad4eba5
AM
3255 goto error_free_dyn;
3256
3257 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3258 if (elfsec == -1)
3259 goto error_free_dyn;
3260 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3261
3262 for (extdyn = dynbuf;
eea6121a 3263 extdyn < dynbuf + s->size;
4ad4eba5
AM
3264 extdyn += bed->s->sizeof_dyn)
3265 {
3266 Elf_Internal_Dyn dyn;
3267
3268 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3269 if (dyn.d_tag == DT_SONAME)
3270 {
3271 unsigned int tagv = dyn.d_un.d_val;
3272 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3273 if (soname == NULL)
3274 goto error_free_dyn;
3275 }
3276 if (dyn.d_tag == DT_NEEDED)
3277 {
3278 struct bfd_link_needed_list *n, **pn;
3279 char *fnm, *anm;
3280 unsigned int tagv = dyn.d_un.d_val;
3281
3282 amt = sizeof (struct bfd_link_needed_list);
3283 n = bfd_alloc (abfd, amt);
3284 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3285 if (n == NULL || fnm == NULL)
3286 goto error_free_dyn;
3287 amt = strlen (fnm) + 1;
3288 anm = bfd_alloc (abfd, amt);
3289 if (anm == NULL)
3290 goto error_free_dyn;
3291 memcpy (anm, fnm, amt);
3292 n->name = anm;
3293 n->by = abfd;
3294 n->next = NULL;
3295 for (pn = & hash_table->needed;
3296 *pn != NULL;
3297 pn = &(*pn)->next)
3298 ;
3299 *pn = n;
3300 }
3301 if (dyn.d_tag == DT_RUNPATH)
3302 {
3303 struct bfd_link_needed_list *n, **pn;
3304 char *fnm, *anm;
3305 unsigned int tagv = dyn.d_un.d_val;
3306
3307 amt = sizeof (struct bfd_link_needed_list);
3308 n = bfd_alloc (abfd, amt);
3309 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3310 if (n == NULL || fnm == NULL)
3311 goto error_free_dyn;
3312 amt = strlen (fnm) + 1;
3313 anm = bfd_alloc (abfd, amt);
3314 if (anm == NULL)
3315 goto error_free_dyn;
3316 memcpy (anm, fnm, amt);
3317 n->name = anm;
3318 n->by = abfd;
3319 n->next = NULL;
3320 for (pn = & runpath;
3321 *pn != NULL;
3322 pn = &(*pn)->next)
3323 ;
3324 *pn = n;
3325 }
3326 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3327 if (!runpath && dyn.d_tag == DT_RPATH)
3328 {
3329 struct bfd_link_needed_list *n, **pn;
3330 char *fnm, *anm;
3331 unsigned int tagv = dyn.d_un.d_val;
3332
3333 amt = sizeof (struct bfd_link_needed_list);
3334 n = bfd_alloc (abfd, amt);
3335 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3336 if (n == NULL || fnm == NULL)
3337 goto error_free_dyn;
3338 amt = strlen (fnm) + 1;
3339 anm = bfd_alloc (abfd, amt);
3340 if (anm == NULL)
3341 {
3342 error_free_dyn:
3343 free (dynbuf);
3344 goto error_return;
3345 }
3346 memcpy (anm, fnm, amt);
3347 n->name = anm;
3348 n->by = abfd;
3349 n->next = NULL;
3350 for (pn = & rpath;
3351 *pn != NULL;
3352 pn = &(*pn)->next)
3353 ;
3354 *pn = n;
3355 }
3356 }
3357
3358 free (dynbuf);
3359 }
3360
3361 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3362 frees all more recently bfd_alloc'd blocks as well. */
3363 if (runpath)
3364 rpath = runpath;
3365
3366 if (rpath)
3367 {
3368 struct bfd_link_needed_list **pn;
3369 for (pn = & hash_table->runpath;
3370 *pn != NULL;
3371 pn = &(*pn)->next)
3372 ;
3373 *pn = rpath;
3374 }
3375
3376 /* We do not want to include any of the sections in a dynamic
3377 object in the output file. We hack by simply clobbering the
3378 list of sections in the BFD. This could be handled more
3379 cleanly by, say, a new section flag; the existing
3380 SEC_NEVER_LOAD flag is not the one we want, because that one
3381 still implies that the section takes up space in the output
3382 file. */
3383 bfd_section_list_clear (abfd);
3384
4ad4eba5
AM
3385 /* Find the name to use in a DT_NEEDED entry that refers to this
3386 object. If the object has a DT_SONAME entry, we use it.
3387 Otherwise, if the generic linker stuck something in
3388 elf_dt_name, we use that. Otherwise, we just use the file
3389 name. */
3390 if (soname == NULL || *soname == '\0')
3391 {
3392 soname = elf_dt_name (abfd);
3393 if (soname == NULL || *soname == '\0')
3394 soname = bfd_get_filename (abfd);
3395 }
3396
3397 /* Save the SONAME because sometimes the linker emulation code
3398 will need to know it. */
3399 elf_dt_name (abfd) = soname;
3400
7e9f0867 3401 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3402 if (ret < 0)
3403 goto error_return;
3404
3405 /* If we have already included this dynamic object in the
3406 link, just ignore it. There is no reason to include a
3407 particular dynamic object more than once. */
3408 if (ret > 0)
3409 return TRUE;
3410 }
3411
3412 /* If this is a dynamic object, we always link against the .dynsym
3413 symbol table, not the .symtab symbol table. The dynamic linker
3414 will only see the .dynsym symbol table, so there is no reason to
3415 look at .symtab for a dynamic object. */
3416
3417 if (! dynamic || elf_dynsymtab (abfd) == 0)
3418 hdr = &elf_tdata (abfd)->symtab_hdr;
3419 else
3420 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3421
3422 symcount = hdr->sh_size / bed->s->sizeof_sym;
3423
3424 /* The sh_info field of the symtab header tells us where the
3425 external symbols start. We don't care about the local symbols at
3426 this point. */
3427 if (elf_bad_symtab (abfd))
3428 {
3429 extsymcount = symcount;
3430 extsymoff = 0;
3431 }
3432 else
3433 {
3434 extsymcount = symcount - hdr->sh_info;
3435 extsymoff = hdr->sh_info;
3436 }
3437
3438 sym_hash = NULL;
3439 if (extsymcount != 0)
3440 {
3441 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3442 NULL, NULL, NULL);
3443 if (isymbuf == NULL)
3444 goto error_return;
3445
3446 /* We store a pointer to the hash table entry for each external
3447 symbol. */
3448 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3449 sym_hash = bfd_alloc (abfd, amt);
3450 if (sym_hash == NULL)
3451 goto error_free_sym;
3452 elf_sym_hashes (abfd) = sym_hash;
3453 }
3454
3455 if (dynamic)
3456 {
3457 /* Read in any version definitions. */
fc0e6df6
PB
3458 if (!_bfd_elf_slurp_version_tables (abfd,
3459 info->default_imported_symver))
4ad4eba5
AM
3460 goto error_free_sym;
3461
3462 /* Read in the symbol versions, but don't bother to convert them
3463 to internal format. */
3464 if (elf_dynversym (abfd) != 0)
3465 {
3466 Elf_Internal_Shdr *versymhdr;
3467
3468 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3469 extversym = bfd_malloc (versymhdr->sh_size);
3470 if (extversym == NULL)
3471 goto error_free_sym;
3472 amt = versymhdr->sh_size;
3473 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3474 || bfd_bread (extversym, amt, abfd) != amt)
3475 goto error_free_vers;
3476 }
3477 }
3478
3479 weaks = NULL;
3480
3481 ever = extversym != NULL ? extversym + extsymoff : NULL;
3482 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3483 isym < isymend;
3484 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3485 {
3486 int bind;
3487 bfd_vma value;
3488 asection *sec;
3489 flagword flags;
3490 const char *name;
3491 struct elf_link_hash_entry *h;
3492 bfd_boolean definition;
3493 bfd_boolean size_change_ok;
3494 bfd_boolean type_change_ok;
3495 bfd_boolean new_weakdef;
3496 bfd_boolean override;
3497 unsigned int old_alignment;
3498 bfd *old_bfd;
3499
3500 override = FALSE;
3501
3502 flags = BSF_NO_FLAGS;
3503 sec = NULL;
3504 value = isym->st_value;
3505 *sym_hash = NULL;
3506
3507 bind = ELF_ST_BIND (isym->st_info);
3508 if (bind == STB_LOCAL)
3509 {
3510 /* This should be impossible, since ELF requires that all
3511 global symbols follow all local symbols, and that sh_info
3512 point to the first global symbol. Unfortunately, Irix 5
3513 screws this up. */
3514 continue;
3515 }
3516 else if (bind == STB_GLOBAL)
3517 {
3518 if (isym->st_shndx != SHN_UNDEF
3519 && isym->st_shndx != SHN_COMMON)
3520 flags = BSF_GLOBAL;
3521 }
3522 else if (bind == STB_WEAK)
3523 flags = BSF_WEAK;
3524 else
3525 {
3526 /* Leave it up to the processor backend. */
3527 }
3528
3529 if (isym->st_shndx == SHN_UNDEF)
3530 sec = bfd_und_section_ptr;
3531 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3532 {
3533 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3534 if (sec == NULL)
3535 sec = bfd_abs_section_ptr;
529fcb95
PB
3536 else if (sec->kept_section)
3537 {
3538 /* Symbols from discarded section are undefined. */
3539 sec = bfd_und_section_ptr;
3540 isym->st_shndx = SHN_UNDEF;
3541 }
4ad4eba5
AM
3542 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3543 value -= sec->vma;
3544 }
3545 else if (isym->st_shndx == SHN_ABS)
3546 sec = bfd_abs_section_ptr;
3547 else if (isym->st_shndx == SHN_COMMON)
3548 {
3549 sec = bfd_com_section_ptr;
3550 /* What ELF calls the size we call the value. What ELF
3551 calls the value we call the alignment. */
3552 value = isym->st_size;
3553 }
3554 else
3555 {
3556 /* Leave it up to the processor backend. */
3557 }
3558
3559 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3560 isym->st_name);
3561 if (name == NULL)
3562 goto error_free_vers;
3563
3564 if (isym->st_shndx == SHN_COMMON
3565 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3566 {
3567 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3568
3569 if (tcomm == NULL)
3570 {
3571 tcomm = bfd_make_section (abfd, ".tcommon");
3572 if (tcomm == NULL
3573 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3574 | SEC_IS_COMMON
3575 | SEC_LINKER_CREATED
3576 | SEC_THREAD_LOCAL)))
3577 goto error_free_vers;
3578 }
3579 sec = tcomm;
3580 }
3581 else if (add_symbol_hook)
3582 {
3583 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3584 &value))
3585 goto error_free_vers;
3586
3587 /* The hook function sets the name to NULL if this symbol
3588 should be skipped for some reason. */
3589 if (name == NULL)
3590 continue;
3591 }
3592
3593 /* Sanity check that all possibilities were handled. */
3594 if (sec == NULL)
3595 {
3596 bfd_set_error (bfd_error_bad_value);
3597 goto error_free_vers;
3598 }
3599
3600 if (bfd_is_und_section (sec)
3601 || bfd_is_com_section (sec))
3602 definition = FALSE;
3603 else
3604 definition = TRUE;
3605
3606 size_change_ok = FALSE;
3607 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3608 old_alignment = 0;
3609 old_bfd = NULL;
3610
3611 if (is_elf_hash_table (hash_table))
3612 {
3613 Elf_Internal_Versym iver;
3614 unsigned int vernum = 0;
3615 bfd_boolean skip;
3616
fc0e6df6 3617 if (ever == NULL)
4ad4eba5 3618 {
fc0e6df6
PB
3619 if (info->default_imported_symver)
3620 /* Use the default symbol version created earlier. */
3621 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3622 else
3623 iver.vs_vers = 0;
3624 }
3625 else
3626 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3627
3628 vernum = iver.vs_vers & VERSYM_VERSION;
3629
3630 /* If this is a hidden symbol, or if it is not version
3631 1, we append the version name to the symbol name.
3632 However, we do not modify a non-hidden absolute
3633 symbol, because it might be the version symbol
3634 itself. FIXME: What if it isn't? */
3635 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3636 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3637 {
3638 const char *verstr;
3639 size_t namelen, verlen, newlen;
3640 char *newname, *p;
3641
3642 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 3643 {
fc0e6df6
PB
3644 if (vernum > elf_tdata (abfd)->cverdefs)
3645 verstr = NULL;
3646 else if (vernum > 1)
3647 verstr =
3648 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3649 else
3650 verstr = "";
4ad4eba5 3651
fc0e6df6 3652 if (verstr == NULL)
4ad4eba5 3653 {
fc0e6df6
PB
3654 (*_bfd_error_handler)
3655 (_("%B: %s: invalid version %u (max %d)"),
3656 abfd, name, vernum,
3657 elf_tdata (abfd)->cverdefs);
3658 bfd_set_error (bfd_error_bad_value);
3659 goto error_free_vers;
4ad4eba5 3660 }
fc0e6df6
PB
3661 }
3662 else
3663 {
3664 /* We cannot simply test for the number of
3665 entries in the VERNEED section since the
3666 numbers for the needed versions do not start
3667 at 0. */
3668 Elf_Internal_Verneed *t;
3669
3670 verstr = NULL;
3671 for (t = elf_tdata (abfd)->verref;
3672 t != NULL;
3673 t = t->vn_nextref)
4ad4eba5 3674 {
fc0e6df6 3675 Elf_Internal_Vernaux *a;
4ad4eba5 3676
fc0e6df6
PB
3677 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3678 {
3679 if (a->vna_other == vernum)
4ad4eba5 3680 {
fc0e6df6
PB
3681 verstr = a->vna_nodename;
3682 break;
4ad4eba5 3683 }
4ad4eba5 3684 }
fc0e6df6
PB
3685 if (a != NULL)
3686 break;
3687 }
3688 if (verstr == NULL)
3689 {
3690 (*_bfd_error_handler)
3691 (_("%B: %s: invalid needed version %d"),
3692 abfd, name, vernum);
3693 bfd_set_error (bfd_error_bad_value);
3694 goto error_free_vers;
4ad4eba5 3695 }
4ad4eba5 3696 }
fc0e6df6
PB
3697
3698 namelen = strlen (name);
3699 verlen = strlen (verstr);
3700 newlen = namelen + verlen + 2;
3701 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3702 && isym->st_shndx != SHN_UNDEF)
3703 ++newlen;
3704
3705 newname = bfd_alloc (abfd, newlen);
3706 if (newname == NULL)
3707 goto error_free_vers;
3708 memcpy (newname, name, namelen);
3709 p = newname + namelen;
3710 *p++ = ELF_VER_CHR;
3711 /* If this is a defined non-hidden version symbol,
3712 we add another @ to the name. This indicates the
3713 default version of the symbol. */
3714 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3715 && isym->st_shndx != SHN_UNDEF)
3716 *p++ = ELF_VER_CHR;
3717 memcpy (p, verstr, verlen + 1);
3718
3719 name = newname;
4ad4eba5
AM
3720 }
3721
3722 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3723 sym_hash, &skip, &override,
3724 &type_change_ok, &size_change_ok))
3725 goto error_free_vers;
3726
3727 if (skip)
3728 continue;
3729
3730 if (override)
3731 definition = FALSE;
3732
3733 h = *sym_hash;
3734 while (h->root.type == bfd_link_hash_indirect
3735 || h->root.type == bfd_link_hash_warning)
3736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3737
3738 /* Remember the old alignment if this is a common symbol, so
3739 that we don't reduce the alignment later on. We can't
3740 check later, because _bfd_generic_link_add_one_symbol
3741 will set a default for the alignment which we want to
3742 override. We also remember the old bfd where the existing
3743 definition comes from. */
3744 switch (h->root.type)
3745 {
3746 default:
3747 break;
3748
3749 case bfd_link_hash_defined:
3750 case bfd_link_hash_defweak:
3751 old_bfd = h->root.u.def.section->owner;
3752 break;
3753
3754 case bfd_link_hash_common:
3755 old_bfd = h->root.u.c.p->section->owner;
3756 old_alignment = h->root.u.c.p->alignment_power;
3757 break;
3758 }
3759
3760 if (elf_tdata (abfd)->verdef != NULL
3761 && ! override
3762 && vernum > 1
3763 && definition)
3764 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3765 }
3766
3767 if (! (_bfd_generic_link_add_one_symbol
3768 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3769 (struct bfd_link_hash_entry **) sym_hash)))
3770 goto error_free_vers;
3771
3772 h = *sym_hash;
3773 while (h->root.type == bfd_link_hash_indirect
3774 || h->root.type == bfd_link_hash_warning)
3775 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3776 *sym_hash = h;
3777
3778 new_weakdef = FALSE;
3779 if (dynamic
3780 && definition
3781 && (flags & BSF_WEAK) != 0
3782 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3783 && is_elf_hash_table (hash_table)
f6e332e6 3784 && h->u.weakdef == NULL)
4ad4eba5
AM
3785 {
3786 /* Keep a list of all weak defined non function symbols from
3787 a dynamic object, using the weakdef field. Later in this
3788 function we will set the weakdef field to the correct
3789 value. We only put non-function symbols from dynamic
3790 objects on this list, because that happens to be the only
3791 time we need to know the normal symbol corresponding to a
3792 weak symbol, and the information is time consuming to
3793 figure out. If the weakdef field is not already NULL,
3794 then this symbol was already defined by some previous
3795 dynamic object, and we will be using that previous
3796 definition anyhow. */
3797
f6e332e6 3798 h->u.weakdef = weaks;
4ad4eba5
AM
3799 weaks = h;
3800 new_weakdef = TRUE;
3801 }
3802
3803 /* Set the alignment of a common symbol. */
3804 if (isym->st_shndx == SHN_COMMON
3805 && h->root.type == bfd_link_hash_common)
3806 {
3807 unsigned int align;
3808
3809 align = bfd_log2 (isym->st_value);
3810 if (align > old_alignment
3811 /* Permit an alignment power of zero if an alignment of one
3812 is specified and no other alignments have been specified. */
3813 || (isym->st_value == 1 && old_alignment == 0))
3814 h->root.u.c.p->alignment_power = align;
3815 else
3816 h->root.u.c.p->alignment_power = old_alignment;
3817 }
3818
3819 if (is_elf_hash_table (hash_table))
3820 {
4ad4eba5 3821 bfd_boolean dynsym;
4ad4eba5
AM
3822
3823 /* Check the alignment when a common symbol is involved. This
3824 can change when a common symbol is overridden by a normal
3825 definition or a common symbol is ignored due to the old
3826 normal definition. We need to make sure the maximum
3827 alignment is maintained. */
3828 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3829 && h->root.type != bfd_link_hash_common)
3830 {
3831 unsigned int common_align;
3832 unsigned int normal_align;
3833 unsigned int symbol_align;
3834 bfd *normal_bfd;
3835 bfd *common_bfd;
3836
3837 symbol_align = ffs (h->root.u.def.value) - 1;
3838 if (h->root.u.def.section->owner != NULL
3839 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3840 {
3841 normal_align = h->root.u.def.section->alignment_power;
3842 if (normal_align > symbol_align)
3843 normal_align = symbol_align;
3844 }
3845 else
3846 normal_align = symbol_align;
3847
3848 if (old_alignment)
3849 {
3850 common_align = old_alignment;
3851 common_bfd = old_bfd;
3852 normal_bfd = abfd;
3853 }
3854 else
3855 {
3856 common_align = bfd_log2 (isym->st_value);
3857 common_bfd = abfd;
3858 normal_bfd = old_bfd;
3859 }
3860
3861 if (normal_align < common_align)
3862 (*_bfd_error_handler)
d003868e
AM
3863 (_("Warning: alignment %u of symbol `%s' in %B"
3864 " is smaller than %u in %B"),
3865 normal_bfd, common_bfd,
3866 1 << normal_align, name, 1 << common_align);
4ad4eba5
AM
3867 }
3868
3869 /* Remember the symbol size and type. */
3870 if (isym->st_size != 0
3871 && (definition || h->size == 0))
3872 {
3873 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3874 (*_bfd_error_handler)
d003868e
AM
3875 (_("Warning: size of symbol `%s' changed"
3876 " from %lu in %B to %lu in %B"),
3877 old_bfd, abfd,
4ad4eba5 3878 name, (unsigned long) h->size,
d003868e 3879 (unsigned long) isym->st_size);
4ad4eba5
AM
3880
3881 h->size = isym->st_size;
3882 }
3883
3884 /* If this is a common symbol, then we always want H->SIZE
3885 to be the size of the common symbol. The code just above
3886 won't fix the size if a common symbol becomes larger. We
3887 don't warn about a size change here, because that is
3888 covered by --warn-common. */
3889 if (h->root.type == bfd_link_hash_common)
3890 h->size = h->root.u.c.size;
3891
3892 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3893 && (definition || h->type == STT_NOTYPE))
3894 {
3895 if (h->type != STT_NOTYPE
3896 && h->type != ELF_ST_TYPE (isym->st_info)
3897 && ! type_change_ok)
3898 (*_bfd_error_handler)
d003868e
AM
3899 (_("Warning: type of symbol `%s' changed"
3900 " from %d to %d in %B"),
3901 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4ad4eba5
AM
3902
3903 h->type = ELF_ST_TYPE (isym->st_info);
3904 }
3905
3906 /* If st_other has a processor-specific meaning, specific
3907 code might be needed here. We never merge the visibility
3908 attribute with the one from a dynamic object. */
3909 if (bed->elf_backend_merge_symbol_attribute)
3910 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3911 dynamic);
3912
b58f81ae
DJ
3913 /* If this symbol has default visibility and the user has requested
3914 we not re-export it, then mark it as hidden. */
3915 if (definition && !dynamic
3916 && (abfd->no_export
3917 || (abfd->my_archive && abfd->my_archive->no_export))
3918 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3919 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3920
4ad4eba5
AM
3921 if (isym->st_other != 0 && !dynamic)
3922 {
3923 unsigned char hvis, symvis, other, nvis;
3924
3925 /* Take the balance of OTHER from the definition. */
3926 other = (definition ? isym->st_other : h->other);
3927 other &= ~ ELF_ST_VISIBILITY (-1);
3928
3929 /* Combine visibilities, using the most constraining one. */
3930 hvis = ELF_ST_VISIBILITY (h->other);
3931 symvis = ELF_ST_VISIBILITY (isym->st_other);
3932 if (! hvis)
3933 nvis = symvis;
3934 else if (! symvis)
3935 nvis = hvis;
3936 else
3937 nvis = hvis < symvis ? hvis : symvis;
3938
3939 h->other = other | nvis;
3940 }
3941
3942 /* Set a flag in the hash table entry indicating the type of
3943 reference or definition we just found. Keep a count of
3944 the number of dynamic symbols we find. A dynamic symbol
3945 is one which is referenced or defined by both a regular
3946 object and a shared object. */
4ad4eba5
AM
3947 dynsym = FALSE;
3948 if (! dynamic)
3949 {
3950 if (! definition)
3951 {
f5385ebf 3952 h->ref_regular = 1;
4ad4eba5 3953 if (bind != STB_WEAK)
f5385ebf 3954 h->ref_regular_nonweak = 1;
4ad4eba5
AM
3955 }
3956 else
f5385ebf 3957 h->def_regular = 1;
4ad4eba5 3958 if (! info->executable
f5385ebf
AM
3959 || h->def_dynamic
3960 || h->ref_dynamic)
4ad4eba5
AM
3961 dynsym = TRUE;
3962 }
3963 else
3964 {
3965 if (! definition)
f5385ebf 3966 h->ref_dynamic = 1;
4ad4eba5 3967 else
f5385ebf
AM
3968 h->def_dynamic = 1;
3969 if (h->def_regular
3970 || h->ref_regular
f6e332e6 3971 || (h->u.weakdef != NULL
4ad4eba5 3972 && ! new_weakdef
f6e332e6 3973 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
3974 dynsym = TRUE;
3975 }
3976
4ad4eba5
AM
3977 /* Check to see if we need to add an indirect symbol for
3978 the default name. */
3979 if (definition || h->root.type == bfd_link_hash_common)
3980 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3981 &sec, &value, &dynsym,
3982 override))
3983 goto error_free_vers;
3984
3985 if (definition && !dynamic)
3986 {
3987 char *p = strchr (name, ELF_VER_CHR);
3988 if (p != NULL && p[1] != ELF_VER_CHR)
3989 {
3990 /* Queue non-default versions so that .symver x, x@FOO
3991 aliases can be checked. */
3992 if (! nondeflt_vers)
3993 {
3994 amt = (isymend - isym + 1)
3995 * sizeof (struct elf_link_hash_entry *);
3996 nondeflt_vers = bfd_malloc (amt);
3997 }
3998 nondeflt_vers [nondeflt_vers_cnt++] = h;
3999 }
4000 }
4001
4002 if (dynsym && h->dynindx == -1)
4003 {
c152c796 4004 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4005 goto error_free_vers;
f6e332e6 4006 if (h->u.weakdef != NULL
4ad4eba5 4007 && ! new_weakdef
f6e332e6 4008 && h->u.weakdef->dynindx == -1)
4ad4eba5 4009 {
f6e332e6 4010 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4011 goto error_free_vers;
4012 }
4013 }
4014 else if (dynsym && h->dynindx != -1)
4015 /* If the symbol already has a dynamic index, but
4016 visibility says it should not be visible, turn it into
4017 a local symbol. */
4018 switch (ELF_ST_VISIBILITY (h->other))
4019 {
4020 case STV_INTERNAL:
4021 case STV_HIDDEN:
4022 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4023 dynsym = FALSE;
4024 break;
4025 }
4026
4027 if (!add_needed
4028 && definition
4029 && dynsym
f5385ebf 4030 && h->ref_regular)
4ad4eba5
AM
4031 {
4032 int ret;
4033 const char *soname = elf_dt_name (abfd);
4034
4035 /* A symbol from a library loaded via DT_NEEDED of some
4036 other library is referenced by a regular object.
e56f61be
L
4037 Add a DT_NEEDED entry for it. Issue an error if
4038 --no-add-needed is used. */
4039 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4040 {
4041 (*_bfd_error_handler)
4042 (_("%s: invalid DSO for symbol `%s' definition"),
d003868e 4043 abfd, name);
e56f61be
L
4044 bfd_set_error (bfd_error_bad_value);
4045 goto error_free_vers;
4046 }
4047
a5db907e
AM
4048 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4049
4ad4eba5 4050 add_needed = TRUE;
7e9f0867 4051 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4052 if (ret < 0)
4053 goto error_free_vers;
4054
4055 BFD_ASSERT (ret == 0);
4056 }
4057 }
4058 }
4059
4060 /* Now that all the symbols from this input file are created, handle
4061 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4062 if (nondeflt_vers != NULL)
4063 {
4064 bfd_size_type cnt, symidx;
4065
4066 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4067 {
4068 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4069 char *shortname, *p;
4070
4071 p = strchr (h->root.root.string, ELF_VER_CHR);
4072 if (p == NULL
4073 || (h->root.type != bfd_link_hash_defined
4074 && h->root.type != bfd_link_hash_defweak))
4075 continue;
4076
4077 amt = p - h->root.root.string;
4078 shortname = bfd_malloc (amt + 1);
4079 memcpy (shortname, h->root.root.string, amt);
4080 shortname[amt] = '\0';
4081
4082 hi = (struct elf_link_hash_entry *)
4083 bfd_link_hash_lookup (&hash_table->root, shortname,
4084 FALSE, FALSE, FALSE);
4085 if (hi != NULL
4086 && hi->root.type == h->root.type
4087 && hi->root.u.def.value == h->root.u.def.value
4088 && hi->root.u.def.section == h->root.u.def.section)
4089 {
4090 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4091 hi->root.type = bfd_link_hash_indirect;
4092 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4093 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
4094 sym_hash = elf_sym_hashes (abfd);
4095 if (sym_hash)
4096 for (symidx = 0; symidx < extsymcount; ++symidx)
4097 if (sym_hash[symidx] == hi)
4098 {
4099 sym_hash[symidx] = h;
4100 break;
4101 }
4102 }
4103 free (shortname);
4104 }
4105 free (nondeflt_vers);
4106 nondeflt_vers = NULL;
4107 }
4108
4109 if (extversym != NULL)
4110 {
4111 free (extversym);
4112 extversym = NULL;
4113 }
4114
4115 if (isymbuf != NULL)
4116 free (isymbuf);
4117 isymbuf = NULL;
4118
ec13b3bb
AM
4119 if (!add_needed
4120 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
77cfaee6 4121 {
ec13b3bb
AM
4122 /* Remove symbols defined in an as-needed shared lib that wasn't
4123 needed. */
77cfaee6
AM
4124 struct elf_smash_syms_data inf;
4125 inf.not_needed = abfd;
4126 inf.htab = hash_table;
4127 inf.twiddled = FALSE;
4128 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4129 if (inf.twiddled)
4130 bfd_link_repair_undef_list (&hash_table->root);
4131 weaks = NULL;
4132 }
4133
4ad4eba5
AM
4134 /* Now set the weakdefs field correctly for all the weak defined
4135 symbols we found. The only way to do this is to search all the
4136 symbols. Since we only need the information for non functions in
4137 dynamic objects, that's the only time we actually put anything on
4138 the list WEAKS. We need this information so that if a regular
4139 object refers to a symbol defined weakly in a dynamic object, the
4140 real symbol in the dynamic object is also put in the dynamic
4141 symbols; we also must arrange for both symbols to point to the
4142 same memory location. We could handle the general case of symbol
4143 aliasing, but a general symbol alias can only be generated in
4144 assembler code, handling it correctly would be very time
4145 consuming, and other ELF linkers don't handle general aliasing
4146 either. */
4147 if (weaks != NULL)
4148 {
4149 struct elf_link_hash_entry **hpp;
4150 struct elf_link_hash_entry **hppend;
4151 struct elf_link_hash_entry **sorted_sym_hash;
4152 struct elf_link_hash_entry *h;
4153 size_t sym_count;
4154
4155 /* Since we have to search the whole symbol list for each weak
4156 defined symbol, search time for N weak defined symbols will be
4157 O(N^2). Binary search will cut it down to O(NlogN). */
4158 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4159 sorted_sym_hash = bfd_malloc (amt);
4160 if (sorted_sym_hash == NULL)
4161 goto error_return;
4162 sym_hash = sorted_sym_hash;
4163 hpp = elf_sym_hashes (abfd);
4164 hppend = hpp + extsymcount;
4165 sym_count = 0;
4166 for (; hpp < hppend; hpp++)
4167 {
4168 h = *hpp;
4169 if (h != NULL
4170 && h->root.type == bfd_link_hash_defined
4171 && h->type != STT_FUNC)
4172 {
4173 *sym_hash = h;
4174 sym_hash++;
4175 sym_count++;
4176 }
4177 }
4178
4179 qsort (sorted_sym_hash, sym_count,
4180 sizeof (struct elf_link_hash_entry *),
4181 elf_sort_symbol);
4182
4183 while (weaks != NULL)
4184 {
4185 struct elf_link_hash_entry *hlook;
4186 asection *slook;
4187 bfd_vma vlook;
4188 long ilook;
4189 size_t i, j, idx;
4190
4191 hlook = weaks;
f6e332e6
AM
4192 weaks = hlook->u.weakdef;
4193 hlook->u.weakdef = NULL;
4ad4eba5
AM
4194
4195 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4196 || hlook->root.type == bfd_link_hash_defweak
4197 || hlook->root.type == bfd_link_hash_common
4198 || hlook->root.type == bfd_link_hash_indirect);
4199 slook = hlook->root.u.def.section;
4200 vlook = hlook->root.u.def.value;
4201
4202 ilook = -1;
4203 i = 0;
4204 j = sym_count;
4205 while (i < j)
4206 {
4207 bfd_signed_vma vdiff;
4208 idx = (i + j) / 2;
4209 h = sorted_sym_hash [idx];
4210 vdiff = vlook - h->root.u.def.value;
4211 if (vdiff < 0)
4212 j = idx;
4213 else if (vdiff > 0)
4214 i = idx + 1;
4215 else
4216 {
a9b881be 4217 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4218 if (sdiff < 0)
4219 j = idx;
4220 else if (sdiff > 0)
4221 i = idx + 1;
4222 else
4223 {
4224 ilook = idx;
4225 break;
4226 }
4227 }
4228 }
4229
4230 /* We didn't find a value/section match. */
4231 if (ilook == -1)
4232 continue;
4233
4234 for (i = ilook; i < sym_count; i++)
4235 {
4236 h = sorted_sym_hash [i];
4237
4238 /* Stop if value or section doesn't match. */
4239 if (h->root.u.def.value != vlook
4240 || h->root.u.def.section != slook)
4241 break;
4242 else if (h != hlook)
4243 {
f6e332e6 4244 hlook->u.weakdef = h;
4ad4eba5
AM
4245
4246 /* If the weak definition is in the list of dynamic
4247 symbols, make sure the real definition is put
4248 there as well. */
4249 if (hlook->dynindx != -1 && h->dynindx == -1)
4250 {
c152c796 4251 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5
AM
4252 goto error_return;
4253 }
4254
4255 /* If the real definition is in the list of dynamic
4256 symbols, make sure the weak definition is put
4257 there as well. If we don't do this, then the
4258 dynamic loader might not merge the entries for the
4259 real definition and the weak definition. */
4260 if (h->dynindx != -1 && hlook->dynindx == -1)
4261 {
c152c796 4262 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4ad4eba5
AM
4263 goto error_return;
4264 }
4265 break;
4266 }
4267 }
4268 }
4269
4270 free (sorted_sym_hash);
4271 }
4272
85fbca6a
NC
4273 check_directives = get_elf_backend_data (abfd)->check_directives;
4274 if (check_directives)
4275 check_directives (abfd, info);
4276
4ad4eba5
AM
4277 /* If this object is the same format as the output object, and it is
4278 not a shared library, then let the backend look through the
4279 relocs.
4280
4281 This is required to build global offset table entries and to
4282 arrange for dynamic relocs. It is not required for the
4283 particular common case of linking non PIC code, even when linking
4284 against shared libraries, but unfortunately there is no way of
4285 knowing whether an object file has been compiled PIC or not.
4286 Looking through the relocs is not particularly time consuming.
4287 The problem is that we must either (1) keep the relocs in memory,
4288 which causes the linker to require additional runtime memory or
4289 (2) read the relocs twice from the input file, which wastes time.
4290 This would be a good case for using mmap.
4291
4292 I have no idea how to handle linking PIC code into a file of a
4293 different format. It probably can't be done. */
4294 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4295 if (! dynamic
4296 && is_elf_hash_table (hash_table)
4297 && hash_table->root.creator == abfd->xvec
4298 && check_relocs != NULL)
4299 {
4300 asection *o;
4301
4302 for (o = abfd->sections; o != NULL; o = o->next)
4303 {
4304 Elf_Internal_Rela *internal_relocs;
4305 bfd_boolean ok;
4306
4307 if ((o->flags & SEC_RELOC) == 0
4308 || o->reloc_count == 0
4309 || ((info->strip == strip_all || info->strip == strip_debugger)
4310 && (o->flags & SEC_DEBUGGING) != 0)
4311 || bfd_is_abs_section (o->output_section))
4312 continue;
4313
4314 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4315 info->keep_memory);
4316 if (internal_relocs == NULL)
4317 goto error_return;
4318
4319 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4320
4321 if (elf_section_data (o)->relocs != internal_relocs)
4322 free (internal_relocs);
4323
4324 if (! ok)
4325 goto error_return;
4326 }
4327 }
4328
4329 /* If this is a non-traditional link, try to optimize the handling
4330 of the .stab/.stabstr sections. */
4331 if (! dynamic
4332 && ! info->traditional_format
4333 && is_elf_hash_table (hash_table)
4334 && (info->strip != strip_all && info->strip != strip_debugger))
4335 {
4336 asection *stabstr;
4337
4338 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4339 if (stabstr != NULL)
4340 {
4341 bfd_size_type string_offset = 0;
4342 asection *stab;
4343
4344 for (stab = abfd->sections; stab; stab = stab->next)
4345 if (strncmp (".stab", stab->name, 5) == 0
4346 && (!stab->name[5] ||
4347 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4348 && (stab->flags & SEC_MERGE) == 0
4349 && !bfd_is_abs_section (stab->output_section))
4350 {
4351 struct bfd_elf_section_data *secdata;
4352
4353 secdata = elf_section_data (stab);
4354 if (! _bfd_link_section_stabs (abfd,
3722b82f 4355 &hash_table->stab_info,
4ad4eba5
AM
4356 stab, stabstr,
4357 &secdata->sec_info,
4358 &string_offset))
4359 goto error_return;
4360 if (secdata->sec_info)
4361 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4362 }
4363 }
4364 }
4365
77cfaee6 4366 if (is_elf_hash_table (hash_table) && add_needed)
4ad4eba5
AM
4367 {
4368 /* Add this bfd to the loaded list. */
4369 struct elf_link_loaded_list *n;
4370
4371 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4372 if (n == NULL)
4373 goto error_return;
4374 n->abfd = abfd;
4375 n->next = hash_table->loaded;
4376 hash_table->loaded = n;
4377 }
4378
4379 return TRUE;
4380
4381 error_free_vers:
4382 if (nondeflt_vers != NULL)
4383 free (nondeflt_vers);
4384 if (extversym != NULL)
4385 free (extversym);
4386 error_free_sym:
4387 if (isymbuf != NULL)
4388 free (isymbuf);
4389 error_return:
4390 return FALSE;
4391}
4392
8387904d
AM
4393/* Return the linker hash table entry of a symbol that might be
4394 satisfied by an archive symbol. Return -1 on error. */
4395
4396struct elf_link_hash_entry *
4397_bfd_elf_archive_symbol_lookup (bfd *abfd,
4398 struct bfd_link_info *info,
4399 const char *name)
4400{
4401 struct elf_link_hash_entry *h;
4402 char *p, *copy;
4403 size_t len, first;
4404
4405 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4406 if (h != NULL)
4407 return h;
4408
4409 /* If this is a default version (the name contains @@), look up the
4410 symbol again with only one `@' as well as without the version.
4411 The effect is that references to the symbol with and without the
4412 version will be matched by the default symbol in the archive. */
4413
4414 p = strchr (name, ELF_VER_CHR);
4415 if (p == NULL || p[1] != ELF_VER_CHR)
4416 return h;
4417
4418 /* First check with only one `@'. */
4419 len = strlen (name);
4420 copy = bfd_alloc (abfd, len);
4421 if (copy == NULL)
4422 return (struct elf_link_hash_entry *) 0 - 1;
4423
4424 first = p - name + 1;
4425 memcpy (copy, name, first);
4426 memcpy (copy + first, name + first + 1, len - first);
4427
4428 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4429 if (h == NULL)
4430 {
4431 /* We also need to check references to the symbol without the
4432 version. */
4433 copy[first - 1] = '\0';
4434 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4435 FALSE, FALSE, FALSE);
4436 }
4437
4438 bfd_release (abfd, copy);
4439 return h;
4440}
4441
0ad989f9
L
4442/* Add symbols from an ELF archive file to the linker hash table. We
4443 don't use _bfd_generic_link_add_archive_symbols because of a
4444 problem which arises on UnixWare. The UnixWare libc.so is an
4445 archive which includes an entry libc.so.1 which defines a bunch of
4446 symbols. The libc.so archive also includes a number of other
4447 object files, which also define symbols, some of which are the same
4448 as those defined in libc.so.1. Correct linking requires that we
4449 consider each object file in turn, and include it if it defines any
4450 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4451 this; it looks through the list of undefined symbols, and includes
4452 any object file which defines them. When this algorithm is used on
4453 UnixWare, it winds up pulling in libc.so.1 early and defining a
4454 bunch of symbols. This means that some of the other objects in the
4455 archive are not included in the link, which is incorrect since they
4456 precede libc.so.1 in the archive.
4457
4458 Fortunately, ELF archive handling is simpler than that done by
4459 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4460 oddities. In ELF, if we find a symbol in the archive map, and the
4461 symbol is currently undefined, we know that we must pull in that
4462 object file.
4463
4464 Unfortunately, we do have to make multiple passes over the symbol
4465 table until nothing further is resolved. */
4466
4ad4eba5
AM
4467static bfd_boolean
4468elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4469{
4470 symindex c;
4471 bfd_boolean *defined = NULL;
4472 bfd_boolean *included = NULL;
4473 carsym *symdefs;
4474 bfd_boolean loop;
4475 bfd_size_type amt;
8387904d
AM
4476 const struct elf_backend_data *bed;
4477 struct elf_link_hash_entry * (*archive_symbol_lookup)
4478 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4479
4480 if (! bfd_has_map (abfd))
4481 {
4482 /* An empty archive is a special case. */
4483 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4484 return TRUE;
4485 bfd_set_error (bfd_error_no_armap);
4486 return FALSE;
4487 }
4488
4489 /* Keep track of all symbols we know to be already defined, and all
4490 files we know to be already included. This is to speed up the
4491 second and subsequent passes. */
4492 c = bfd_ardata (abfd)->symdef_count;
4493 if (c == 0)
4494 return TRUE;
4495 amt = c;
4496 amt *= sizeof (bfd_boolean);
4497 defined = bfd_zmalloc (amt);
4498 included = bfd_zmalloc (amt);
4499 if (defined == NULL || included == NULL)
4500 goto error_return;
4501
4502 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4503 bed = get_elf_backend_data (abfd);
4504 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4505
4506 do
4507 {
4508 file_ptr last;
4509 symindex i;
4510 carsym *symdef;
4511 carsym *symdefend;
4512
4513 loop = FALSE;
4514 last = -1;
4515
4516 symdef = symdefs;
4517 symdefend = symdef + c;
4518 for (i = 0; symdef < symdefend; symdef++, i++)
4519 {
4520 struct elf_link_hash_entry *h;
4521 bfd *element;
4522 struct bfd_link_hash_entry *undefs_tail;
4523 symindex mark;
4524
4525 if (defined[i] || included[i])
4526 continue;
4527 if (symdef->file_offset == last)
4528 {
4529 included[i] = TRUE;
4530 continue;
4531 }
4532
8387904d
AM
4533 h = archive_symbol_lookup (abfd, info, symdef->name);
4534 if (h == (struct elf_link_hash_entry *) 0 - 1)
4535 goto error_return;
0ad989f9
L
4536
4537 if (h == NULL)
4538 continue;
4539
4540 if (h->root.type == bfd_link_hash_common)
4541 {
4542 /* We currently have a common symbol. The archive map contains
4543 a reference to this symbol, so we may want to include it. We
4544 only want to include it however, if this archive element
4545 contains a definition of the symbol, not just another common
4546 declaration of it.
4547
4548 Unfortunately some archivers (including GNU ar) will put
4549 declarations of common symbols into their archive maps, as
4550 well as real definitions, so we cannot just go by the archive
4551 map alone. Instead we must read in the element's symbol
4552 table and check that to see what kind of symbol definition
4553 this is. */
4554 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4555 continue;
4556 }
4557 else if (h->root.type != bfd_link_hash_undefined)
4558 {
4559 if (h->root.type != bfd_link_hash_undefweak)
4560 defined[i] = TRUE;
4561 continue;
4562 }
4563
4564 /* We need to include this archive member. */
4565 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4566 if (element == NULL)
4567 goto error_return;
4568
4569 if (! bfd_check_format (element, bfd_object))
4570 goto error_return;
4571
4572 /* Doublecheck that we have not included this object
4573 already--it should be impossible, but there may be
4574 something wrong with the archive. */
4575 if (element->archive_pass != 0)
4576 {
4577 bfd_set_error (bfd_error_bad_value);
4578 goto error_return;
4579 }
4580 element->archive_pass = 1;
4581
4582 undefs_tail = info->hash->undefs_tail;
4583
4584 if (! (*info->callbacks->add_archive_element) (info, element,
4585 symdef->name))
4586 goto error_return;
4587 if (! bfd_link_add_symbols (element, info))
4588 goto error_return;
4589
4590 /* If there are any new undefined symbols, we need to make
4591 another pass through the archive in order to see whether
4592 they can be defined. FIXME: This isn't perfect, because
4593 common symbols wind up on undefs_tail and because an
4594 undefined symbol which is defined later on in this pass
4595 does not require another pass. This isn't a bug, but it
4596 does make the code less efficient than it could be. */
4597 if (undefs_tail != info->hash->undefs_tail)
4598 loop = TRUE;
4599
4600 /* Look backward to mark all symbols from this object file
4601 which we have already seen in this pass. */
4602 mark = i;
4603 do
4604 {
4605 included[mark] = TRUE;
4606 if (mark == 0)
4607 break;
4608 --mark;
4609 }
4610 while (symdefs[mark].file_offset == symdef->file_offset);
4611
4612 /* We mark subsequent symbols from this object file as we go
4613 on through the loop. */
4614 last = symdef->file_offset;
4615 }
4616 }
4617 while (loop);
4618
4619 free (defined);
4620 free (included);
4621
4622 return TRUE;
4623
4624 error_return:
4625 if (defined != NULL)
4626 free (defined);
4627 if (included != NULL)
4628 free (included);
4629 return FALSE;
4630}
4ad4eba5
AM
4631
4632/* Given an ELF BFD, add symbols to the global hash table as
4633 appropriate. */
4634
4635bfd_boolean
4636bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4637{
4638 switch (bfd_get_format (abfd))
4639 {
4640 case bfd_object:
4641 return elf_link_add_object_symbols (abfd, info);
4642 case bfd_archive:
4643 return elf_link_add_archive_symbols (abfd, info);
4644 default:
4645 bfd_set_error (bfd_error_wrong_format);
4646 return FALSE;
4647 }
4648}
5a580b3a
AM
4649\f
4650/* This function will be called though elf_link_hash_traverse to store
4651 all hash value of the exported symbols in an array. */
4652
4653static bfd_boolean
4654elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4655{
4656 unsigned long **valuep = data;
4657 const char *name;
4658 char *p;
4659 unsigned long ha;
4660 char *alc = NULL;
4661
4662 if (h->root.type == bfd_link_hash_warning)
4663 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4664
4665 /* Ignore indirect symbols. These are added by the versioning code. */
4666 if (h->dynindx == -1)
4667 return TRUE;
4668
4669 name = h->root.root.string;
4670 p = strchr (name, ELF_VER_CHR);
4671 if (p != NULL)
4672 {
4673 alc = bfd_malloc (p - name + 1);
4674 memcpy (alc, name, p - name);
4675 alc[p - name] = '\0';
4676 name = alc;
4677 }
4678
4679 /* Compute the hash value. */
4680 ha = bfd_elf_hash (name);
4681
4682 /* Store the found hash value in the array given as the argument. */
4683 *(*valuep)++ = ha;
4684
4685 /* And store it in the struct so that we can put it in the hash table
4686 later. */
f6e332e6 4687 h->u.elf_hash_value = ha;
5a580b3a
AM
4688
4689 if (alc != NULL)
4690 free (alc);
4691
4692 return TRUE;
4693}
4694
4695/* Array used to determine the number of hash table buckets to use
4696 based on the number of symbols there are. If there are fewer than
4697 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4698 fewer than 37 we use 17 buckets, and so forth. We never use more
4699 than 32771 buckets. */
4700
4701static const size_t elf_buckets[] =
4702{
4703 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4704 16411, 32771, 0
4705};
4706
4707/* Compute bucket count for hashing table. We do not use a static set
4708 of possible tables sizes anymore. Instead we determine for all
4709 possible reasonable sizes of the table the outcome (i.e., the
4710 number of collisions etc) and choose the best solution. The
4711 weighting functions are not too simple to allow the table to grow
4712 without bounds. Instead one of the weighting factors is the size.
4713 Therefore the result is always a good payoff between few collisions
4714 (= short chain lengths) and table size. */
4715static size_t
4716compute_bucket_count (struct bfd_link_info *info)
4717{
4718 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4719 size_t best_size = 0;
4720 unsigned long int *hashcodes;
4721 unsigned long int *hashcodesp;
4722 unsigned long int i;
4723 bfd_size_type amt;
4724
4725 /* Compute the hash values for all exported symbols. At the same
4726 time store the values in an array so that we could use them for
4727 optimizations. */
4728 amt = dynsymcount;
4729 amt *= sizeof (unsigned long int);
4730 hashcodes = bfd_malloc (amt);
4731 if (hashcodes == NULL)
4732 return 0;
4733 hashcodesp = hashcodes;
4734
4735 /* Put all hash values in HASHCODES. */
4736 elf_link_hash_traverse (elf_hash_table (info),
4737 elf_collect_hash_codes, &hashcodesp);
4738
4739 /* We have a problem here. The following code to optimize the table
4740 size requires an integer type with more the 32 bits. If
4741 BFD_HOST_U_64_BIT is set we know about such a type. */
4742#ifdef BFD_HOST_U_64_BIT
4743 if (info->optimize)
4744 {
4745 unsigned long int nsyms = hashcodesp - hashcodes;
4746 size_t minsize;
4747 size_t maxsize;
4748 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4749 unsigned long int *counts ;
4750 bfd *dynobj = elf_hash_table (info)->dynobj;
4751 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4752
4753 /* Possible optimization parameters: if we have NSYMS symbols we say
4754 that the hashing table must at least have NSYMS/4 and at most
4755 2*NSYMS buckets. */
4756 minsize = nsyms / 4;
4757 if (minsize == 0)
4758 minsize = 1;
4759 best_size = maxsize = nsyms * 2;
4760
4761 /* Create array where we count the collisions in. We must use bfd_malloc
4762 since the size could be large. */
4763 amt = maxsize;
4764 amt *= sizeof (unsigned long int);
4765 counts = bfd_malloc (amt);
4766 if (counts == NULL)
4767 {
4768 free (hashcodes);
4769 return 0;
4770 }
4771
4772 /* Compute the "optimal" size for the hash table. The criteria is a
4773 minimal chain length. The minor criteria is (of course) the size
4774 of the table. */
4775 for (i = minsize; i < maxsize; ++i)
4776 {
4777 /* Walk through the array of hashcodes and count the collisions. */
4778 BFD_HOST_U_64_BIT max;
4779 unsigned long int j;
4780 unsigned long int fact;
4781
4782 memset (counts, '\0', i * sizeof (unsigned long int));
4783
4784 /* Determine how often each hash bucket is used. */
4785 for (j = 0; j < nsyms; ++j)
4786 ++counts[hashcodes[j] % i];
4787
4788 /* For the weight function we need some information about the
4789 pagesize on the target. This is information need not be 100%
4790 accurate. Since this information is not available (so far) we
4791 define it here to a reasonable default value. If it is crucial
4792 to have a better value some day simply define this value. */
4793# ifndef BFD_TARGET_PAGESIZE
4794# define BFD_TARGET_PAGESIZE (4096)
4795# endif
4796
4797 /* We in any case need 2 + NSYMS entries for the size values and
4798 the chains. */
4799 max = (2 + nsyms) * (bed->s->arch_size / 8);
4800
4801# if 1
4802 /* Variant 1: optimize for short chains. We add the squares
4803 of all the chain lengths (which favors many small chain
4804 over a few long chains). */
4805 for (j = 0; j < i; ++j)
4806 max += counts[j] * counts[j];
4807
4808 /* This adds penalties for the overall size of the table. */
4809 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4810 max *= fact * fact;
4811# else
4812 /* Variant 2: Optimize a lot more for small table. Here we
4813 also add squares of the size but we also add penalties for
4814 empty slots (the +1 term). */
4815 for (j = 0; j < i; ++j)
4816 max += (1 + counts[j]) * (1 + counts[j]);
4817
4818 /* The overall size of the table is considered, but not as
4819 strong as in variant 1, where it is squared. */
4820 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4821 max *= fact;
4822# endif
4823
4824 /* Compare with current best results. */
4825 if (max < best_chlen)
4826 {
4827 best_chlen = max;
4828 best_size = i;
4829 }
4830 }
4831
4832 free (counts);
4833 }
4834 else
4835#endif /* defined (BFD_HOST_U_64_BIT) */
4836 {
4837 /* This is the fallback solution if no 64bit type is available or if we
4838 are not supposed to spend much time on optimizations. We select the
4839 bucket count using a fixed set of numbers. */
4840 for (i = 0; elf_buckets[i] != 0; i++)
4841 {
4842 best_size = elf_buckets[i];
4843 if (dynsymcount < elf_buckets[i + 1])
4844 break;
4845 }
4846 }
4847
4848 /* Free the arrays we needed. */
4849 free (hashcodes);
4850
4851 return best_size;
4852}
4853
4854/* Set up the sizes and contents of the ELF dynamic sections. This is
4855 called by the ELF linker emulation before_allocation routine. We
4856 must set the sizes of the sections before the linker sets the
4857 addresses of the various sections. */
4858
4859bfd_boolean
4860bfd_elf_size_dynamic_sections (bfd *output_bfd,
4861 const char *soname,
4862 const char *rpath,
4863 const char *filter_shlib,
4864 const char * const *auxiliary_filters,
4865 struct bfd_link_info *info,
4866 asection **sinterpptr,
4867 struct bfd_elf_version_tree *verdefs)
4868{
4869 bfd_size_type soname_indx;
4870 bfd *dynobj;
4871 const struct elf_backend_data *bed;
4872 struct elf_assign_sym_version_info asvinfo;
4873
4874 *sinterpptr = NULL;
4875
4876 soname_indx = (bfd_size_type) -1;
4877
4878 if (!is_elf_hash_table (info->hash))
4879 return TRUE;
4880
8c37241b 4881 elf_tdata (output_bfd)->relro = info->relro;
5a580b3a
AM
4882 if (info->execstack)
4883 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4884 else if (info->noexecstack)
4885 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4886 else
4887 {
4888 bfd *inputobj;
4889 asection *notesec = NULL;
4890 int exec = 0;
4891
4892 for (inputobj = info->input_bfds;
4893 inputobj;
4894 inputobj = inputobj->link_next)
4895 {
4896 asection *s;
4897
d457dcf6 4898 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5a580b3a
AM
4899 continue;
4900 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4901 if (s)
4902 {
4903 if (s->flags & SEC_CODE)
4904 exec = PF_X;
4905 notesec = s;
4906 }
4907 else
4908 exec = PF_X;
4909 }
4910 if (notesec)
4911 {
4912 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4913 if (exec && info->relocatable
4914 && notesec->output_section != bfd_abs_section_ptr)
4915 notesec->output_section->flags |= SEC_CODE;
4916 }
4917 }
4918
4919 /* Any syms created from now on start with -1 in
4920 got.refcount/offset and plt.refcount/offset. */
4921 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4922
4923 /* The backend may have to create some sections regardless of whether
4924 we're dynamic or not. */
4925 bed = get_elf_backend_data (output_bfd);
4926 if (bed->elf_backend_always_size_sections
4927 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4928 return FALSE;
4929
4930 dynobj = elf_hash_table (info)->dynobj;
4931
4932 /* If there were no dynamic objects in the link, there is nothing to
4933 do here. */
4934 if (dynobj == NULL)
4935 return TRUE;
4936
4937 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4938 return FALSE;
4939
4940 if (elf_hash_table (info)->dynamic_sections_created)
4941 {
4942 struct elf_info_failed eif;
4943 struct elf_link_hash_entry *h;
4944 asection *dynstr;
4945 struct bfd_elf_version_tree *t;
4946 struct bfd_elf_version_expr *d;
4947 bfd_boolean all_defined;
4948
4949 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4950 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4951
4952 if (soname != NULL)
4953 {
4954 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4955 soname, TRUE);
4956 if (soname_indx == (bfd_size_type) -1
4957 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4958 return FALSE;
4959 }
4960
4961 if (info->symbolic)
4962 {
4963 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4964 return FALSE;
4965 info->flags |= DF_SYMBOLIC;
4966 }
4967
4968 if (rpath != NULL)
4969 {
4970 bfd_size_type indx;
4971
4972 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4973 TRUE);
4974 if (indx == (bfd_size_type) -1
4975 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4976 return FALSE;
4977
4978 if (info->new_dtags)
4979 {
4980 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4981 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4982 return FALSE;
4983 }
4984 }
4985
4986 if (filter_shlib != NULL)
4987 {
4988 bfd_size_type indx;
4989
4990 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4991 filter_shlib, TRUE);
4992 if (indx == (bfd_size_type) -1
4993 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4994 return FALSE;
4995 }
4996
4997 if (auxiliary_filters != NULL)
4998 {
4999 const char * const *p;
5000
5001 for (p = auxiliary_filters; *p != NULL; p++)
5002 {
5003 bfd_size_type indx;
5004
5005 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5006 *p, TRUE);
5007 if (indx == (bfd_size_type) -1
5008 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5009 return FALSE;
5010 }
5011 }
5012
5013 eif.info = info;
5014 eif.verdefs = verdefs;
5015 eif.failed = FALSE;
5016
5017 /* If we are supposed to export all symbols into the dynamic symbol
5018 table (this is not the normal case), then do so. */
5019 if (info->export_dynamic)
5020 {
5021 elf_link_hash_traverse (elf_hash_table (info),
5022 _bfd_elf_export_symbol,
5023 &eif);
5024 if (eif.failed)
5025 return FALSE;
5026 }
5027
5028 /* Make all global versions with definition. */
5029 for (t = verdefs; t != NULL; t = t->next)
5030 for (d = t->globals.list; d != NULL; d = d->next)
5031 if (!d->symver && d->symbol)
5032 {
5033 const char *verstr, *name;
5034 size_t namelen, verlen, newlen;
5035 char *newname, *p;
5036 struct elf_link_hash_entry *newh;
5037
5038 name = d->symbol;
5039 namelen = strlen (name);
5040 verstr = t->name;
5041 verlen = strlen (verstr);
5042 newlen = namelen + verlen + 3;
5043
5044 newname = bfd_malloc (newlen);
5045 if (newname == NULL)
5046 return FALSE;
5047 memcpy (newname, name, namelen);
5048
5049 /* Check the hidden versioned definition. */
5050 p = newname + namelen;
5051 *p++ = ELF_VER_CHR;
5052 memcpy (p, verstr, verlen + 1);
5053 newh = elf_link_hash_lookup (elf_hash_table (info),
5054 newname, FALSE, FALSE,
5055 FALSE);
5056 if (newh == NULL
5057 || (newh->root.type != bfd_link_hash_defined
5058 && newh->root.type != bfd_link_hash_defweak))
5059 {
5060 /* Check the default versioned definition. */
5061 *p++ = ELF_VER_CHR;
5062 memcpy (p, verstr, verlen + 1);
5063 newh = elf_link_hash_lookup (elf_hash_table (info),
5064 newname, FALSE, FALSE,
5065 FALSE);
5066 }
5067 free (newname);
5068
5069 /* Mark this version if there is a definition and it is
5070 not defined in a shared object. */
5071 if (newh != NULL
f5385ebf 5072 && !newh->def_dynamic
5a580b3a
AM
5073 && (newh->root.type == bfd_link_hash_defined
5074 || newh->root.type == bfd_link_hash_defweak))
5075 d->symver = 1;
5076 }
5077
5078 /* Attach all the symbols to their version information. */
5079 asvinfo.output_bfd = output_bfd;
5080 asvinfo.info = info;
5081 asvinfo.verdefs = verdefs;
5082 asvinfo.failed = FALSE;
5083
5084 elf_link_hash_traverse (elf_hash_table (info),
5085 _bfd_elf_link_assign_sym_version,
5086 &asvinfo);
5087 if (asvinfo.failed)
5088 return FALSE;
5089
5090 if (!info->allow_undefined_version)
5091 {
5092 /* Check if all global versions have a definition. */
5093 all_defined = TRUE;
5094 for (t = verdefs; t != NULL; t = t->next)
5095 for (d = t->globals.list; d != NULL; d = d->next)
5096 if (!d->symver && !d->script)
5097 {
5098 (*_bfd_error_handler)
5099 (_("%s: undefined version: %s"),
5100 d->pattern, t->name);
5101 all_defined = FALSE;
5102 }
5103
5104 if (!all_defined)
5105 {
5106 bfd_set_error (bfd_error_bad_value);
5107 return FALSE;
5108 }
5109 }
5110
5111 /* Find all symbols which were defined in a dynamic object and make
5112 the backend pick a reasonable value for them. */
5113 elf_link_hash_traverse (elf_hash_table (info),
5114 _bfd_elf_adjust_dynamic_symbol,
5115 &eif);
5116 if (eif.failed)
5117 return FALSE;
5118
5119 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5120 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5121 now so that we know the final size of the .dynamic section. */
5122
5123 /* If there are initialization and/or finalization functions to
5124 call then add the corresponding DT_INIT/DT_FINI entries. */
5125 h = (info->init_function
5126 ? elf_link_hash_lookup (elf_hash_table (info),
5127 info->init_function, FALSE,
5128 FALSE, FALSE)
5129 : NULL);
5130 if (h != NULL
f5385ebf
AM
5131 && (h->ref_regular
5132 || h->def_regular))
5a580b3a
AM
5133 {
5134 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5135 return FALSE;
5136 }
5137 h = (info->fini_function
5138 ? elf_link_hash_lookup (elf_hash_table (info),
5139 info->fini_function, FALSE,
5140 FALSE, FALSE)
5141 : NULL);
5142 if (h != NULL
f5385ebf
AM
5143 && (h->ref_regular
5144 || h->def_regular))
5a580b3a
AM
5145 {
5146 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5147 return FALSE;
5148 }
5149
5150 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5151 {
5152 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5153 if (! info->executable)
5154 {
5155 bfd *sub;
5156 asection *o;
5157
5158 for (sub = info->input_bfds; sub != NULL;
5159 sub = sub->link_next)
5160 for (o = sub->sections; o != NULL; o = o->next)
5161 if (elf_section_data (o)->this_hdr.sh_type
5162 == SHT_PREINIT_ARRAY)
5163 {
5164 (*_bfd_error_handler)
d003868e
AM
5165 (_("%B: .preinit_array section is not allowed in DSO"),
5166 sub);
5a580b3a
AM
5167 break;
5168 }
5169
5170 bfd_set_error (bfd_error_nonrepresentable_section);
5171 return FALSE;
5172 }
5173
5174 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5175 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5176 return FALSE;
5177 }
5178 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5179 {
5180 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5181 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5182 return FALSE;
5183 }
5184 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5185 {
5186 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5187 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5188 return FALSE;
5189 }
5190
5191 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5192 /* If .dynstr is excluded from the link, we don't want any of
5193 these tags. Strictly, we should be checking each section
5194 individually; This quick check covers for the case where
5195 someone does a /DISCARD/ : { *(*) }. */
5196 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5197 {
5198 bfd_size_type strsize;
5199
5200 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5201 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5202 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5203 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5204 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5205 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5206 bed->s->sizeof_sym))
5207 return FALSE;
5208 }
5209 }
5210
5211 /* The backend must work out the sizes of all the other dynamic
5212 sections. */
5213 if (bed->elf_backend_size_dynamic_sections
5214 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5215 return FALSE;
5216
5217 if (elf_hash_table (info)->dynamic_sections_created)
5218 {
5219 bfd_size_type dynsymcount;
5220 asection *s;
5221 size_t bucketcount = 0;
5222 size_t hash_entry_size;
5223 unsigned int dtagcount;
5224
5225 /* Set up the version definition section. */
5226 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5227 BFD_ASSERT (s != NULL);
5228
5229 /* We may have created additional version definitions if we are
5230 just linking a regular application. */
5231 verdefs = asvinfo.verdefs;
5232
5233 /* Skip anonymous version tag. */
5234 if (verdefs != NULL && verdefs->vernum == 0)
5235 verdefs = verdefs->next;
5236
3e3b46e5 5237 if (verdefs == NULL && !info->create_default_symver)
5a580b3a
AM
5238 _bfd_strip_section_from_output (info, s);
5239 else
5240 {
5241 unsigned int cdefs;
5242 bfd_size_type size;
5243 struct bfd_elf_version_tree *t;
5244 bfd_byte *p;
5245 Elf_Internal_Verdef def;
5246 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5247 struct bfd_link_hash_entry *bh;
5248 struct elf_link_hash_entry *h;
5249 const char *name;
5a580b3a
AM
5250
5251 cdefs = 0;
5252 size = 0;
5253
5254 /* Make space for the base version. */
5255 size += sizeof (Elf_External_Verdef);
5256 size += sizeof (Elf_External_Verdaux);
5257 ++cdefs;
5258
3e3b46e5
PB
5259 /* Make space for the default version. */
5260 if (info->create_default_symver)
5261 {
5262 size += sizeof (Elf_External_Verdef);
5263 ++cdefs;
5264 }
5265
5a580b3a
AM
5266 for (t = verdefs; t != NULL; t = t->next)
5267 {
5268 struct bfd_elf_version_deps *n;
5269
5270 size += sizeof (Elf_External_Verdef);
5271 size += sizeof (Elf_External_Verdaux);
5272 ++cdefs;
5273
5274 for (n = t->deps; n != NULL; n = n->next)
5275 size += sizeof (Elf_External_Verdaux);
5276 }
5277
eea6121a
AM
5278 s->size = size;
5279 s->contents = bfd_alloc (output_bfd, s->size);
5280 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5281 return FALSE;
5282
5283 /* Fill in the version definition section. */
5284
5285 p = s->contents;
5286
5287 def.vd_version = VER_DEF_CURRENT;
5288 def.vd_flags = VER_FLG_BASE;
5289 def.vd_ndx = 1;
5290 def.vd_cnt = 1;
3e3b46e5
PB
5291 if (info->create_default_symver)
5292 {
5293 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5294 def.vd_next = sizeof (Elf_External_Verdef);
5295 }
5296 else
5297 {
5298 def.vd_aux = sizeof (Elf_External_Verdef);
5299 def.vd_next = (sizeof (Elf_External_Verdef)
5300 + sizeof (Elf_External_Verdaux));
5301 }
5a580b3a
AM
5302
5303 if (soname_indx != (bfd_size_type) -1)
5304 {
5305 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5306 soname_indx);
5307 def.vd_hash = bfd_elf_hash (soname);
5308 defaux.vda_name = soname_indx;
3e3b46e5 5309 name = soname;
5a580b3a
AM
5310 }
5311 else
5312 {
5a580b3a
AM
5313 bfd_size_type indx;
5314
5315 name = basename (output_bfd->filename);
5316 def.vd_hash = bfd_elf_hash (name);
5317 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5318 name, FALSE);
5319 if (indx == (bfd_size_type) -1)
5320 return FALSE;
5321 defaux.vda_name = indx;
5322 }
5323 defaux.vda_next = 0;
5324
5325 _bfd_elf_swap_verdef_out (output_bfd, &def,
5326 (Elf_External_Verdef *) p);
5327 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
5328 if (info->create_default_symver)
5329 {
5330 /* Add a symbol representing this version. */
5331 bh = NULL;
5332 if (! (_bfd_generic_link_add_one_symbol
5333 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5334 0, NULL, FALSE,
5335 get_elf_backend_data (dynobj)->collect, &bh)))
5336 return FALSE;
5337 h = (struct elf_link_hash_entry *) bh;
5338 h->non_elf = 0;
5339 h->def_regular = 1;
5340 h->type = STT_OBJECT;
5341 h->verinfo.vertree = NULL;
5342
5343 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5344 return FALSE;
5345
5346 /* Create a duplicate of the base version with the same
5347 aux block, but different flags. */
5348 def.vd_flags = 0;
5349 def.vd_ndx = 2;
5350 def.vd_aux = sizeof (Elf_External_Verdef);
5351 if (verdefs)
5352 def.vd_next = (sizeof (Elf_External_Verdef)
5353 + sizeof (Elf_External_Verdaux));
5354 else
5355 def.vd_next = 0;
5356 _bfd_elf_swap_verdef_out (output_bfd, &def,
5357 (Elf_External_Verdef *) p);
5358 p += sizeof (Elf_External_Verdef);
5359 }
5a580b3a
AM
5360 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5361 (Elf_External_Verdaux *) p);
5362 p += sizeof (Elf_External_Verdaux);
5363
5364 for (t = verdefs; t != NULL; t = t->next)
5365 {
5366 unsigned int cdeps;
5367 struct bfd_elf_version_deps *n;
5a580b3a
AM
5368
5369 cdeps = 0;
5370 for (n = t->deps; n != NULL; n = n->next)
5371 ++cdeps;
5372
5373 /* Add a symbol representing this version. */
5374 bh = NULL;
5375 if (! (_bfd_generic_link_add_one_symbol
5376 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5377 0, NULL, FALSE,
5378 get_elf_backend_data (dynobj)->collect, &bh)))
5379 return FALSE;
5380 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5381 h->non_elf = 0;
5382 h->def_regular = 1;
5a580b3a
AM
5383 h->type = STT_OBJECT;
5384 h->verinfo.vertree = t;
5385
c152c796 5386 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
5387 return FALSE;
5388
5389 def.vd_version = VER_DEF_CURRENT;
5390 def.vd_flags = 0;
5391 if (t->globals.list == NULL
5392 && t->locals.list == NULL
5393 && ! t->used)
5394 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 5395 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
5396 def.vd_cnt = cdeps + 1;
5397 def.vd_hash = bfd_elf_hash (t->name);
5398 def.vd_aux = sizeof (Elf_External_Verdef);
5399 def.vd_next = 0;
5400 if (t->next != NULL)
5401 def.vd_next = (sizeof (Elf_External_Verdef)
5402 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5403
5404 _bfd_elf_swap_verdef_out (output_bfd, &def,
5405 (Elf_External_Verdef *) p);
5406 p += sizeof (Elf_External_Verdef);
5407
5408 defaux.vda_name = h->dynstr_index;
5409 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5410 h->dynstr_index);
5411 defaux.vda_next = 0;
5412 if (t->deps != NULL)
5413 defaux.vda_next = sizeof (Elf_External_Verdaux);
5414 t->name_indx = defaux.vda_name;
5415
5416 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5417 (Elf_External_Verdaux *) p);
5418 p += sizeof (Elf_External_Verdaux);
5419
5420 for (n = t->deps; n != NULL; n = n->next)
5421 {
5422 if (n->version_needed == NULL)
5423 {
5424 /* This can happen if there was an error in the
5425 version script. */
5426 defaux.vda_name = 0;
5427 }
5428 else
5429 {
5430 defaux.vda_name = n->version_needed->name_indx;
5431 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5432 defaux.vda_name);
5433 }
5434 if (n->next == NULL)
5435 defaux.vda_next = 0;
5436 else
5437 defaux.vda_next = sizeof (Elf_External_Verdaux);
5438
5439 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5440 (Elf_External_Verdaux *) p);
5441 p += sizeof (Elf_External_Verdaux);
5442 }
5443 }
5444
5445 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5446 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5447 return FALSE;
5448
5449 elf_tdata (output_bfd)->cverdefs = cdefs;
5450 }
5451
5452 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5453 {
5454 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5455 return FALSE;
5456 }
5457 else if (info->flags & DF_BIND_NOW)
5458 {
5459 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5460 return FALSE;
5461 }
5462
5463 if (info->flags_1)
5464 {
5465 if (info->executable)
5466 info->flags_1 &= ~ (DF_1_INITFIRST
5467 | DF_1_NODELETE
5468 | DF_1_NOOPEN);
5469 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5470 return FALSE;
5471 }
5472
5473 /* Work out the size of the version reference section. */
5474
5475 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5476 BFD_ASSERT (s != NULL);
5477 {
5478 struct elf_find_verdep_info sinfo;
5479
5480 sinfo.output_bfd = output_bfd;
5481 sinfo.info = info;
5482 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5483 if (sinfo.vers == 0)
5484 sinfo.vers = 1;
5485 sinfo.failed = FALSE;
5486
5487 elf_link_hash_traverse (elf_hash_table (info),
5488 _bfd_elf_link_find_version_dependencies,
5489 &sinfo);
5490
5491 if (elf_tdata (output_bfd)->verref == NULL)
5492 _bfd_strip_section_from_output (info, s);
5493 else
5494 {
5495 Elf_Internal_Verneed *t;
5496 unsigned int size;
5497 unsigned int crefs;
5498 bfd_byte *p;
5499
5500 /* Build the version definition section. */
5501 size = 0;
5502 crefs = 0;
5503 for (t = elf_tdata (output_bfd)->verref;
5504 t != NULL;
5505 t = t->vn_nextref)
5506 {
5507 Elf_Internal_Vernaux *a;
5508
5509 size += sizeof (Elf_External_Verneed);
5510 ++crefs;
5511 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5512 size += sizeof (Elf_External_Vernaux);
5513 }
5514
eea6121a
AM
5515 s->size = size;
5516 s->contents = bfd_alloc (output_bfd, s->size);
5a580b3a
AM
5517 if (s->contents == NULL)
5518 return FALSE;
5519
5520 p = s->contents;
5521 for (t = elf_tdata (output_bfd)->verref;
5522 t != NULL;
5523 t = t->vn_nextref)
5524 {
5525 unsigned int caux;
5526 Elf_Internal_Vernaux *a;
5527 bfd_size_type indx;
5528
5529 caux = 0;
5530 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5531 ++caux;
5532
5533 t->vn_version = VER_NEED_CURRENT;
5534 t->vn_cnt = caux;
5535 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5536 elf_dt_name (t->vn_bfd) != NULL
5537 ? elf_dt_name (t->vn_bfd)
5538 : basename (t->vn_bfd->filename),
5539 FALSE);
5540 if (indx == (bfd_size_type) -1)
5541 return FALSE;
5542 t->vn_file = indx;
5543 t->vn_aux = sizeof (Elf_External_Verneed);
5544 if (t->vn_nextref == NULL)
5545 t->vn_next = 0;
5546 else
5547 t->vn_next = (sizeof (Elf_External_Verneed)
5548 + caux * sizeof (Elf_External_Vernaux));
5549
5550 _bfd_elf_swap_verneed_out (output_bfd, t,
5551 (Elf_External_Verneed *) p);
5552 p += sizeof (Elf_External_Verneed);
5553
5554 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5555 {
5556 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5557 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5558 a->vna_nodename, FALSE);
5559 if (indx == (bfd_size_type) -1)
5560 return FALSE;
5561 a->vna_name = indx;
5562 if (a->vna_nextptr == NULL)
5563 a->vna_next = 0;
5564 else
5565 a->vna_next = sizeof (Elf_External_Vernaux);
5566
5567 _bfd_elf_swap_vernaux_out (output_bfd, a,
5568 (Elf_External_Vernaux *) p);
5569 p += sizeof (Elf_External_Vernaux);
5570 }
5571 }
5572
5573 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5574 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5575 return FALSE;
5576
5577 elf_tdata (output_bfd)->cverrefs = crefs;
5578 }
5579 }
5580
5581 /* Assign dynsym indicies. In a shared library we generate a
5582 section symbol for each output section, which come first.
5583 Next come all of the back-end allocated local dynamic syms,
5584 followed by the rest of the global symbols. */
5585
5586 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5587
5588 /* Work out the size of the symbol version section. */
5589 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5590 BFD_ASSERT (s != NULL);
5591 if (dynsymcount == 0
3e3b46e5
PB
5592 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5593 && !info->create_default_symver))
5a580b3a
AM
5594 {
5595 _bfd_strip_section_from_output (info, s);
5596 /* The DYNSYMCOUNT might have changed if we were going to
5597 output a dynamic symbol table entry for S. */
5598 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5599 }
5600 else
5601 {
eea6121a
AM
5602 s->size = dynsymcount * sizeof (Elf_External_Versym);
5603 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
5604 if (s->contents == NULL)
5605 return FALSE;
5606
5607 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5608 return FALSE;
5609 }
5610
5611 /* Set the size of the .dynsym and .hash sections. We counted
5612 the number of dynamic symbols in elf_link_add_object_symbols.
5613 We will build the contents of .dynsym and .hash when we build
5614 the final symbol table, because until then we do not know the
5615 correct value to give the symbols. We built the .dynstr
5616 section as we went along in elf_link_add_object_symbols. */
5617 s = bfd_get_section_by_name (dynobj, ".dynsym");
5618 BFD_ASSERT (s != NULL);
eea6121a
AM
5619 s->size = dynsymcount * bed->s->sizeof_sym;
5620 s->contents = bfd_alloc (output_bfd, s->size);
5621 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5622 return FALSE;
5623
5624 if (dynsymcount != 0)
5625 {
5626 Elf_Internal_Sym isym;
5627
5628 /* The first entry in .dynsym is a dummy symbol. */
5629 isym.st_value = 0;
5630 isym.st_size = 0;
5631 isym.st_name = 0;
5632 isym.st_info = 0;
5633 isym.st_other = 0;
5634 isym.st_shndx = 0;
5635 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5636 }
5637
5638 /* Compute the size of the hashing table. As a side effect this
5639 computes the hash values for all the names we export. */
5640 bucketcount = compute_bucket_count (info);
5641
5642 s = bfd_get_section_by_name (dynobj, ".hash");
5643 BFD_ASSERT (s != NULL);
5644 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
eea6121a
AM
5645 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5646 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
5647 if (s->contents == NULL)
5648 return FALSE;
5649
5650 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5651 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5652 s->contents + hash_entry_size);
5653
5654 elf_hash_table (info)->bucketcount = bucketcount;
5655
5656 s = bfd_get_section_by_name (dynobj, ".dynstr");
5657 BFD_ASSERT (s != NULL);
5658
4ad4eba5 5659 elf_finalize_dynstr (output_bfd, info);
5a580b3a 5660
eea6121a 5661 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
5662
5663 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5664 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5665 return FALSE;
5666 }
5667
5668 return TRUE;
5669}
c152c796
AM
5670
5671/* Final phase of ELF linker. */
5672
5673/* A structure we use to avoid passing large numbers of arguments. */
5674
5675struct elf_final_link_info
5676{
5677 /* General link information. */
5678 struct bfd_link_info *info;
5679 /* Output BFD. */
5680 bfd *output_bfd;
5681 /* Symbol string table. */
5682 struct bfd_strtab_hash *symstrtab;
5683 /* .dynsym section. */
5684 asection *dynsym_sec;
5685 /* .hash section. */
5686 asection *hash_sec;
5687 /* symbol version section (.gnu.version). */
5688 asection *symver_sec;
5689 /* Buffer large enough to hold contents of any section. */
5690 bfd_byte *contents;
5691 /* Buffer large enough to hold external relocs of any section. */
5692 void *external_relocs;
5693 /* Buffer large enough to hold internal relocs of any section. */
5694 Elf_Internal_Rela *internal_relocs;
5695 /* Buffer large enough to hold external local symbols of any input
5696 BFD. */
5697 bfd_byte *external_syms;
5698 /* And a buffer for symbol section indices. */
5699 Elf_External_Sym_Shndx *locsym_shndx;
5700 /* Buffer large enough to hold internal local symbols of any input
5701 BFD. */
5702 Elf_Internal_Sym *internal_syms;
5703 /* Array large enough to hold a symbol index for each local symbol
5704 of any input BFD. */
5705 long *indices;
5706 /* Array large enough to hold a section pointer for each local
5707 symbol of any input BFD. */
5708 asection **sections;
5709 /* Buffer to hold swapped out symbols. */
5710 bfd_byte *symbuf;
5711 /* And one for symbol section indices. */
5712 Elf_External_Sym_Shndx *symshndxbuf;
5713 /* Number of swapped out symbols in buffer. */
5714 size_t symbuf_count;
5715 /* Number of symbols which fit in symbuf. */
5716 size_t symbuf_size;
5717 /* And same for symshndxbuf. */
5718 size_t shndxbuf_size;
5719};
5720
5721/* This struct is used to pass information to elf_link_output_extsym. */
5722
5723struct elf_outext_info
5724{
5725 bfd_boolean failed;
5726 bfd_boolean localsyms;
5727 struct elf_final_link_info *finfo;
5728};
5729
5730/* When performing a relocatable link, the input relocations are
5731 preserved. But, if they reference global symbols, the indices
5732 referenced must be updated. Update all the relocations in
5733 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5734
5735static void
5736elf_link_adjust_relocs (bfd *abfd,
5737 Elf_Internal_Shdr *rel_hdr,
5738 unsigned int count,
5739 struct elf_link_hash_entry **rel_hash)
5740{
5741 unsigned int i;
5742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5743 bfd_byte *erela;
5744 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5745 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5746 bfd_vma r_type_mask;
5747 int r_sym_shift;
5748
5749 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5750 {
5751 swap_in = bed->s->swap_reloc_in;
5752 swap_out = bed->s->swap_reloc_out;
5753 }
5754 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5755 {
5756 swap_in = bed->s->swap_reloca_in;
5757 swap_out = bed->s->swap_reloca_out;
5758 }
5759 else
5760 abort ();
5761
5762 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5763 abort ();
5764
5765 if (bed->s->arch_size == 32)
5766 {
5767 r_type_mask = 0xff;
5768 r_sym_shift = 8;
5769 }
5770 else
5771 {
5772 r_type_mask = 0xffffffff;
5773 r_sym_shift = 32;
5774 }
5775
5776 erela = rel_hdr->contents;
5777 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5778 {
5779 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5780 unsigned int j;
5781
5782 if (*rel_hash == NULL)
5783 continue;
5784
5785 BFD_ASSERT ((*rel_hash)->indx >= 0);
5786
5787 (*swap_in) (abfd, erela, irela);
5788 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5789 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5790 | (irela[j].r_info & r_type_mask));
5791 (*swap_out) (abfd, irela, erela);
5792 }
5793}
5794
5795struct elf_link_sort_rela
5796{
5797 union {
5798 bfd_vma offset;
5799 bfd_vma sym_mask;
5800 } u;
5801 enum elf_reloc_type_class type;
5802 /* We use this as an array of size int_rels_per_ext_rel. */
5803 Elf_Internal_Rela rela[1];
5804};
5805
5806static int
5807elf_link_sort_cmp1 (const void *A, const void *B)
5808{
5809 const struct elf_link_sort_rela *a = A;
5810 const struct elf_link_sort_rela *b = B;
5811 int relativea, relativeb;
5812
5813 relativea = a->type == reloc_class_relative;
5814 relativeb = b->type == reloc_class_relative;
5815
5816 if (relativea < relativeb)
5817 return 1;
5818 if (relativea > relativeb)
5819 return -1;
5820 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5821 return -1;
5822 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5823 return 1;
5824 if (a->rela->r_offset < b->rela->r_offset)
5825 return -1;
5826 if (a->rela->r_offset > b->rela->r_offset)
5827 return 1;
5828 return 0;
5829}
5830
5831static int
5832elf_link_sort_cmp2 (const void *A, const void *B)
5833{
5834 const struct elf_link_sort_rela *a = A;
5835 const struct elf_link_sort_rela *b = B;
5836 int copya, copyb;
5837
5838 if (a->u.offset < b->u.offset)
5839 return -1;
5840 if (a->u.offset > b->u.offset)
5841 return 1;
5842 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5843 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5844 if (copya < copyb)
5845 return -1;
5846 if (copya > copyb)
5847 return 1;
5848 if (a->rela->r_offset < b->rela->r_offset)
5849 return -1;
5850 if (a->rela->r_offset > b->rela->r_offset)
5851 return 1;
5852 return 0;
5853}
5854
5855static size_t
5856elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5857{
5858 asection *reldyn;
5859 bfd_size_type count, size;
5860 size_t i, ret, sort_elt, ext_size;
5861 bfd_byte *sort, *s_non_relative, *p;
5862 struct elf_link_sort_rela *sq;
5863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5864 int i2e = bed->s->int_rels_per_ext_rel;
5865 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5866 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5867 struct bfd_link_order *lo;
5868 bfd_vma r_sym_mask;
5869
5870 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
eea6121a 5871 if (reldyn == NULL || reldyn->size == 0)
c152c796
AM
5872 {
5873 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
eea6121a 5874 if (reldyn == NULL || reldyn->size == 0)
c152c796
AM
5875 return 0;
5876 ext_size = bed->s->sizeof_rel;
5877 swap_in = bed->s->swap_reloc_in;
5878 swap_out = bed->s->swap_reloc_out;
5879 }
5880 else
5881 {
5882 ext_size = bed->s->sizeof_rela;
5883 swap_in = bed->s->swap_reloca_in;
5884 swap_out = bed->s->swap_reloca_out;
5885 }
eea6121a 5886 count = reldyn->size / ext_size;
c152c796
AM
5887
5888 size = 0;
5889 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5890 if (lo->type == bfd_indirect_link_order)
5891 {
5892 asection *o = lo->u.indirect.section;
eea6121a 5893 size += o->size;
c152c796
AM
5894 }
5895
eea6121a 5896 if (size != reldyn->size)
c152c796
AM
5897 return 0;
5898
5899 sort_elt = (sizeof (struct elf_link_sort_rela)
5900 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5901 sort = bfd_zmalloc (sort_elt * count);
5902 if (sort == NULL)
5903 {
5904 (*info->callbacks->warning)
5905 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5906 return 0;
5907 }
5908
5909 if (bed->s->arch_size == 32)
5910 r_sym_mask = ~(bfd_vma) 0xff;
5911 else
5912 r_sym_mask = ~(bfd_vma) 0xffffffff;
5913
5914 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5915 if (lo->type == bfd_indirect_link_order)
5916 {
5917 bfd_byte *erel, *erelend;
5918 asection *o = lo->u.indirect.section;
5919
1da212d6
AM
5920 if (o->contents == NULL && o->size != 0)
5921 {
5922 /* This is a reloc section that is being handled as a normal
5923 section. See bfd_section_from_shdr. We can't combine
5924 relocs in this case. */
5925 free (sort);
5926 return 0;
5927 }
c152c796 5928 erel = o->contents;
eea6121a 5929 erelend = o->contents + o->size;
c152c796
AM
5930 p = sort + o->output_offset / ext_size * sort_elt;
5931 while (erel < erelend)
5932 {
5933 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5934 (*swap_in) (abfd, erel, s->rela);
5935 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5936 s->u.sym_mask = r_sym_mask;
5937 p += sort_elt;
5938 erel += ext_size;
5939 }
5940 }
5941
5942 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5943
5944 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5945 {
5946 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5947 if (s->type != reloc_class_relative)
5948 break;
5949 }
5950 ret = i;
5951 s_non_relative = p;
5952
5953 sq = (struct elf_link_sort_rela *) s_non_relative;
5954 for (; i < count; i++, p += sort_elt)
5955 {
5956 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5957 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5958 sq = sp;
5959 sp->u.offset = sq->rela->r_offset;
5960 }
5961
5962 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5963
5964 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5965 if (lo->type == bfd_indirect_link_order)
5966 {
5967 bfd_byte *erel, *erelend;
5968 asection *o = lo->u.indirect.section;
5969
5970 erel = o->contents;
eea6121a 5971 erelend = o->contents + o->size;
c152c796
AM
5972 p = sort + o->output_offset / ext_size * sort_elt;
5973 while (erel < erelend)
5974 {
5975 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5976 (*swap_out) (abfd, s->rela, erel);
5977 p += sort_elt;
5978 erel += ext_size;
5979 }
5980 }
5981
5982 free (sort);
5983 *psec = reldyn;
5984 return ret;
5985}
5986
5987/* Flush the output symbols to the file. */
5988
5989static bfd_boolean
5990elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5991 const struct elf_backend_data *bed)
5992{
5993 if (finfo->symbuf_count > 0)
5994 {
5995 Elf_Internal_Shdr *hdr;
5996 file_ptr pos;
5997 bfd_size_type amt;
5998
5999 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6000 pos = hdr->sh_offset + hdr->sh_size;
6001 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6002 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6003 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6004 return FALSE;
6005
6006 hdr->sh_size += amt;
6007 finfo->symbuf_count = 0;
6008 }
6009
6010 return TRUE;
6011}
6012
6013/* Add a symbol to the output symbol table. */
6014
6015static bfd_boolean
6016elf_link_output_sym (struct elf_final_link_info *finfo,
6017 const char *name,
6018 Elf_Internal_Sym *elfsym,
6019 asection *input_sec,
6020 struct elf_link_hash_entry *h)
6021{
6022 bfd_byte *dest;
6023 Elf_External_Sym_Shndx *destshndx;
6024 bfd_boolean (*output_symbol_hook)
6025 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6026 struct elf_link_hash_entry *);
6027 const struct elf_backend_data *bed;
6028
6029 bed = get_elf_backend_data (finfo->output_bfd);
6030 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6031 if (output_symbol_hook != NULL)
6032 {
6033 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6034 return FALSE;
6035 }
6036
6037 if (name == NULL || *name == '\0')
6038 elfsym->st_name = 0;
6039 else if (input_sec->flags & SEC_EXCLUDE)
6040 elfsym->st_name = 0;
6041 else
6042 {
6043 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6044 name, TRUE, FALSE);
6045 if (elfsym->st_name == (unsigned long) -1)
6046 return FALSE;
6047 }
6048
6049 if (finfo->symbuf_count >= finfo->symbuf_size)
6050 {
6051 if (! elf_link_flush_output_syms (finfo, bed))
6052 return FALSE;
6053 }
6054
6055 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6056 destshndx = finfo->symshndxbuf;
6057 if (destshndx != NULL)
6058 {
6059 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6060 {
6061 bfd_size_type amt;
6062
6063 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6064 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6065 if (destshndx == NULL)
6066 return FALSE;
6067 memset ((char *) destshndx + amt, 0, amt);
6068 finfo->shndxbuf_size *= 2;
6069 }
6070 destshndx += bfd_get_symcount (finfo->output_bfd);
6071 }
6072
6073 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6074 finfo->symbuf_count += 1;
6075 bfd_get_symcount (finfo->output_bfd) += 1;
6076
6077 return TRUE;
6078}
6079
6080/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6081 allowing an unsatisfied unversioned symbol in the DSO to match a
6082 versioned symbol that would normally require an explicit version.
6083 We also handle the case that a DSO references a hidden symbol
6084 which may be satisfied by a versioned symbol in another DSO. */
6085
6086static bfd_boolean
6087elf_link_check_versioned_symbol (struct bfd_link_info *info,
6088 const struct elf_backend_data *bed,
6089 struct elf_link_hash_entry *h)
6090{
6091 bfd *abfd;
6092 struct elf_link_loaded_list *loaded;
6093
6094 if (!is_elf_hash_table (info->hash))
6095 return FALSE;
6096
6097 switch (h->root.type)
6098 {
6099 default:
6100 abfd = NULL;
6101 break;
6102
6103 case bfd_link_hash_undefined:
6104 case bfd_link_hash_undefweak:
6105 abfd = h->root.u.undef.abfd;
6106 if ((abfd->flags & DYNAMIC) == 0
e56f61be 6107 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
6108 return FALSE;
6109 break;
6110
6111 case bfd_link_hash_defined:
6112 case bfd_link_hash_defweak:
6113 abfd = h->root.u.def.section->owner;
6114 break;
6115
6116 case bfd_link_hash_common:
6117 abfd = h->root.u.c.p->section->owner;
6118 break;
6119 }
6120 BFD_ASSERT (abfd != NULL);
6121
6122 for (loaded = elf_hash_table (info)->loaded;
6123 loaded != NULL;
6124 loaded = loaded->next)
6125 {
6126 bfd *input;
6127 Elf_Internal_Shdr *hdr;
6128 bfd_size_type symcount;
6129 bfd_size_type extsymcount;
6130 bfd_size_type extsymoff;
6131 Elf_Internal_Shdr *versymhdr;
6132 Elf_Internal_Sym *isym;
6133 Elf_Internal_Sym *isymend;
6134 Elf_Internal_Sym *isymbuf;
6135 Elf_External_Versym *ever;
6136 Elf_External_Versym *extversym;
6137
6138 input = loaded->abfd;
6139
6140 /* We check each DSO for a possible hidden versioned definition. */
6141 if (input == abfd
6142 || (input->flags & DYNAMIC) == 0
6143 || elf_dynversym (input) == 0)
6144 continue;
6145
6146 hdr = &elf_tdata (input)->dynsymtab_hdr;
6147
6148 symcount = hdr->sh_size / bed->s->sizeof_sym;
6149 if (elf_bad_symtab (input))
6150 {
6151 extsymcount = symcount;
6152 extsymoff = 0;
6153 }
6154 else
6155 {
6156 extsymcount = symcount - hdr->sh_info;
6157 extsymoff = hdr->sh_info;
6158 }
6159
6160 if (extsymcount == 0)
6161 continue;
6162
6163 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6164 NULL, NULL, NULL);
6165 if (isymbuf == NULL)
6166 return FALSE;
6167
6168 /* Read in any version definitions. */
6169 versymhdr = &elf_tdata (input)->dynversym_hdr;
6170 extversym = bfd_malloc (versymhdr->sh_size);
6171 if (extversym == NULL)
6172 goto error_ret;
6173
6174 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6175 || (bfd_bread (extversym, versymhdr->sh_size, input)
6176 != versymhdr->sh_size))
6177 {
6178 free (extversym);
6179 error_ret:
6180 free (isymbuf);
6181 return FALSE;
6182 }
6183
6184 ever = extversym + extsymoff;
6185 isymend = isymbuf + extsymcount;
6186 for (isym = isymbuf; isym < isymend; isym++, ever++)
6187 {
6188 const char *name;
6189 Elf_Internal_Versym iver;
6190 unsigned short version_index;
6191
6192 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6193 || isym->st_shndx == SHN_UNDEF)
6194 continue;
6195
6196 name = bfd_elf_string_from_elf_section (input,
6197 hdr->sh_link,
6198 isym->st_name);
6199 if (strcmp (name, h->root.root.string) != 0)
6200 continue;
6201
6202 _bfd_elf_swap_versym_in (input, ever, &iver);
6203
6204 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6205 {
6206 /* If we have a non-hidden versioned sym, then it should
6207 have provided a definition for the undefined sym. */
6208 abort ();
6209 }
6210
6211 version_index = iver.vs_vers & VERSYM_VERSION;
6212 if (version_index == 1 || version_index == 2)
6213 {
6214 /* This is the base or first version. We can use it. */
6215 free (extversym);
6216 free (isymbuf);
6217 return TRUE;
6218 }
6219 }
6220
6221 free (extversym);
6222 free (isymbuf);
6223 }
6224
6225 return FALSE;
6226}
6227
6228/* Add an external symbol to the symbol table. This is called from
6229 the hash table traversal routine. When generating a shared object,
6230 we go through the symbol table twice. The first time we output
6231 anything that might have been forced to local scope in a version
6232 script. The second time we output the symbols that are still
6233 global symbols. */
6234
6235static bfd_boolean
6236elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6237{
6238 struct elf_outext_info *eoinfo = data;
6239 struct elf_final_link_info *finfo = eoinfo->finfo;
6240 bfd_boolean strip;
6241 Elf_Internal_Sym sym;
6242 asection *input_sec;
6243 const struct elf_backend_data *bed;
6244
6245 if (h->root.type == bfd_link_hash_warning)
6246 {
6247 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6248 if (h->root.type == bfd_link_hash_new)
6249 return TRUE;
6250 }
6251
6252 /* Decide whether to output this symbol in this pass. */
6253 if (eoinfo->localsyms)
6254 {
f5385ebf 6255 if (!h->forced_local)
c152c796
AM
6256 return TRUE;
6257 }
6258 else
6259 {
f5385ebf 6260 if (h->forced_local)
c152c796
AM
6261 return TRUE;
6262 }
6263
6264 bed = get_elf_backend_data (finfo->output_bfd);
6265
6266 /* If we have an undefined symbol reference here then it must have
6267 come from a shared library that is being linked in. (Undefined
6268 references in regular files have already been handled). If we
6269 are reporting errors for this situation then do so now. */
6270 if (h->root.type == bfd_link_hash_undefined
f5385ebf
AM
6271 && h->ref_dynamic
6272 && !h->ref_regular
c152c796
AM
6273 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6274 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6275 {
6276 if (! ((*finfo->info->callbacks->undefined_symbol)
6277 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6278 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6279 {
6280 eoinfo->failed = TRUE;
6281 return FALSE;
6282 }
6283 }
6284
6285 /* We should also warn if a forced local symbol is referenced from
6286 shared libraries. */
6287 if (! finfo->info->relocatable
6288 && (! finfo->info->shared)
f5385ebf
AM
6289 && h->forced_local
6290 && h->ref_dynamic
6291 && !h->dynamic_def
6292 && !h->dynamic_weak
c152c796
AM
6293 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6294 {
6295 (*_bfd_error_handler)
d003868e
AM
6296 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6297 finfo->output_bfd, h->root.u.def.section->owner,
c152c796
AM
6298 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6299 ? "internal"
6300 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
d003868e
AM
6301 ? "hidden" : "local",
6302 h->root.root.string);
c152c796
AM
6303 eoinfo->failed = TRUE;
6304 return FALSE;
6305 }
6306
6307 /* We don't want to output symbols that have never been mentioned by
6308 a regular file, or that we have been told to strip. However, if
6309 h->indx is set to -2, the symbol is used by a reloc and we must
6310 output it. */
6311 if (h->indx == -2)
6312 strip = FALSE;
f5385ebf 6313 else if ((h->def_dynamic
77cfaee6
AM
6314 || h->ref_dynamic
6315 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
6316 && !h->def_regular
6317 && !h->ref_regular)
c152c796
AM
6318 strip = TRUE;
6319 else if (finfo->info->strip == strip_all)
6320 strip = TRUE;
6321 else if (finfo->info->strip == strip_some
6322 && bfd_hash_lookup (finfo->info->keep_hash,
6323 h->root.root.string, FALSE, FALSE) == NULL)
6324 strip = TRUE;
6325 else if (finfo->info->strip_discarded
6326 && (h->root.type == bfd_link_hash_defined
6327 || h->root.type == bfd_link_hash_defweak)
6328 && elf_discarded_section (h->root.u.def.section))
6329 strip = TRUE;
6330 else
6331 strip = FALSE;
6332
6333 /* If we're stripping it, and it's not a dynamic symbol, there's
6334 nothing else to do unless it is a forced local symbol. */
6335 if (strip
6336 && h->dynindx == -1
f5385ebf 6337 && !h->forced_local)
c152c796
AM
6338 return TRUE;
6339
6340 sym.st_value = 0;
6341 sym.st_size = h->size;
6342 sym.st_other = h->other;
f5385ebf 6343 if (h->forced_local)
c152c796
AM
6344 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6345 else if (h->root.type == bfd_link_hash_undefweak
6346 || h->root.type == bfd_link_hash_defweak)
6347 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6348 else
6349 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6350
6351 switch (h->root.type)
6352 {
6353 default:
6354 case bfd_link_hash_new:
6355 case bfd_link_hash_warning:
6356 abort ();
6357 return FALSE;
6358
6359 case bfd_link_hash_undefined:
6360 case bfd_link_hash_undefweak:
6361 input_sec = bfd_und_section_ptr;
6362 sym.st_shndx = SHN_UNDEF;
6363 break;
6364
6365 case bfd_link_hash_defined:
6366 case bfd_link_hash_defweak:
6367 {
6368 input_sec = h->root.u.def.section;
6369 if (input_sec->output_section != NULL)
6370 {
6371 sym.st_shndx =
6372 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6373 input_sec->output_section);
6374 if (sym.st_shndx == SHN_BAD)
6375 {
6376 (*_bfd_error_handler)
d003868e
AM
6377 (_("%B: could not find output section %A for input section %A"),
6378 finfo->output_bfd, input_sec->output_section, input_sec);
c152c796
AM
6379 eoinfo->failed = TRUE;
6380 return FALSE;
6381 }
6382
6383 /* ELF symbols in relocatable files are section relative,
6384 but in nonrelocatable files they are virtual
6385 addresses. */
6386 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6387 if (! finfo->info->relocatable)
6388 {
6389 sym.st_value += input_sec->output_section->vma;
6390 if (h->type == STT_TLS)
6391 {
6392 /* STT_TLS symbols are relative to PT_TLS segment
6393 base. */
6394 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6395 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6396 }
6397 }
6398 }
6399 else
6400 {
6401 BFD_ASSERT (input_sec->owner == NULL
6402 || (input_sec->owner->flags & DYNAMIC) != 0);
6403 sym.st_shndx = SHN_UNDEF;
6404 input_sec = bfd_und_section_ptr;
6405 }
6406 }
6407 break;
6408
6409 case bfd_link_hash_common:
6410 input_sec = h->root.u.c.p->section;
6411 sym.st_shndx = SHN_COMMON;
6412 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6413 break;
6414
6415 case bfd_link_hash_indirect:
6416 /* These symbols are created by symbol versioning. They point
6417 to the decorated version of the name. For example, if the
6418 symbol foo@@GNU_1.2 is the default, which should be used when
6419 foo is used with no version, then we add an indirect symbol
6420 foo which points to foo@@GNU_1.2. We ignore these symbols,
6421 since the indirected symbol is already in the hash table. */
6422 return TRUE;
6423 }
6424
6425 /* Give the processor backend a chance to tweak the symbol value,
6426 and also to finish up anything that needs to be done for this
6427 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6428 forced local syms when non-shared is due to a historical quirk. */
6429 if ((h->dynindx != -1
f5385ebf 6430 || h->forced_local)
c152c796
AM
6431 && ((finfo->info->shared
6432 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6433 || h->root.type != bfd_link_hash_undefweak))
f5385ebf 6434 || !h->forced_local)
c152c796
AM
6435 && elf_hash_table (finfo->info)->dynamic_sections_created)
6436 {
6437 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6438 (finfo->output_bfd, finfo->info, h, &sym)))
6439 {
6440 eoinfo->failed = TRUE;
6441 return FALSE;
6442 }
6443 }
6444
6445 /* If we are marking the symbol as undefined, and there are no
6446 non-weak references to this symbol from a regular object, then
6447 mark the symbol as weak undefined; if there are non-weak
6448 references, mark the symbol as strong. We can't do this earlier,
6449 because it might not be marked as undefined until the
6450 finish_dynamic_symbol routine gets through with it. */
6451 if (sym.st_shndx == SHN_UNDEF
f5385ebf 6452 && h->ref_regular
c152c796
AM
6453 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6454 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6455 {
6456 int bindtype;
6457
f5385ebf 6458 if (h->ref_regular_nonweak)
c152c796
AM
6459 bindtype = STB_GLOBAL;
6460 else
6461 bindtype = STB_WEAK;
6462 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6463 }
6464
6465 /* If a non-weak symbol with non-default visibility is not defined
6466 locally, it is a fatal error. */
6467 if (! finfo->info->relocatable
6468 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6469 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6470 && h->root.type == bfd_link_hash_undefined
f5385ebf 6471 && !h->def_regular)
c152c796
AM
6472 {
6473 (*_bfd_error_handler)
d003868e
AM
6474 (_("%B: %s symbol `%s' isn't defined"),
6475 finfo->output_bfd,
6476 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6477 ? "protected"
6478 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6479 ? "internal" : "hidden",
6480 h->root.root.string);
c152c796
AM
6481 eoinfo->failed = TRUE;
6482 return FALSE;
6483 }
6484
6485 /* If this symbol should be put in the .dynsym section, then put it
6486 there now. We already know the symbol index. We also fill in
6487 the entry in the .hash section. */
6488 if (h->dynindx != -1
6489 && elf_hash_table (finfo->info)->dynamic_sections_created)
6490 {
6491 size_t bucketcount;
6492 size_t bucket;
6493 size_t hash_entry_size;
6494 bfd_byte *bucketpos;
6495 bfd_vma chain;
6496 bfd_byte *esym;
6497
6498 sym.st_name = h->dynstr_index;
6499 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6500 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6501
6502 bucketcount = elf_hash_table (finfo->info)->bucketcount;
f6e332e6 6503 bucket = h->u.elf_hash_value % bucketcount;
c152c796
AM
6504 hash_entry_size
6505 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6506 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6507 + (bucket + 2) * hash_entry_size);
6508 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6509 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6510 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6511 ((bfd_byte *) finfo->hash_sec->contents
6512 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6513
6514 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6515 {
6516 Elf_Internal_Versym iversym;
6517 Elf_External_Versym *eversym;
6518
f5385ebf 6519 if (!h->def_regular)
c152c796
AM
6520 {
6521 if (h->verinfo.verdef == NULL)
6522 iversym.vs_vers = 0;
6523 else
6524 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6525 }
6526 else
6527 {
6528 if (h->verinfo.vertree == NULL)
6529 iversym.vs_vers = 1;
6530 else
6531 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
6532 if (finfo->info->create_default_symver)
6533 iversym.vs_vers++;
c152c796
AM
6534 }
6535
f5385ebf 6536 if (h->hidden)
c152c796
AM
6537 iversym.vs_vers |= VERSYM_HIDDEN;
6538
6539 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6540 eversym += h->dynindx;
6541 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6542 }
6543 }
6544
6545 /* If we're stripping it, then it was just a dynamic symbol, and
6546 there's nothing else to do. */
6547 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6548 return TRUE;
6549
6550 h->indx = bfd_get_symcount (finfo->output_bfd);
6551
6552 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6553 {
6554 eoinfo->failed = TRUE;
6555 return FALSE;
6556 }
6557
6558 return TRUE;
6559}
6560
cdd3575c
AM
6561/* Return TRUE if special handling is done for relocs in SEC against
6562 symbols defined in discarded sections. */
6563
c152c796
AM
6564static bfd_boolean
6565elf_section_ignore_discarded_relocs (asection *sec)
6566{
6567 const struct elf_backend_data *bed;
6568
cdd3575c
AM
6569 switch (sec->sec_info_type)
6570 {
6571 case ELF_INFO_TYPE_STABS:
6572 case ELF_INFO_TYPE_EH_FRAME:
6573 return TRUE;
6574 default:
6575 break;
6576 }
c152c796
AM
6577
6578 bed = get_elf_backend_data (sec->owner);
6579 if (bed->elf_backend_ignore_discarded_relocs != NULL
6580 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6581 return TRUE;
6582
6583 return FALSE;
6584}
6585
9e66c942
AM
6586enum action_discarded
6587 {
6588 COMPLAIN = 1,
6589 PRETEND = 2
6590 };
6591
6592/* Return a mask saying how ld should treat relocations in SEC against
6593 symbols defined in discarded sections. If this function returns
6594 COMPLAIN set, ld will issue a warning message. If this function
6595 returns PRETEND set, and the discarded section was link-once and the
6596 same size as the kept link-once section, ld will pretend that the
6597 symbol was actually defined in the kept section. Otherwise ld will
6598 zero the reloc (at least that is the intent, but some cooperation by
6599 the target dependent code is needed, particularly for REL targets). */
6600
6601static unsigned int
6602elf_action_discarded (asection *sec)
cdd3575c 6603{
9e66c942
AM
6604 if (sec->flags & SEC_DEBUGGING)
6605 return PRETEND;
cdd3575c
AM
6606
6607 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 6608 return 0;
cdd3575c
AM
6609
6610 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 6611 return 0;
cdd3575c 6612
27b56da8 6613 if (strcmp (".PARISC.unwind", sec->name) == 0)
9e66c942 6614 return 0;
327c1315
AM
6615
6616 if (strcmp (".fixup", sec->name) == 0)
9e66c942 6617 return 0;
27b56da8 6618
9e66c942 6619 return COMPLAIN | PRETEND;
cdd3575c
AM
6620}
6621
3d7f7666
L
6622/* Find a match between a section and a member of a section group. */
6623
6624static asection *
6625match_group_member (asection *sec, asection *group)
6626{
6627 asection *first = elf_next_in_group (group);
6628 asection *s = first;
6629
6630 while (s != NULL)
6631 {
6632 if (bfd_elf_match_symbols_in_sections (s, sec))
6633 return s;
6634
6635 if (s == first)
6636 break;
6637 }
6638
6639 return NULL;
6640}
6641
c152c796
AM
6642/* Link an input file into the linker output file. This function
6643 handles all the sections and relocations of the input file at once.
6644 This is so that we only have to read the local symbols once, and
6645 don't have to keep them in memory. */
6646
6647static bfd_boolean
6648elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6649{
6650 bfd_boolean (*relocate_section)
6651 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6652 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6653 bfd *output_bfd;
6654 Elf_Internal_Shdr *symtab_hdr;
6655 size_t locsymcount;
6656 size_t extsymoff;
6657 Elf_Internal_Sym *isymbuf;
6658 Elf_Internal_Sym *isym;
6659 Elf_Internal_Sym *isymend;
6660 long *pindex;
6661 asection **ppsection;
6662 asection *o;
6663 const struct elf_backend_data *bed;
6664 bfd_boolean emit_relocs;
6665 struct elf_link_hash_entry **sym_hashes;
6666
6667 output_bfd = finfo->output_bfd;
6668 bed = get_elf_backend_data (output_bfd);
6669 relocate_section = bed->elf_backend_relocate_section;
6670
6671 /* If this is a dynamic object, we don't want to do anything here:
6672 we don't want the local symbols, and we don't want the section
6673 contents. */
6674 if ((input_bfd->flags & DYNAMIC) != 0)
6675 return TRUE;
6676
6677 emit_relocs = (finfo->info->relocatable
6678 || finfo->info->emitrelocations
6679 || bed->elf_backend_emit_relocs);
6680
6681 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6682 if (elf_bad_symtab (input_bfd))
6683 {
6684 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6685 extsymoff = 0;
6686 }
6687 else
6688 {
6689 locsymcount = symtab_hdr->sh_info;
6690 extsymoff = symtab_hdr->sh_info;
6691 }
6692
6693 /* Read the local symbols. */
6694 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6695 if (isymbuf == NULL && locsymcount != 0)
6696 {
6697 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6698 finfo->internal_syms,
6699 finfo->external_syms,
6700 finfo->locsym_shndx);
6701 if (isymbuf == NULL)
6702 return FALSE;
6703 }
6704
6705 /* Find local symbol sections and adjust values of symbols in
6706 SEC_MERGE sections. Write out those local symbols we know are
6707 going into the output file. */
6708 isymend = isymbuf + locsymcount;
6709 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6710 isym < isymend;
6711 isym++, pindex++, ppsection++)
6712 {
6713 asection *isec;
6714 const char *name;
6715 Elf_Internal_Sym osym;
6716
6717 *pindex = -1;
6718
6719 if (elf_bad_symtab (input_bfd))
6720 {
6721 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6722 {
6723 *ppsection = NULL;
6724 continue;
6725 }
6726 }
6727
6728 if (isym->st_shndx == SHN_UNDEF)
6729 isec = bfd_und_section_ptr;
6730 else if (isym->st_shndx < SHN_LORESERVE
6731 || isym->st_shndx > SHN_HIRESERVE)
6732 {
6733 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6734 if (isec
6735 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6736 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6737 isym->st_value =
6738 _bfd_merged_section_offset (output_bfd, &isec,
6739 elf_section_data (isec)->sec_info,
753731ee 6740 isym->st_value);
c152c796
AM
6741 }
6742 else if (isym->st_shndx == SHN_ABS)
6743 isec = bfd_abs_section_ptr;
6744 else if (isym->st_shndx == SHN_COMMON)
6745 isec = bfd_com_section_ptr;
6746 else
6747 {
6748 /* Who knows? */
6749 isec = NULL;
6750 }
6751
6752 *ppsection = isec;
6753
6754 /* Don't output the first, undefined, symbol. */
6755 if (ppsection == finfo->sections)
6756 continue;
6757
6758 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6759 {
6760 /* We never output section symbols. Instead, we use the
6761 section symbol of the corresponding section in the output
6762 file. */
6763 continue;
6764 }
6765
6766 /* If we are stripping all symbols, we don't want to output this
6767 one. */
6768 if (finfo->info->strip == strip_all)
6769 continue;
6770
6771 /* If we are discarding all local symbols, we don't want to
6772 output this one. If we are generating a relocatable output
6773 file, then some of the local symbols may be required by
6774 relocs; we output them below as we discover that they are
6775 needed. */
6776 if (finfo->info->discard == discard_all)
6777 continue;
6778
6779 /* If this symbol is defined in a section which we are
6780 discarding, we don't need to keep it, but note that
6781 linker_mark is only reliable for sections that have contents.
6782 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6783 as well as linker_mark. */
6784 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
ccf5f610
PB
6785 && (isec == NULL
6786 || (! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
c152c796
AM
6787 || (! finfo->info->relocatable
6788 && (isec->flags & SEC_EXCLUDE) != 0)))
6789 continue;
6790
6791 /* Get the name of the symbol. */
6792 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6793 isym->st_name);
6794 if (name == NULL)
6795 return FALSE;
6796
6797 /* See if we are discarding symbols with this name. */
6798 if ((finfo->info->strip == strip_some
6799 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6800 == NULL))
6801 || (((finfo->info->discard == discard_sec_merge
6802 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6803 || finfo->info->discard == discard_l)
6804 && bfd_is_local_label_name (input_bfd, name)))
6805 continue;
6806
6807 /* If we get here, we are going to output this symbol. */
6808
6809 osym = *isym;
6810
6811 /* Adjust the section index for the output file. */
6812 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6813 isec->output_section);
6814 if (osym.st_shndx == SHN_BAD)
6815 return FALSE;
6816
6817 *pindex = bfd_get_symcount (output_bfd);
6818
6819 /* ELF symbols in relocatable files are section relative, but
6820 in executable files they are virtual addresses. Note that
6821 this code assumes that all ELF sections have an associated
6822 BFD section with a reasonable value for output_offset; below
6823 we assume that they also have a reasonable value for
6824 output_section. Any special sections must be set up to meet
6825 these requirements. */
6826 osym.st_value += isec->output_offset;
6827 if (! finfo->info->relocatable)
6828 {
6829 osym.st_value += isec->output_section->vma;
6830 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6831 {
6832 /* STT_TLS symbols are relative to PT_TLS segment base. */
6833 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6834 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6835 }
6836 }
6837
6838 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6839 return FALSE;
6840 }
6841
6842 /* Relocate the contents of each section. */
6843 sym_hashes = elf_sym_hashes (input_bfd);
6844 for (o = input_bfd->sections; o != NULL; o = o->next)
6845 {
6846 bfd_byte *contents;
6847
6848 if (! o->linker_mark)
6849 {
6850 /* This section was omitted from the link. */
6851 continue;
6852 }
6853
6854 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 6855 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
6856 continue;
6857
6858 if ((o->flags & SEC_LINKER_CREATED) != 0)
6859 {
6860 /* Section was created by _bfd_elf_link_create_dynamic_sections
6861 or somesuch. */
6862 continue;
6863 }
6864
6865 /* Get the contents of the section. They have been cached by a
6866 relaxation routine. Note that o is a section in an input
6867 file, so the contents field will not have been set by any of
6868 the routines which work on output files. */
6869 if (elf_section_data (o)->this_hdr.contents != NULL)
6870 contents = elf_section_data (o)->this_hdr.contents;
6871 else
6872 {
eea6121a
AM
6873 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6874
c152c796 6875 contents = finfo->contents;
eea6121a 6876 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
c152c796
AM
6877 return FALSE;
6878 }
6879
6880 if ((o->flags & SEC_RELOC) != 0)
6881 {
6882 Elf_Internal_Rela *internal_relocs;
6883 bfd_vma r_type_mask;
6884 int r_sym_shift;
6885
6886 /* Get the swapped relocs. */
6887 internal_relocs
6888 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6889 finfo->internal_relocs, FALSE);
6890 if (internal_relocs == NULL
6891 && o->reloc_count > 0)
6892 return FALSE;
6893
6894 if (bed->s->arch_size == 32)
6895 {
6896 r_type_mask = 0xff;
6897 r_sym_shift = 8;
6898 }
6899 else
6900 {
6901 r_type_mask = 0xffffffff;
6902 r_sym_shift = 32;
6903 }
6904
6905 /* Run through the relocs looking for any against symbols
6906 from discarded sections and section symbols from
6907 removed link-once sections. Complain about relocs
6908 against discarded sections. Zero relocs against removed
6909 link-once sections. Preserve debug information as much
6910 as we can. */
6911 if (!elf_section_ignore_discarded_relocs (o))
6912 {
6913 Elf_Internal_Rela *rel, *relend;
9e66c942 6914 unsigned int action = elf_action_discarded (o);
c152c796
AM
6915
6916 rel = internal_relocs;
6917 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6918 for ( ; rel < relend; rel++)
6919 {
6920 unsigned long r_symndx = rel->r_info >> r_sym_shift;
cdd3575c
AM
6921 asection **ps, *sec;
6922 struct elf_link_hash_entry *h = NULL;
6923 const char *sym_name;
c152c796 6924
ee75fd95
AM
6925 if (r_symndx == STN_UNDEF)
6926 continue;
6927
c152c796
AM
6928 if (r_symndx >= locsymcount
6929 || (elf_bad_symtab (input_bfd)
6930 && finfo->sections[r_symndx] == NULL))
6931 {
c152c796
AM
6932 h = sym_hashes[r_symndx - extsymoff];
6933 while (h->root.type == bfd_link_hash_indirect
6934 || h->root.type == bfd_link_hash_warning)
6935 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6936
cdd3575c
AM
6937 if (h->root.type != bfd_link_hash_defined
6938 && h->root.type != bfd_link_hash_defweak)
6939 continue;
6940
6941 ps = &h->root.u.def.section;
6942 sym_name = h->root.root.string;
c152c796
AM
6943 }
6944 else
6945 {
cdd3575c
AM
6946 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6947 ps = &finfo->sections[r_symndx];
be8dd2ca 6948 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
cdd3575c 6949 }
c152c796 6950
cdd3575c
AM
6951 /* Complain if the definition comes from a
6952 discarded section. */
6953 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6954 {
87e5235d 6955 asection *kept;
3d7f7666 6956
87e5235d 6957 BFD_ASSERT (r_symndx != 0);
9e66c942 6958 if (action & COMPLAIN)
cdd3575c 6959 {
d003868e
AM
6960 (*_bfd_error_handler)
6961 (_("`%s' referenced in section `%A' of %B: "
6962 "defined in discarded section `%A' of %B\n"),
6963 o, input_bfd, sec, sec->owner, sym_name);
cdd3575c
AM
6964 }
6965
87e5235d
AM
6966 /* Try to do the best we can to support buggy old
6967 versions of gcc. If we've warned, or this is
6968 debugging info, pretend that the symbol is
6969 really defined in the kept linkonce section.
6970 FIXME: This is quite broken. Modifying the
6971 symbol here means we will be changing all later
6972 uses of the symbol, not just in this section.
6973 The only thing that makes this half reasonable
6974 is that we warn in non-debug sections, and
6975 debug sections tend to come after other
6976 sections. */
6977 kept = sec->kept_section;
9e66c942 6978 if (kept != NULL && (action & PRETEND))
87e5235d
AM
6979 {
6980 if (elf_sec_group (sec) != NULL)
6981 kept = match_group_member (sec, kept);
6982 if (kept != NULL
6983 && sec->size == kept->size)
6984 {
6985 *ps = kept;
6986 continue;
6987 }
6988 }
6989
cdd3575c
AM
6990 /* Remove the symbol reference from the reloc, but
6991 don't kill the reloc completely. This is so that
6992 a zero value will be written into the section,
6993 which may have non-zero contents put there by the
6994 assembler. Zero in things like an eh_frame fde
6995 pc_begin allows stack unwinders to recognize the
6996 fde as bogus. */
6997 rel->r_info &= r_type_mask;
6998 rel->r_addend = 0;
c152c796
AM
6999 }
7000 }
7001 }
7002
7003 /* Relocate the section by invoking a back end routine.
7004
7005 The back end routine is responsible for adjusting the
7006 section contents as necessary, and (if using Rela relocs
7007 and generating a relocatable output file) adjusting the
7008 reloc addend as necessary.
7009
7010 The back end routine does not have to worry about setting
7011 the reloc address or the reloc symbol index.
7012
7013 The back end routine is given a pointer to the swapped in
7014 internal symbols, and can access the hash table entries
7015 for the external symbols via elf_sym_hashes (input_bfd).
7016
7017 When generating relocatable output, the back end routine
7018 must handle STB_LOCAL/STT_SECTION symbols specially. The
7019 output symbol is going to be a section symbol
7020 corresponding to the output section, which will require
7021 the addend to be adjusted. */
7022
7023 if (! (*relocate_section) (output_bfd, finfo->info,
7024 input_bfd, o, contents,
7025 internal_relocs,
7026 isymbuf,
7027 finfo->sections))
7028 return FALSE;
7029
7030 if (emit_relocs)
7031 {
7032 Elf_Internal_Rela *irela;
7033 Elf_Internal_Rela *irelaend;
7034 bfd_vma last_offset;
7035 struct elf_link_hash_entry **rel_hash;
7036 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7037 unsigned int next_erel;
7038 bfd_boolean (*reloc_emitter)
7039 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
7040 bfd_boolean rela_normal;
7041
7042 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7043 rela_normal = (bed->rela_normal
7044 && (input_rel_hdr->sh_entsize
7045 == bed->s->sizeof_rela));
7046
7047 /* Adjust the reloc addresses and symbol indices. */
7048
7049 irela = internal_relocs;
7050 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7051 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7052 + elf_section_data (o->output_section)->rel_count
7053 + elf_section_data (o->output_section)->rel_count2);
7054 last_offset = o->output_offset;
7055 if (!finfo->info->relocatable)
7056 last_offset += o->output_section->vma;
7057 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7058 {
7059 unsigned long r_symndx;
7060 asection *sec;
7061 Elf_Internal_Sym sym;
7062
7063 if (next_erel == bed->s->int_rels_per_ext_rel)
7064 {
7065 rel_hash++;
7066 next_erel = 0;
7067 }
7068
7069 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7070 finfo->info, o,
7071 irela->r_offset);
7072 if (irela->r_offset >= (bfd_vma) -2)
7073 {
7074 /* This is a reloc for a deleted entry or somesuch.
7075 Turn it into an R_*_NONE reloc, at the same
7076 offset as the last reloc. elf_eh_frame.c and
7077 elf_bfd_discard_info rely on reloc offsets
7078 being ordered. */
7079 irela->r_offset = last_offset;
7080 irela->r_info = 0;
7081 irela->r_addend = 0;
7082 continue;
7083 }
7084
7085 irela->r_offset += o->output_offset;
7086
7087 /* Relocs in an executable have to be virtual addresses. */
7088 if (!finfo->info->relocatable)
7089 irela->r_offset += o->output_section->vma;
7090
7091 last_offset = irela->r_offset;
7092
7093 r_symndx = irela->r_info >> r_sym_shift;
7094 if (r_symndx == STN_UNDEF)
7095 continue;
7096
7097 if (r_symndx >= locsymcount
7098 || (elf_bad_symtab (input_bfd)
7099 && finfo->sections[r_symndx] == NULL))
7100 {
7101 struct elf_link_hash_entry *rh;
7102 unsigned long indx;
7103
7104 /* This is a reloc against a global symbol. We
7105 have not yet output all the local symbols, so
7106 we do not know the symbol index of any global
7107 symbol. We set the rel_hash entry for this
7108 reloc to point to the global hash table entry
7109 for this symbol. The symbol index is then
ee75fd95 7110 set at the end of bfd_elf_final_link. */
c152c796
AM
7111 indx = r_symndx - extsymoff;
7112 rh = elf_sym_hashes (input_bfd)[indx];
7113 while (rh->root.type == bfd_link_hash_indirect
7114 || rh->root.type == bfd_link_hash_warning)
7115 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7116
7117 /* Setting the index to -2 tells
7118 elf_link_output_extsym that this symbol is
7119 used by a reloc. */
7120 BFD_ASSERT (rh->indx < 0);
7121 rh->indx = -2;
7122
7123 *rel_hash = rh;
7124
7125 continue;
7126 }
7127
7128 /* This is a reloc against a local symbol. */
7129
7130 *rel_hash = NULL;
7131 sym = isymbuf[r_symndx];
7132 sec = finfo->sections[r_symndx];
7133 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7134 {
7135 /* I suppose the backend ought to fill in the
7136 section of any STT_SECTION symbol against a
6a8d1586
AM
7137 processor specific section. */
7138 r_symndx = 0;
7139 if (bfd_is_abs_section (sec))
7140 ;
c152c796
AM
7141 else if (sec == NULL || sec->owner == NULL)
7142 {
7143 bfd_set_error (bfd_error_bad_value);
7144 return FALSE;
7145 }
7146 else
7147 {
6a8d1586
AM
7148 asection *osec = sec->output_section;
7149
7150 /* If we have discarded a section, the output
7151 section will be the absolute section. In
7152 case of discarded link-once and discarded
7153 SEC_MERGE sections, use the kept section. */
7154 if (bfd_is_abs_section (osec)
7155 && sec->kept_section != NULL
7156 && sec->kept_section->output_section != NULL)
7157 {
7158 osec = sec->kept_section->output_section;
7159 irela->r_addend -= osec->vma;
7160 }
7161
7162 if (!bfd_is_abs_section (osec))
7163 {
7164 r_symndx = osec->target_index;
7165 BFD_ASSERT (r_symndx != 0);
7166 }
c152c796
AM
7167 }
7168
7169 /* Adjust the addend according to where the
7170 section winds up in the output section. */
7171 if (rela_normal)
7172 irela->r_addend += sec->output_offset;
7173 }
7174 else
7175 {
7176 if (finfo->indices[r_symndx] == -1)
7177 {
7178 unsigned long shlink;
7179 const char *name;
7180 asection *osec;
7181
7182 if (finfo->info->strip == strip_all)
7183 {
7184 /* You can't do ld -r -s. */
7185 bfd_set_error (bfd_error_invalid_operation);
7186 return FALSE;
7187 }
7188
7189 /* This symbol was skipped earlier, but
7190 since it is needed by a reloc, we
7191 must output it now. */
7192 shlink = symtab_hdr->sh_link;
7193 name = (bfd_elf_string_from_elf_section
7194 (input_bfd, shlink, sym.st_name));
7195 if (name == NULL)
7196 return FALSE;
7197
7198 osec = sec->output_section;
7199 sym.st_shndx =
7200 _bfd_elf_section_from_bfd_section (output_bfd,
7201 osec);
7202 if (sym.st_shndx == SHN_BAD)
7203 return FALSE;
7204
7205 sym.st_value += sec->output_offset;
7206 if (! finfo->info->relocatable)
7207 {
7208 sym.st_value += osec->vma;
7209 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7210 {
7211 /* STT_TLS symbols are relative to PT_TLS
7212 segment base. */
7213 BFD_ASSERT (elf_hash_table (finfo->info)
7214 ->tls_sec != NULL);
7215 sym.st_value -= (elf_hash_table (finfo->info)
7216 ->tls_sec->vma);
7217 }
7218 }
7219
7220 finfo->indices[r_symndx]
7221 = bfd_get_symcount (output_bfd);
7222
7223 if (! elf_link_output_sym (finfo, name, &sym, sec,
7224 NULL))
7225 return FALSE;
7226 }
7227
7228 r_symndx = finfo->indices[r_symndx];
7229 }
7230
7231 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7232 | (irela->r_info & r_type_mask));
7233 }
7234
7235 /* Swap out the relocs. */
7236 if (bed->elf_backend_emit_relocs
7237 && !(finfo->info->relocatable
7238 || finfo->info->emitrelocations))
7239 reloc_emitter = bed->elf_backend_emit_relocs;
7240 else
7241 reloc_emitter = _bfd_elf_link_output_relocs;
7242
7243 if (input_rel_hdr->sh_size != 0
7244 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7245 internal_relocs))
7246 return FALSE;
7247
7248 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7249 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7250 {
7251 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7252 * bed->s->int_rels_per_ext_rel);
7253 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7254 internal_relocs))
7255 return FALSE;
7256 }
7257 }
7258 }
7259
7260 /* Write out the modified section contents. */
7261 if (bed->elf_backend_write_section
7262 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7263 {
7264 /* Section written out. */
7265 }
7266 else switch (o->sec_info_type)
7267 {
7268 case ELF_INFO_TYPE_STABS:
7269 if (! (_bfd_write_section_stabs
7270 (output_bfd,
7271 &elf_hash_table (finfo->info)->stab_info,
7272 o, &elf_section_data (o)->sec_info, contents)))
7273 return FALSE;
7274 break;
7275 case ELF_INFO_TYPE_MERGE:
7276 if (! _bfd_write_merged_section (output_bfd, o,
7277 elf_section_data (o)->sec_info))
7278 return FALSE;
7279 break;
7280 case ELF_INFO_TYPE_EH_FRAME:
7281 {
7282 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7283 o, contents))
7284 return FALSE;
7285 }
7286 break;
7287 default:
7288 {
c152c796
AM
7289 if (! (o->flags & SEC_EXCLUDE)
7290 && ! bfd_set_section_contents (output_bfd, o->output_section,
7291 contents,
7292 (file_ptr) o->output_offset,
eea6121a 7293 o->size))
c152c796
AM
7294 return FALSE;
7295 }
7296 break;
7297 }
7298 }
7299
7300 return TRUE;
7301}
7302
7303/* Generate a reloc when linking an ELF file. This is a reloc
7304 requested by the linker, and does come from any input file. This
7305 is used to build constructor and destructor tables when linking
7306 with -Ur. */
7307
7308static bfd_boolean
7309elf_reloc_link_order (bfd *output_bfd,
7310 struct bfd_link_info *info,
7311 asection *output_section,
7312 struct bfd_link_order *link_order)
7313{
7314 reloc_howto_type *howto;
7315 long indx;
7316 bfd_vma offset;
7317 bfd_vma addend;
7318 struct elf_link_hash_entry **rel_hash_ptr;
7319 Elf_Internal_Shdr *rel_hdr;
7320 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7321 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7322 bfd_byte *erel;
7323 unsigned int i;
7324
7325 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7326 if (howto == NULL)
7327 {
7328 bfd_set_error (bfd_error_bad_value);
7329 return FALSE;
7330 }
7331
7332 addend = link_order->u.reloc.p->addend;
7333
7334 /* Figure out the symbol index. */
7335 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7336 + elf_section_data (output_section)->rel_count
7337 + elf_section_data (output_section)->rel_count2);
7338 if (link_order->type == bfd_section_reloc_link_order)
7339 {
7340 indx = link_order->u.reloc.p->u.section->target_index;
7341 BFD_ASSERT (indx != 0);
7342 *rel_hash_ptr = NULL;
7343 }
7344 else
7345 {
7346 struct elf_link_hash_entry *h;
7347
7348 /* Treat a reloc against a defined symbol as though it were
7349 actually against the section. */
7350 h = ((struct elf_link_hash_entry *)
7351 bfd_wrapped_link_hash_lookup (output_bfd, info,
7352 link_order->u.reloc.p->u.name,
7353 FALSE, FALSE, TRUE));
7354 if (h != NULL
7355 && (h->root.type == bfd_link_hash_defined
7356 || h->root.type == bfd_link_hash_defweak))
7357 {
7358 asection *section;
7359
7360 section = h->root.u.def.section;
7361 indx = section->output_section->target_index;
7362 *rel_hash_ptr = NULL;
7363 /* It seems that we ought to add the symbol value to the
7364 addend here, but in practice it has already been added
7365 because it was passed to constructor_callback. */
7366 addend += section->output_section->vma + section->output_offset;
7367 }
7368 else if (h != NULL)
7369 {
7370 /* Setting the index to -2 tells elf_link_output_extsym that
7371 this symbol is used by a reloc. */
7372 h->indx = -2;
7373 *rel_hash_ptr = h;
7374 indx = 0;
7375 }
7376 else
7377 {
7378 if (! ((*info->callbacks->unattached_reloc)
7379 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7380 return FALSE;
7381 indx = 0;
7382 }
7383 }
7384
7385 /* If this is an inplace reloc, we must write the addend into the
7386 object file. */
7387 if (howto->partial_inplace && addend != 0)
7388 {
7389 bfd_size_type size;
7390 bfd_reloc_status_type rstat;
7391 bfd_byte *buf;
7392 bfd_boolean ok;
7393 const char *sym_name;
7394
7395 size = bfd_get_reloc_size (howto);
7396 buf = bfd_zmalloc (size);
7397 if (buf == NULL)
7398 return FALSE;
7399 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7400 switch (rstat)
7401 {
7402 case bfd_reloc_ok:
7403 break;
7404
7405 default:
7406 case bfd_reloc_outofrange:
7407 abort ();
7408
7409 case bfd_reloc_overflow:
7410 if (link_order->type == bfd_section_reloc_link_order)
7411 sym_name = bfd_section_name (output_bfd,
7412 link_order->u.reloc.p->u.section);
7413 else
7414 sym_name = link_order->u.reloc.p->u.name;
7415 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
7416 (info, NULL, sym_name, howto->name, addend, NULL,
7417 NULL, (bfd_vma) 0)))
c152c796
AM
7418 {
7419 free (buf);
7420 return FALSE;
7421 }
7422 break;
7423 }
7424 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7425 link_order->offset, size);
7426 free (buf);
7427 if (! ok)
7428 return FALSE;
7429 }
7430
7431 /* The address of a reloc is relative to the section in a
7432 relocatable file, and is a virtual address in an executable
7433 file. */
7434 offset = link_order->offset;
7435 if (! info->relocatable)
7436 offset += output_section->vma;
7437
7438 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7439 {
7440 irel[i].r_offset = offset;
7441 irel[i].r_info = 0;
7442 irel[i].r_addend = 0;
7443 }
7444 if (bed->s->arch_size == 32)
7445 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7446 else
7447 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7448
7449 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7450 erel = rel_hdr->contents;
7451 if (rel_hdr->sh_type == SHT_REL)
7452 {
7453 erel += (elf_section_data (output_section)->rel_count
7454 * bed->s->sizeof_rel);
7455 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7456 }
7457 else
7458 {
7459 irel[0].r_addend = addend;
7460 erel += (elf_section_data (output_section)->rel_count
7461 * bed->s->sizeof_rela);
7462 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7463 }
7464
7465 ++elf_section_data (output_section)->rel_count;
7466
7467 return TRUE;
7468}
7469
0b52efa6
PB
7470
7471/* Get the output vma of the section pointed to by the sh_link field. */
7472
7473static bfd_vma
7474elf_get_linked_section_vma (struct bfd_link_order *p)
7475{
7476 Elf_Internal_Shdr **elf_shdrp;
7477 asection *s;
7478 int elfsec;
7479
7480 s = p->u.indirect.section;
7481 elf_shdrp = elf_elfsections (s->owner);
7482 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7483 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
7484 /* PR 290:
7485 The Intel C compiler generates SHT_IA_64_UNWIND with
7486 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7487 sh_info fields. Hence we could get the situation
7488 where elfsec is 0. */
7489 if (elfsec == 0)
7490 {
7491 const struct elf_backend_data *bed
7492 = get_elf_backend_data (s->owner);
7493 if (bed->link_order_error_handler)
d003868e
AM
7494 bed->link_order_error_handler
7495 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
7496 return 0;
7497 }
7498 else
7499 {
7500 s = elf_shdrp[elfsec]->bfd_section;
7501 return s->output_section->vma + s->output_offset;
7502 }
0b52efa6
PB
7503}
7504
7505
7506/* Compare two sections based on the locations of the sections they are
7507 linked to. Used by elf_fixup_link_order. */
7508
7509static int
7510compare_link_order (const void * a, const void * b)
7511{
7512 bfd_vma apos;
7513 bfd_vma bpos;
7514
7515 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7516 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7517 if (apos < bpos)
7518 return -1;
7519 return apos > bpos;
7520}
7521
7522
7523/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7524 order as their linked sections. Returns false if this could not be done
7525 because an output section includes both ordered and unordered
7526 sections. Ideally we'd do this in the linker proper. */
7527
7528static bfd_boolean
7529elf_fixup_link_order (bfd *abfd, asection *o)
7530{
7531 int seen_linkorder;
7532 int seen_other;
7533 int n;
7534 struct bfd_link_order *p;
7535 bfd *sub;
7536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7537 int elfsec;
7538 struct bfd_link_order **sections;
7539 asection *s;
7540 bfd_vma offset;
7541
7542 seen_other = 0;
7543 seen_linkorder = 0;
7544 for (p = o->link_order_head; p != NULL; p = p->next)
7545 {
7546 if (p->type == bfd_indirect_link_order
7547 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7548 == bfd_target_elf_flavour)
7549 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7550 {
7551 s = p->u.indirect.section;
7552 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7553 if (elfsec != -1
7554 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7555 seen_linkorder++;
7556 else
7557 seen_other++;
7558 }
7559 else
7560 seen_other++;
7561 }
7562
7563 if (!seen_linkorder)
7564 return TRUE;
7565
7566 if (seen_other && seen_linkorder)
08ccf96b 7567 {
d003868e
AM
7568 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7569 o);
08ccf96b
L
7570 bfd_set_error (bfd_error_bad_value);
7571 return FALSE;
7572 }
0b52efa6
PB
7573
7574 sections = (struct bfd_link_order **)
7575 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7576 seen_linkorder = 0;
7577
7578 for (p = o->link_order_head; p != NULL; p = p->next)
7579 {
7580 sections[seen_linkorder++] = p;
7581 }
7582 /* Sort the input sections in the order of their linked section. */
7583 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7584 compare_link_order);
7585
7586 /* Change the offsets of the sections. */
7587 offset = 0;
7588 for (n = 0; n < seen_linkorder; n++)
7589 {
7590 s = sections[n]->u.indirect.section;
7591 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7592 s->output_offset = offset;
7593 sections[n]->offset = offset;
7594 offset += sections[n]->size;
7595 }
7596
7597 return TRUE;
7598}
7599
7600
c152c796
AM
7601/* Do the final step of an ELF link. */
7602
7603bfd_boolean
7604bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7605{
7606 bfd_boolean dynamic;
7607 bfd_boolean emit_relocs;
7608 bfd *dynobj;
7609 struct elf_final_link_info finfo;
7610 register asection *o;
7611 register struct bfd_link_order *p;
7612 register bfd *sub;
7613 bfd_size_type max_contents_size;
7614 bfd_size_type max_external_reloc_size;
7615 bfd_size_type max_internal_reloc_count;
7616 bfd_size_type max_sym_count;
7617 bfd_size_type max_sym_shndx_count;
7618 file_ptr off;
7619 Elf_Internal_Sym elfsym;
7620 unsigned int i;
7621 Elf_Internal_Shdr *symtab_hdr;
7622 Elf_Internal_Shdr *symtab_shndx_hdr;
7623 Elf_Internal_Shdr *symstrtab_hdr;
7624 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7625 struct elf_outext_info eoinfo;
7626 bfd_boolean merged;
7627 size_t relativecount = 0;
7628 asection *reldyn = 0;
7629 bfd_size_type amt;
7630
7631 if (! is_elf_hash_table (info->hash))
7632 return FALSE;
7633
7634 if (info->shared)
7635 abfd->flags |= DYNAMIC;
7636
7637 dynamic = elf_hash_table (info)->dynamic_sections_created;
7638 dynobj = elf_hash_table (info)->dynobj;
7639
7640 emit_relocs = (info->relocatable
7641 || info->emitrelocations
7642 || bed->elf_backend_emit_relocs);
7643
7644 finfo.info = info;
7645 finfo.output_bfd = abfd;
7646 finfo.symstrtab = _bfd_elf_stringtab_init ();
7647 if (finfo.symstrtab == NULL)
7648 return FALSE;
7649
7650 if (! dynamic)
7651 {
7652 finfo.dynsym_sec = NULL;
7653 finfo.hash_sec = NULL;
7654 finfo.symver_sec = NULL;
7655 }
7656 else
7657 {
7658 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7659 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7660 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7661 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7662 /* Note that it is OK if symver_sec is NULL. */
7663 }
7664
7665 finfo.contents = NULL;
7666 finfo.external_relocs = NULL;
7667 finfo.internal_relocs = NULL;
7668 finfo.external_syms = NULL;
7669 finfo.locsym_shndx = NULL;
7670 finfo.internal_syms = NULL;
7671 finfo.indices = NULL;
7672 finfo.sections = NULL;
7673 finfo.symbuf = NULL;
7674 finfo.symshndxbuf = NULL;
7675 finfo.symbuf_count = 0;
7676 finfo.shndxbuf_size = 0;
7677
7678 /* Count up the number of relocations we will output for each output
7679 section, so that we know the sizes of the reloc sections. We
7680 also figure out some maximum sizes. */
7681 max_contents_size = 0;
7682 max_external_reloc_size = 0;
7683 max_internal_reloc_count = 0;
7684 max_sym_count = 0;
7685 max_sym_shndx_count = 0;
7686 merged = FALSE;
7687 for (o = abfd->sections; o != NULL; o = o->next)
7688 {
7689 struct bfd_elf_section_data *esdo = elf_section_data (o);
7690 o->reloc_count = 0;
7691
7692 for (p = o->link_order_head; p != NULL; p = p->next)
7693 {
7694 unsigned int reloc_count = 0;
7695 struct bfd_elf_section_data *esdi = NULL;
7696 unsigned int *rel_count1;
7697
7698 if (p->type == bfd_section_reloc_link_order
7699 || p->type == bfd_symbol_reloc_link_order)
7700 reloc_count = 1;
7701 else if (p->type == bfd_indirect_link_order)
7702 {
7703 asection *sec;
7704
7705 sec = p->u.indirect.section;
7706 esdi = elf_section_data (sec);
7707
7708 /* Mark all sections which are to be included in the
7709 link. This will normally be every section. We need
7710 to do this so that we can identify any sections which
7711 the linker has decided to not include. */
7712 sec->linker_mark = TRUE;
7713
7714 if (sec->flags & SEC_MERGE)
7715 merged = TRUE;
7716
7717 if (info->relocatable || info->emitrelocations)
7718 reloc_count = sec->reloc_count;
7719 else if (bed->elf_backend_count_relocs)
7720 {
7721 Elf_Internal_Rela * relocs;
7722
7723 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7724 info->keep_memory);
7725
7726 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7727
7728 if (elf_section_data (o)->relocs != relocs)
7729 free (relocs);
7730 }
7731
eea6121a
AM
7732 if (sec->rawsize > max_contents_size)
7733 max_contents_size = sec->rawsize;
7734 if (sec->size > max_contents_size)
7735 max_contents_size = sec->size;
c152c796
AM
7736
7737 /* We are interested in just local symbols, not all
7738 symbols. */
7739 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7740 && (sec->owner->flags & DYNAMIC) == 0)
7741 {
7742 size_t sym_count;
7743
7744 if (elf_bad_symtab (sec->owner))
7745 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7746 / bed->s->sizeof_sym);
7747 else
7748 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7749
7750 if (sym_count > max_sym_count)
7751 max_sym_count = sym_count;
7752
7753 if (sym_count > max_sym_shndx_count
7754 && elf_symtab_shndx (sec->owner) != 0)
7755 max_sym_shndx_count = sym_count;
7756
7757 if ((sec->flags & SEC_RELOC) != 0)
7758 {
7759 size_t ext_size;
7760
7761 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7762 if (ext_size > max_external_reloc_size)
7763 max_external_reloc_size = ext_size;
7764 if (sec->reloc_count > max_internal_reloc_count)
7765 max_internal_reloc_count = sec->reloc_count;
7766 }
7767 }
7768 }
7769
7770 if (reloc_count == 0)
7771 continue;
7772
7773 o->reloc_count += reloc_count;
7774
7775 /* MIPS may have a mix of REL and RELA relocs on sections.
7776 To support this curious ABI we keep reloc counts in
7777 elf_section_data too. We must be careful to add the
7778 relocations from the input section to the right output
7779 count. FIXME: Get rid of one count. We have
7780 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7781 rel_count1 = &esdo->rel_count;
7782 if (esdi != NULL)
7783 {
7784 bfd_boolean same_size;
7785 bfd_size_type entsize1;
7786
7787 entsize1 = esdi->rel_hdr.sh_entsize;
7788 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7789 || entsize1 == bed->s->sizeof_rela);
7790 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7791
7792 if (!same_size)
7793 rel_count1 = &esdo->rel_count2;
7794
7795 if (esdi->rel_hdr2 != NULL)
7796 {
7797 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7798 unsigned int alt_count;
7799 unsigned int *rel_count2;
7800
7801 BFD_ASSERT (entsize2 != entsize1
7802 && (entsize2 == bed->s->sizeof_rel
7803 || entsize2 == bed->s->sizeof_rela));
7804
7805 rel_count2 = &esdo->rel_count2;
7806 if (!same_size)
7807 rel_count2 = &esdo->rel_count;
7808
7809 /* The following is probably too simplistic if the
7810 backend counts output relocs unusually. */
7811 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7812 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7813 *rel_count2 += alt_count;
7814 reloc_count -= alt_count;
7815 }
7816 }
7817 *rel_count1 += reloc_count;
7818 }
7819
7820 if (o->reloc_count > 0)
7821 o->flags |= SEC_RELOC;
7822 else
7823 {
7824 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7825 set it (this is probably a bug) and if it is set
7826 assign_section_numbers will create a reloc section. */
7827 o->flags &=~ SEC_RELOC;
7828 }
7829
7830 /* If the SEC_ALLOC flag is not set, force the section VMA to
7831 zero. This is done in elf_fake_sections as well, but forcing
7832 the VMA to 0 here will ensure that relocs against these
7833 sections are handled correctly. */
7834 if ((o->flags & SEC_ALLOC) == 0
7835 && ! o->user_set_vma)
7836 o->vma = 0;
7837 }
7838
7839 if (! info->relocatable && merged)
7840 elf_link_hash_traverse (elf_hash_table (info),
7841 _bfd_elf_link_sec_merge_syms, abfd);
7842
7843 /* Figure out the file positions for everything but the symbol table
7844 and the relocs. We set symcount to force assign_section_numbers
7845 to create a symbol table. */
7846 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7847 BFD_ASSERT (! abfd->output_has_begun);
7848 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7849 goto error_return;
7850
ee75fd95 7851 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
7852 for (o = abfd->sections; o != NULL; o = o->next)
7853 {
7854 if ((o->flags & SEC_RELOC) != 0)
7855 {
7856 if (!(_bfd_elf_link_size_reloc_section
7857 (abfd, &elf_section_data (o)->rel_hdr, o)))
7858 goto error_return;
7859
7860 if (elf_section_data (o)->rel_hdr2
7861 && !(_bfd_elf_link_size_reloc_section
7862 (abfd, elf_section_data (o)->rel_hdr2, o)))
7863 goto error_return;
7864 }
7865
7866 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7867 to count upwards while actually outputting the relocations. */
7868 elf_section_data (o)->rel_count = 0;
7869 elf_section_data (o)->rel_count2 = 0;
7870 }
7871
7872 _bfd_elf_assign_file_positions_for_relocs (abfd);
7873
7874 /* We have now assigned file positions for all the sections except
7875 .symtab and .strtab. We start the .symtab section at the current
7876 file position, and write directly to it. We build the .strtab
7877 section in memory. */
7878 bfd_get_symcount (abfd) = 0;
7879 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7880 /* sh_name is set in prep_headers. */
7881 symtab_hdr->sh_type = SHT_SYMTAB;
7882 /* sh_flags, sh_addr and sh_size all start off zero. */
7883 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7884 /* sh_link is set in assign_section_numbers. */
7885 /* sh_info is set below. */
7886 /* sh_offset is set just below. */
7887 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7888
7889 off = elf_tdata (abfd)->next_file_pos;
7890 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7891
7892 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7893 incorrect. We do not yet know the size of the .symtab section.
7894 We correct next_file_pos below, after we do know the size. */
7895
7896 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7897 continuously seeking to the right position in the file. */
7898 if (! info->keep_memory || max_sym_count < 20)
7899 finfo.symbuf_size = 20;
7900 else
7901 finfo.symbuf_size = max_sym_count;
7902 amt = finfo.symbuf_size;
7903 amt *= bed->s->sizeof_sym;
7904 finfo.symbuf = bfd_malloc (amt);
7905 if (finfo.symbuf == NULL)
7906 goto error_return;
7907 if (elf_numsections (abfd) > SHN_LORESERVE)
7908 {
7909 /* Wild guess at number of output symbols. realloc'd as needed. */
7910 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7911 finfo.shndxbuf_size = amt;
7912 amt *= sizeof (Elf_External_Sym_Shndx);
7913 finfo.symshndxbuf = bfd_zmalloc (amt);
7914 if (finfo.symshndxbuf == NULL)
7915 goto error_return;
7916 }
7917
7918 /* Start writing out the symbol table. The first symbol is always a
7919 dummy symbol. */
7920 if (info->strip != strip_all
7921 || emit_relocs)
7922 {
7923 elfsym.st_value = 0;
7924 elfsym.st_size = 0;
7925 elfsym.st_info = 0;
7926 elfsym.st_other = 0;
7927 elfsym.st_shndx = SHN_UNDEF;
7928 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7929 NULL))
7930 goto error_return;
7931 }
7932
c152c796
AM
7933 /* Output a symbol for each section. We output these even if we are
7934 discarding local symbols, since they are used for relocs. These
7935 symbols have no names. We store the index of each one in the
7936 index field of the section, so that we can find it again when
7937 outputting relocs. */
7938 if (info->strip != strip_all
7939 || emit_relocs)
7940 {
7941 elfsym.st_size = 0;
7942 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7943 elfsym.st_other = 0;
7944 for (i = 1; i < elf_numsections (abfd); i++)
7945 {
7946 o = bfd_section_from_elf_index (abfd, i);
7947 if (o != NULL)
7948 o->target_index = bfd_get_symcount (abfd);
7949 elfsym.st_shndx = i;
7950 if (info->relocatable || o == NULL)
7951 elfsym.st_value = 0;
7952 else
7953 elfsym.st_value = o->vma;
7954 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7955 goto error_return;
7956 if (i == SHN_LORESERVE - 1)
7957 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7958 }
7959 }
7960
7961 /* Allocate some memory to hold information read in from the input
7962 files. */
7963 if (max_contents_size != 0)
7964 {
7965 finfo.contents = bfd_malloc (max_contents_size);
7966 if (finfo.contents == NULL)
7967 goto error_return;
7968 }
7969
7970 if (max_external_reloc_size != 0)
7971 {
7972 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7973 if (finfo.external_relocs == NULL)
7974 goto error_return;
7975 }
7976
7977 if (max_internal_reloc_count != 0)
7978 {
7979 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7980 amt *= sizeof (Elf_Internal_Rela);
7981 finfo.internal_relocs = bfd_malloc (amt);
7982 if (finfo.internal_relocs == NULL)
7983 goto error_return;
7984 }
7985
7986 if (max_sym_count != 0)
7987 {
7988 amt = max_sym_count * bed->s->sizeof_sym;
7989 finfo.external_syms = bfd_malloc (amt);
7990 if (finfo.external_syms == NULL)
7991 goto error_return;
7992
7993 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7994 finfo.internal_syms = bfd_malloc (amt);
7995 if (finfo.internal_syms == NULL)
7996 goto error_return;
7997
7998 amt = max_sym_count * sizeof (long);
7999 finfo.indices = bfd_malloc (amt);
8000 if (finfo.indices == NULL)
8001 goto error_return;
8002
8003 amt = max_sym_count * sizeof (asection *);
8004 finfo.sections = bfd_malloc (amt);
8005 if (finfo.sections == NULL)
8006 goto error_return;
8007 }
8008
8009 if (max_sym_shndx_count != 0)
8010 {
8011 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8012 finfo.locsym_shndx = bfd_malloc (amt);
8013 if (finfo.locsym_shndx == NULL)
8014 goto error_return;
8015 }
8016
8017 if (elf_hash_table (info)->tls_sec)
8018 {
8019 bfd_vma base, end = 0;
8020 asection *sec;
8021
8022 for (sec = elf_hash_table (info)->tls_sec;
8023 sec && (sec->flags & SEC_THREAD_LOCAL);
8024 sec = sec->next)
8025 {
eea6121a 8026 bfd_vma size = sec->size;
c152c796
AM
8027
8028 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8029 {
8030 struct bfd_link_order *o;
8031
8032 for (o = sec->link_order_head; o != NULL; o = o->next)
8033 if (size < o->offset + o->size)
8034 size = o->offset + o->size;
8035 }
8036 end = sec->vma + size;
8037 }
8038 base = elf_hash_table (info)->tls_sec->vma;
8039 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8040 elf_hash_table (info)->tls_size = end - base;
8041 }
8042
0b52efa6
PB
8043 /* Reorder SHF_LINK_ORDER sections. */
8044 for (o = abfd->sections; o != NULL; o = o->next)
8045 {
8046 if (!elf_fixup_link_order (abfd, o))
8047 return FALSE;
8048 }
8049
c152c796
AM
8050 /* Since ELF permits relocations to be against local symbols, we
8051 must have the local symbols available when we do the relocations.
8052 Since we would rather only read the local symbols once, and we
8053 would rather not keep them in memory, we handle all the
8054 relocations for a single input file at the same time.
8055
8056 Unfortunately, there is no way to know the total number of local
8057 symbols until we have seen all of them, and the local symbol
8058 indices precede the global symbol indices. This means that when
8059 we are generating relocatable output, and we see a reloc against
8060 a global symbol, we can not know the symbol index until we have
8061 finished examining all the local symbols to see which ones we are
8062 going to output. To deal with this, we keep the relocations in
8063 memory, and don't output them until the end of the link. This is
8064 an unfortunate waste of memory, but I don't see a good way around
8065 it. Fortunately, it only happens when performing a relocatable
8066 link, which is not the common case. FIXME: If keep_memory is set
8067 we could write the relocs out and then read them again; I don't
8068 know how bad the memory loss will be. */
8069
8070 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8071 sub->output_has_begun = FALSE;
8072 for (o = abfd->sections; o != NULL; o = o->next)
8073 {
8074 for (p = o->link_order_head; p != NULL; p = p->next)
8075 {
8076 if (p->type == bfd_indirect_link_order
8077 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8078 == bfd_target_elf_flavour)
8079 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8080 {
8081 if (! sub->output_has_begun)
8082 {
8083 if (! elf_link_input_bfd (&finfo, sub))
8084 goto error_return;
8085 sub->output_has_begun = TRUE;
8086 }
8087 }
8088 else if (p->type == bfd_section_reloc_link_order
8089 || p->type == bfd_symbol_reloc_link_order)
8090 {
8091 if (! elf_reloc_link_order (abfd, info, o, p))
8092 goto error_return;
8093 }
8094 else
8095 {
8096 if (! _bfd_default_link_order (abfd, info, o, p))
8097 goto error_return;
8098 }
8099 }
8100 }
8101
8102 /* Output any global symbols that got converted to local in a
8103 version script or due to symbol visibility. We do this in a
8104 separate step since ELF requires all local symbols to appear
8105 prior to any global symbols. FIXME: We should only do this if
8106 some global symbols were, in fact, converted to become local.
8107 FIXME: Will this work correctly with the Irix 5 linker? */
8108 eoinfo.failed = FALSE;
8109 eoinfo.finfo = &finfo;
8110 eoinfo.localsyms = TRUE;
8111 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8112 &eoinfo);
8113 if (eoinfo.failed)
8114 return FALSE;
8115
8116 /* That wrote out all the local symbols. Finish up the symbol table
8117 with the global symbols. Even if we want to strip everything we
8118 can, we still need to deal with those global symbols that got
8119 converted to local in a version script. */
8120
8121 /* The sh_info field records the index of the first non local symbol. */
8122 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8123
8124 if (dynamic
8125 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8126 {
8127 Elf_Internal_Sym sym;
8128 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8129 long last_local = 0;
8130
8131 /* Write out the section symbols for the output sections. */
8132 if (info->shared)
8133 {
8134 asection *s;
8135
8136 sym.st_size = 0;
8137 sym.st_name = 0;
8138 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8139 sym.st_other = 0;
8140
8141 for (s = abfd->sections; s != NULL; s = s->next)
8142 {
8143 int indx;
8144 bfd_byte *dest;
8145 long dynindx;
8146
c152c796 8147 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
8148 if (dynindx <= 0)
8149 continue;
8150 indx = elf_section_data (s)->this_idx;
c152c796
AM
8151 BFD_ASSERT (indx > 0);
8152 sym.st_shndx = indx;
8153 sym.st_value = s->vma;
8154 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
8155 if (last_local < dynindx)
8156 last_local = dynindx;
c152c796
AM
8157 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8158 }
c152c796
AM
8159 }
8160
8161 /* Write out the local dynsyms. */
8162 if (elf_hash_table (info)->dynlocal)
8163 {
8164 struct elf_link_local_dynamic_entry *e;
8165 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8166 {
8167 asection *s;
8168 bfd_byte *dest;
8169
8170 sym.st_size = e->isym.st_size;
8171 sym.st_other = e->isym.st_other;
8172
8173 /* Copy the internal symbol as is.
8174 Note that we saved a word of storage and overwrote
8175 the original st_name with the dynstr_index. */
8176 sym = e->isym;
8177
8178 if (e->isym.st_shndx != SHN_UNDEF
8179 && (e->isym.st_shndx < SHN_LORESERVE
8180 || e->isym.st_shndx > SHN_HIRESERVE))
8181 {
8182 s = bfd_section_from_elf_index (e->input_bfd,
8183 e->isym.st_shndx);
8184
8185 sym.st_shndx =
8186 elf_section_data (s->output_section)->this_idx;
8187 sym.st_value = (s->output_section->vma
8188 + s->output_offset
8189 + e->isym.st_value);
8190 }
8191
8192 if (last_local < e->dynindx)
8193 last_local = e->dynindx;
8194
8195 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8196 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8197 }
8198 }
8199
8200 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8201 last_local + 1;
8202 }
8203
8204 /* We get the global symbols from the hash table. */
8205 eoinfo.failed = FALSE;
8206 eoinfo.localsyms = FALSE;
8207 eoinfo.finfo = &finfo;
8208 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8209 &eoinfo);
8210 if (eoinfo.failed)
8211 return FALSE;
8212
8213 /* If backend needs to output some symbols not present in the hash
8214 table, do it now. */
8215 if (bed->elf_backend_output_arch_syms)
8216 {
8217 typedef bfd_boolean (*out_sym_func)
8218 (void *, const char *, Elf_Internal_Sym *, asection *,
8219 struct elf_link_hash_entry *);
8220
8221 if (! ((*bed->elf_backend_output_arch_syms)
8222 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8223 return FALSE;
8224 }
8225
8226 /* Flush all symbols to the file. */
8227 if (! elf_link_flush_output_syms (&finfo, bed))
8228 return FALSE;
8229
8230 /* Now we know the size of the symtab section. */
8231 off += symtab_hdr->sh_size;
8232
8233 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8234 if (symtab_shndx_hdr->sh_name != 0)
8235 {
8236 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8237 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8238 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8239 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8240 symtab_shndx_hdr->sh_size = amt;
8241
8242 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8243 off, TRUE);
8244
8245 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8246 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8247 return FALSE;
8248 }
8249
8250
8251 /* Finish up and write out the symbol string table (.strtab)
8252 section. */
8253 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8254 /* sh_name was set in prep_headers. */
8255 symstrtab_hdr->sh_type = SHT_STRTAB;
8256 symstrtab_hdr->sh_flags = 0;
8257 symstrtab_hdr->sh_addr = 0;
8258 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8259 symstrtab_hdr->sh_entsize = 0;
8260 symstrtab_hdr->sh_link = 0;
8261 symstrtab_hdr->sh_info = 0;
8262 /* sh_offset is set just below. */
8263 symstrtab_hdr->sh_addralign = 1;
8264
8265 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8266 elf_tdata (abfd)->next_file_pos = off;
8267
8268 if (bfd_get_symcount (abfd) > 0)
8269 {
8270 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8271 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8272 return FALSE;
8273 }
8274
8275 /* Adjust the relocs to have the correct symbol indices. */
8276 for (o = abfd->sections; o != NULL; o = o->next)
8277 {
8278 if ((o->flags & SEC_RELOC) == 0)
8279 continue;
8280
8281 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8282 elf_section_data (o)->rel_count,
8283 elf_section_data (o)->rel_hashes);
8284 if (elf_section_data (o)->rel_hdr2 != NULL)
8285 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8286 elf_section_data (o)->rel_count2,
8287 (elf_section_data (o)->rel_hashes
8288 + elf_section_data (o)->rel_count));
8289
8290 /* Set the reloc_count field to 0 to prevent write_relocs from
8291 trying to swap the relocs out itself. */
8292 o->reloc_count = 0;
8293 }
8294
8295 if (dynamic && info->combreloc && dynobj != NULL)
8296 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8297
8298 /* If we are linking against a dynamic object, or generating a
8299 shared library, finish up the dynamic linking information. */
8300 if (dynamic)
8301 {
8302 bfd_byte *dyncon, *dynconend;
8303
8304 /* Fix up .dynamic entries. */
8305 o = bfd_get_section_by_name (dynobj, ".dynamic");
8306 BFD_ASSERT (o != NULL);
8307
8308 dyncon = o->contents;
eea6121a 8309 dynconend = o->contents + o->size;
c152c796
AM
8310 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8311 {
8312 Elf_Internal_Dyn dyn;
8313 const char *name;
8314 unsigned int type;
8315
8316 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8317
8318 switch (dyn.d_tag)
8319 {
8320 default:
8321 continue;
8322 case DT_NULL:
8323 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8324 {
8325 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8326 {
8327 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8328 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8329 default: continue;
8330 }
8331 dyn.d_un.d_val = relativecount;
8332 relativecount = 0;
8333 break;
8334 }
8335 continue;
8336
8337 case DT_INIT:
8338 name = info->init_function;
8339 goto get_sym;
8340 case DT_FINI:
8341 name = info->fini_function;
8342 get_sym:
8343 {
8344 struct elf_link_hash_entry *h;
8345
8346 h = elf_link_hash_lookup (elf_hash_table (info), name,
8347 FALSE, FALSE, TRUE);
8348 if (h != NULL
8349 && (h->root.type == bfd_link_hash_defined
8350 || h->root.type == bfd_link_hash_defweak))
8351 {
8352 dyn.d_un.d_val = h->root.u.def.value;
8353 o = h->root.u.def.section;
8354 if (o->output_section != NULL)
8355 dyn.d_un.d_val += (o->output_section->vma
8356 + o->output_offset);
8357 else
8358 {
8359 /* The symbol is imported from another shared
8360 library and does not apply to this one. */
8361 dyn.d_un.d_val = 0;
8362 }
8363 break;
8364 }
8365 }
8366 continue;
8367
8368 case DT_PREINIT_ARRAYSZ:
8369 name = ".preinit_array";
8370 goto get_size;
8371 case DT_INIT_ARRAYSZ:
8372 name = ".init_array";
8373 goto get_size;
8374 case DT_FINI_ARRAYSZ:
8375 name = ".fini_array";
8376 get_size:
8377 o = bfd_get_section_by_name (abfd, name);
8378 if (o == NULL)
8379 {
8380 (*_bfd_error_handler)
d003868e 8381 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
8382 goto error_return;
8383 }
eea6121a 8384 if (o->size == 0)
c152c796
AM
8385 (*_bfd_error_handler)
8386 (_("warning: %s section has zero size"), name);
eea6121a 8387 dyn.d_un.d_val = o->size;
c152c796
AM
8388 break;
8389
8390 case DT_PREINIT_ARRAY:
8391 name = ".preinit_array";
8392 goto get_vma;
8393 case DT_INIT_ARRAY:
8394 name = ".init_array";
8395 goto get_vma;
8396 case DT_FINI_ARRAY:
8397 name = ".fini_array";
8398 goto get_vma;
8399
8400 case DT_HASH:
8401 name = ".hash";
8402 goto get_vma;
8403 case DT_STRTAB:
8404 name = ".dynstr";
8405 goto get_vma;
8406 case DT_SYMTAB:
8407 name = ".dynsym";
8408 goto get_vma;
8409 case DT_VERDEF:
8410 name = ".gnu.version_d";
8411 goto get_vma;
8412 case DT_VERNEED:
8413 name = ".gnu.version_r";
8414 goto get_vma;
8415 case DT_VERSYM:
8416 name = ".gnu.version";
8417 get_vma:
8418 o = bfd_get_section_by_name (abfd, name);
8419 if (o == NULL)
8420 {
8421 (*_bfd_error_handler)
d003868e 8422 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
8423 goto error_return;
8424 }
8425 dyn.d_un.d_ptr = o->vma;
8426 break;
8427
8428 case DT_REL:
8429 case DT_RELA:
8430 case DT_RELSZ:
8431 case DT_RELASZ:
8432 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8433 type = SHT_REL;
8434 else
8435 type = SHT_RELA;
8436 dyn.d_un.d_val = 0;
8437 for (i = 1; i < elf_numsections (abfd); i++)
8438 {
8439 Elf_Internal_Shdr *hdr;
8440
8441 hdr = elf_elfsections (abfd)[i];
8442 if (hdr->sh_type == type
8443 && (hdr->sh_flags & SHF_ALLOC) != 0)
8444 {
8445 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8446 dyn.d_un.d_val += hdr->sh_size;
8447 else
8448 {
8449 if (dyn.d_un.d_val == 0
8450 || hdr->sh_addr < dyn.d_un.d_val)
8451 dyn.d_un.d_val = hdr->sh_addr;
8452 }
8453 }
8454 }
8455 break;
8456 }
8457 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8458 }
8459 }
8460
8461 /* If we have created any dynamic sections, then output them. */
8462 if (dynobj != NULL)
8463 {
8464 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8465 goto error_return;
8466
8467 for (o = dynobj->sections; o != NULL; o = o->next)
8468 {
8469 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 8470 || o->size == 0
c152c796
AM
8471 || o->output_section == bfd_abs_section_ptr)
8472 continue;
8473 if ((o->flags & SEC_LINKER_CREATED) == 0)
8474 {
8475 /* At this point, we are only interested in sections
8476 created by _bfd_elf_link_create_dynamic_sections. */
8477 continue;
8478 }
3722b82f
AM
8479 if (elf_hash_table (info)->stab_info.stabstr == o)
8480 continue;
eea6121a
AM
8481 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8482 continue;
c152c796
AM
8483 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8484 != SHT_STRTAB)
8485 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8486 {
8487 if (! bfd_set_section_contents (abfd, o->output_section,
8488 o->contents,
8489 (file_ptr) o->output_offset,
eea6121a 8490 o->size))
c152c796
AM
8491 goto error_return;
8492 }
8493 else
8494 {
8495 /* The contents of the .dynstr section are actually in a
8496 stringtab. */
8497 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8498 if (bfd_seek (abfd, off, SEEK_SET) != 0
8499 || ! _bfd_elf_strtab_emit (abfd,
8500 elf_hash_table (info)->dynstr))
8501 goto error_return;
8502 }
8503 }
8504 }
8505
8506 if (info->relocatable)
8507 {
8508 bfd_boolean failed = FALSE;
8509
8510 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8511 if (failed)
8512 goto error_return;
8513 }
8514
8515 /* If we have optimized stabs strings, output them. */
3722b82f 8516 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
8517 {
8518 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8519 goto error_return;
8520 }
8521
8522 if (info->eh_frame_hdr)
8523 {
8524 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8525 goto error_return;
8526 }
8527
8528 if (finfo.symstrtab != NULL)
8529 _bfd_stringtab_free (finfo.symstrtab);
8530 if (finfo.contents != NULL)
8531 free (finfo.contents);
8532 if (finfo.external_relocs != NULL)
8533 free (finfo.external_relocs);
8534 if (finfo.internal_relocs != NULL)
8535 free (finfo.internal_relocs);
8536 if (finfo.external_syms != NULL)
8537 free (finfo.external_syms);
8538 if (finfo.locsym_shndx != NULL)
8539 free (finfo.locsym_shndx);
8540 if (finfo.internal_syms != NULL)
8541 free (finfo.internal_syms);
8542 if (finfo.indices != NULL)
8543 free (finfo.indices);
8544 if (finfo.sections != NULL)
8545 free (finfo.sections);
8546 if (finfo.symbuf != NULL)
8547 free (finfo.symbuf);
8548 if (finfo.symshndxbuf != NULL)
8549 free (finfo.symshndxbuf);
8550 for (o = abfd->sections; o != NULL; o = o->next)
8551 {
8552 if ((o->flags & SEC_RELOC) != 0
8553 && elf_section_data (o)->rel_hashes != NULL)
8554 free (elf_section_data (o)->rel_hashes);
8555 }
8556
8557 elf_tdata (abfd)->linker = TRUE;
8558
8559 return TRUE;
8560
8561 error_return:
8562 if (finfo.symstrtab != NULL)
8563 _bfd_stringtab_free (finfo.symstrtab);
8564 if (finfo.contents != NULL)
8565 free (finfo.contents);
8566 if (finfo.external_relocs != NULL)
8567 free (finfo.external_relocs);
8568 if (finfo.internal_relocs != NULL)
8569 free (finfo.internal_relocs);
8570 if (finfo.external_syms != NULL)
8571 free (finfo.external_syms);
8572 if (finfo.locsym_shndx != NULL)
8573 free (finfo.locsym_shndx);
8574 if (finfo.internal_syms != NULL)
8575 free (finfo.internal_syms);
8576 if (finfo.indices != NULL)
8577 free (finfo.indices);
8578 if (finfo.sections != NULL)
8579 free (finfo.sections);
8580 if (finfo.symbuf != NULL)
8581 free (finfo.symbuf);
8582 if (finfo.symshndxbuf != NULL)
8583 free (finfo.symshndxbuf);
8584 for (o = abfd->sections; o != NULL; o = o->next)
8585 {
8586 if ((o->flags & SEC_RELOC) != 0
8587 && elf_section_data (o)->rel_hashes != NULL)
8588 free (elf_section_data (o)->rel_hashes);
8589 }
8590
8591 return FALSE;
8592}
8593\f
8594/* Garbage collect unused sections. */
8595
8596/* The mark phase of garbage collection. For a given section, mark
8597 it and any sections in this section's group, and all the sections
8598 which define symbols to which it refers. */
8599
8600typedef asection * (*gc_mark_hook_fn)
8601 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8602 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8603
ccfa59ea
AM
8604bfd_boolean
8605_bfd_elf_gc_mark (struct bfd_link_info *info,
8606 asection *sec,
8607 gc_mark_hook_fn gc_mark_hook)
c152c796
AM
8608{
8609 bfd_boolean ret;
8610 asection *group_sec;
8611
8612 sec->gc_mark = 1;
8613
8614 /* Mark all the sections in the group. */
8615 group_sec = elf_section_data (sec)->next_in_group;
8616 if (group_sec && !group_sec->gc_mark)
ccfa59ea 8617 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
8618 return FALSE;
8619
8620 /* Look through the section relocs. */
8621 ret = TRUE;
8622 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8623 {
8624 Elf_Internal_Rela *relstart, *rel, *relend;
8625 Elf_Internal_Shdr *symtab_hdr;
8626 struct elf_link_hash_entry **sym_hashes;
8627 size_t nlocsyms;
8628 size_t extsymoff;
8629 bfd *input_bfd = sec->owner;
8630 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8631 Elf_Internal_Sym *isym = NULL;
8632 int r_sym_shift;
8633
8634 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8635 sym_hashes = elf_sym_hashes (input_bfd);
8636
8637 /* Read the local symbols. */
8638 if (elf_bad_symtab (input_bfd))
8639 {
8640 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8641 extsymoff = 0;
8642 }
8643 else
8644 extsymoff = nlocsyms = symtab_hdr->sh_info;
8645
8646 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8647 if (isym == NULL && nlocsyms != 0)
8648 {
8649 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8650 NULL, NULL, NULL);
8651 if (isym == NULL)
8652 return FALSE;
8653 }
8654
8655 /* Read the relocations. */
8656 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8657 info->keep_memory);
8658 if (relstart == NULL)
8659 {
8660 ret = FALSE;
8661 goto out1;
8662 }
8663 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8664
8665 if (bed->s->arch_size == 32)
8666 r_sym_shift = 8;
8667 else
8668 r_sym_shift = 32;
8669
8670 for (rel = relstart; rel < relend; rel++)
8671 {
8672 unsigned long r_symndx;
8673 asection *rsec;
8674 struct elf_link_hash_entry *h;
8675
8676 r_symndx = rel->r_info >> r_sym_shift;
8677 if (r_symndx == 0)
8678 continue;
8679
8680 if (r_symndx >= nlocsyms
8681 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8682 {
8683 h = sym_hashes[r_symndx - extsymoff];
20f0a1ad
AM
8684 while (h->root.type == bfd_link_hash_indirect
8685 || h->root.type == bfd_link_hash_warning)
8686 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796
AM
8687 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8688 }
8689 else
8690 {
8691 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8692 }
8693
8694 if (rsec && !rsec->gc_mark)
8695 {
8696 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8697 rsec->gc_mark = 1;
ccfa59ea 8698 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
c152c796
AM
8699 {
8700 ret = FALSE;
8701 goto out2;
8702 }
8703 }
8704 }
8705
8706 out2:
8707 if (elf_section_data (sec)->relocs != relstart)
8708 free (relstart);
8709 out1:
8710 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8711 {
8712 if (! info->keep_memory)
8713 free (isym);
8714 else
8715 symtab_hdr->contents = (unsigned char *) isym;
8716 }
8717 }
8718
8719 return ret;
8720}
8721
8722/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8723
8724static bfd_boolean
8725elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8726{
8727 int *idx = idxptr;
8728
8729 if (h->root.type == bfd_link_hash_warning)
8730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8731
8732 if (h->dynindx != -1
8733 && ((h->root.type != bfd_link_hash_defined
8734 && h->root.type != bfd_link_hash_defweak)
8735 || h->root.u.def.section->gc_mark))
8736 h->dynindx = (*idx)++;
8737
8738 return TRUE;
8739}
8740
8741/* The sweep phase of garbage collection. Remove all garbage sections. */
8742
8743typedef bfd_boolean (*gc_sweep_hook_fn)
8744 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8745
8746static bfd_boolean
8747elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8748{
8749 bfd *sub;
8750
8751 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8752 {
8753 asection *o;
8754
8755 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8756 continue;
8757
8758 for (o = sub->sections; o != NULL; o = o->next)
8759 {
7c2c8505
AM
8760 /* Keep debug and special sections. */
8761 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8762 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
c152c796
AM
8763 o->gc_mark = 1;
8764
8765 if (o->gc_mark)
8766 continue;
8767
8768 /* Skip sweeping sections already excluded. */
8769 if (o->flags & SEC_EXCLUDE)
8770 continue;
8771
8772 /* Since this is early in the link process, it is simple
8773 to remove a section from the output. */
8774 o->flags |= SEC_EXCLUDE;
8775
8776 /* But we also have to update some of the relocation
8777 info we collected before. */
8778 if (gc_sweep_hook
8779 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8780 {
8781 Elf_Internal_Rela *internal_relocs;
8782 bfd_boolean r;
8783
8784 internal_relocs
8785 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8786 info->keep_memory);
8787 if (internal_relocs == NULL)
8788 return FALSE;
8789
8790 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8791
8792 if (elf_section_data (o)->relocs != internal_relocs)
8793 free (internal_relocs);
8794
8795 if (!r)
8796 return FALSE;
8797 }
8798 }
8799 }
8800
8801 /* Remove the symbols that were in the swept sections from the dynamic
8802 symbol table. GCFIXME: Anyone know how to get them out of the
8803 static symbol table as well? */
8804 {
8805 int i = 0;
8806
8807 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8808
8809 elf_hash_table (info)->dynsymcount = i;
8810 }
8811
8812 return TRUE;
8813}
8814
8815/* Propagate collected vtable information. This is called through
8816 elf_link_hash_traverse. */
8817
8818static bfd_boolean
8819elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8820{
8821 if (h->root.type == bfd_link_hash_warning)
8822 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8823
8824 /* Those that are not vtables. */
f6e332e6 8825 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
8826 return TRUE;
8827
8828 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 8829 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
8830 return TRUE;
8831
8832 /* If we've already been done, exit. */
f6e332e6 8833 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
8834 return TRUE;
8835
8836 /* Make sure the parent's table is up to date. */
f6e332e6 8837 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 8838
f6e332e6 8839 if (h->vtable->used == NULL)
c152c796
AM
8840 {
8841 /* None of this table's entries were referenced. Re-use the
8842 parent's table. */
f6e332e6
AM
8843 h->vtable->used = h->vtable->parent->vtable->used;
8844 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
8845 }
8846 else
8847 {
8848 size_t n;
8849 bfd_boolean *cu, *pu;
8850
8851 /* Or the parent's entries into ours. */
f6e332e6 8852 cu = h->vtable->used;
c152c796 8853 cu[-1] = TRUE;
f6e332e6 8854 pu = h->vtable->parent->vtable->used;
c152c796
AM
8855 if (pu != NULL)
8856 {
8857 const struct elf_backend_data *bed;
8858 unsigned int log_file_align;
8859
8860 bed = get_elf_backend_data (h->root.u.def.section->owner);
8861 log_file_align = bed->s->log_file_align;
f6e332e6 8862 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
8863 while (n--)
8864 {
8865 if (*pu)
8866 *cu = TRUE;
8867 pu++;
8868 cu++;
8869 }
8870 }
8871 }
8872
8873 return TRUE;
8874}
8875
8876static bfd_boolean
8877elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8878{
8879 asection *sec;
8880 bfd_vma hstart, hend;
8881 Elf_Internal_Rela *relstart, *relend, *rel;
8882 const struct elf_backend_data *bed;
8883 unsigned int log_file_align;
8884
8885 if (h->root.type == bfd_link_hash_warning)
8886 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8887
8888 /* Take care of both those symbols that do not describe vtables as
8889 well as those that are not loaded. */
f6e332e6 8890 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
8891 return TRUE;
8892
8893 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8894 || h->root.type == bfd_link_hash_defweak);
8895
8896 sec = h->root.u.def.section;
8897 hstart = h->root.u.def.value;
8898 hend = hstart + h->size;
8899
8900 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8901 if (!relstart)
8902 return *(bfd_boolean *) okp = FALSE;
8903 bed = get_elf_backend_data (sec->owner);
8904 log_file_align = bed->s->log_file_align;
8905
8906 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8907
8908 for (rel = relstart; rel < relend; ++rel)
8909 if (rel->r_offset >= hstart && rel->r_offset < hend)
8910 {
8911 /* If the entry is in use, do nothing. */
f6e332e6
AM
8912 if (h->vtable->used
8913 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
8914 {
8915 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 8916 if (h->vtable->used[entry])
c152c796
AM
8917 continue;
8918 }
8919 /* Otherwise, kill it. */
8920 rel->r_offset = rel->r_info = rel->r_addend = 0;
8921 }
8922
8923 return TRUE;
8924}
8925
715df9b8
EB
8926/* Mark sections containing dynamically referenced symbols. This is called
8927 through elf_link_hash_traverse. */
8928
8929static bfd_boolean
8930elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8931 void *okp ATTRIBUTE_UNUSED)
8932{
8933 if (h->root.type == bfd_link_hash_warning)
8934 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8935
8936 if ((h->root.type == bfd_link_hash_defined
8937 || h->root.type == bfd_link_hash_defweak)
f5385ebf 8938 && h->ref_dynamic)
715df9b8
EB
8939 h->root.u.def.section->flags |= SEC_KEEP;
8940
8941 return TRUE;
8942}
8943
c152c796
AM
8944/* Do mark and sweep of unused sections. */
8945
8946bfd_boolean
8947bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8948{
8949 bfd_boolean ok = TRUE;
8950 bfd *sub;
8951 asection * (*gc_mark_hook)
8952 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8953 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8954
8955 if (!get_elf_backend_data (abfd)->can_gc_sections
8956 || info->relocatable
8957 || info->emitrelocations
715df9b8
EB
8958 || info->shared
8959 || !is_elf_hash_table (info->hash))
c152c796
AM
8960 {
8961 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8962 return TRUE;
8963 }
8964
8965 /* Apply transitive closure to the vtable entry usage info. */
8966 elf_link_hash_traverse (elf_hash_table (info),
8967 elf_gc_propagate_vtable_entries_used,
8968 &ok);
8969 if (!ok)
8970 return FALSE;
8971
8972 /* Kill the vtable relocations that were not used. */
8973 elf_link_hash_traverse (elf_hash_table (info),
8974 elf_gc_smash_unused_vtentry_relocs,
8975 &ok);
8976 if (!ok)
8977 return FALSE;
8978
715df9b8
EB
8979 /* Mark dynamically referenced symbols. */
8980 if (elf_hash_table (info)->dynamic_sections_created)
8981 elf_link_hash_traverse (elf_hash_table (info),
8982 elf_gc_mark_dynamic_ref_symbol,
8983 &ok);
8984 if (!ok)
8985 return FALSE;
c152c796 8986
715df9b8 8987 /* Grovel through relocs to find out who stays ... */
c152c796
AM
8988 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8989 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8990 {
8991 asection *o;
8992
8993 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8994 continue;
8995
8996 for (o = sub->sections; o != NULL; o = o->next)
8997 {
8998 if (o->flags & SEC_KEEP)
715df9b8
EB
8999 {
9000 /* _bfd_elf_discard_section_eh_frame knows how to discard
9001 orphaned FDEs so don't mark sections referenced by the
9002 EH frame section. */
9003 if (strcmp (o->name, ".eh_frame") == 0)
9004 o->gc_mark = 1;
ccfa59ea 9005 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
715df9b8
EB
9006 return FALSE;
9007 }
c152c796
AM
9008 }
9009 }
9010
9011 /* ... and mark SEC_EXCLUDE for those that go. */
9012 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
9013 return FALSE;
9014
9015 return TRUE;
9016}
9017\f
9018/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9019
9020bfd_boolean
9021bfd_elf_gc_record_vtinherit (bfd *abfd,
9022 asection *sec,
9023 struct elf_link_hash_entry *h,
9024 bfd_vma offset)
9025{
9026 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9027 struct elf_link_hash_entry **search, *child;
9028 bfd_size_type extsymcount;
9029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9030
9031 /* The sh_info field of the symtab header tells us where the
9032 external symbols start. We don't care about the local symbols at
9033 this point. */
9034 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9035 if (!elf_bad_symtab (abfd))
9036 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9037
9038 sym_hashes = elf_sym_hashes (abfd);
9039 sym_hashes_end = sym_hashes + extsymcount;
9040
9041 /* Hunt down the child symbol, which is in this section at the same
9042 offset as the relocation. */
9043 for (search = sym_hashes; search != sym_hashes_end; ++search)
9044 {
9045 if ((child = *search) != NULL
9046 && (child->root.type == bfd_link_hash_defined
9047 || child->root.type == bfd_link_hash_defweak)
9048 && child->root.u.def.section == sec
9049 && child->root.u.def.value == offset)
9050 goto win;
9051 }
9052
d003868e
AM
9053 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9054 abfd, sec, (unsigned long) offset);
c152c796
AM
9055 bfd_set_error (bfd_error_invalid_operation);
9056 return FALSE;
9057
9058 win:
f6e332e6
AM
9059 if (!child->vtable)
9060 {
9061 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9062 if (!child->vtable)
9063 return FALSE;
9064 }
c152c796
AM
9065 if (!h)
9066 {
9067 /* This *should* only be the absolute section. It could potentially
9068 be that someone has defined a non-global vtable though, which
9069 would be bad. It isn't worth paging in the local symbols to be
9070 sure though; that case should simply be handled by the assembler. */
9071
f6e332e6 9072 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
9073 }
9074 else
f6e332e6 9075 child->vtable->parent = h;
c152c796
AM
9076
9077 return TRUE;
9078}
9079
9080/* Called from check_relocs to record the existence of a VTENTRY reloc. */
9081
9082bfd_boolean
9083bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9084 asection *sec ATTRIBUTE_UNUSED,
9085 struct elf_link_hash_entry *h,
9086 bfd_vma addend)
9087{
9088 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9089 unsigned int log_file_align = bed->s->log_file_align;
9090
f6e332e6
AM
9091 if (!h->vtable)
9092 {
9093 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9094 if (!h->vtable)
9095 return FALSE;
9096 }
9097
9098 if (addend >= h->vtable->size)
c152c796
AM
9099 {
9100 size_t size, bytes, file_align;
f6e332e6 9101 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
9102
9103 /* While the symbol is undefined, we have to be prepared to handle
9104 a zero size. */
9105 file_align = 1 << log_file_align;
9106 if (h->root.type == bfd_link_hash_undefined)
9107 size = addend + file_align;
9108 else
9109 {
9110 size = h->size;
9111 if (addend >= size)
9112 {
9113 /* Oops! We've got a reference past the defined end of
9114 the table. This is probably a bug -- shall we warn? */
9115 size = addend + file_align;
9116 }
9117 }
9118 size = (size + file_align - 1) & -file_align;
9119
9120 /* Allocate one extra entry for use as a "done" flag for the
9121 consolidation pass. */
9122 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9123
9124 if (ptr)
9125 {
9126 ptr = bfd_realloc (ptr - 1, bytes);
9127
9128 if (ptr != NULL)
9129 {
9130 size_t oldbytes;
9131
f6e332e6 9132 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
9133 * sizeof (bfd_boolean));
9134 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9135 }
9136 }
9137 else
9138 ptr = bfd_zmalloc (bytes);
9139
9140 if (ptr == NULL)
9141 return FALSE;
9142
9143 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
9144 h->vtable->used = ptr + 1;
9145 h->vtable->size = size;
c152c796
AM
9146 }
9147
f6e332e6 9148 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
9149
9150 return TRUE;
9151}
9152
9153struct alloc_got_off_arg {
9154 bfd_vma gotoff;
9155 unsigned int got_elt_size;
9156};
9157
9158/* We need a special top-level link routine to convert got reference counts
9159 to real got offsets. */
9160
9161static bfd_boolean
9162elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9163{
9164 struct alloc_got_off_arg *gofarg = arg;
9165
9166 if (h->root.type == bfd_link_hash_warning)
9167 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9168
9169 if (h->got.refcount > 0)
9170 {
9171 h->got.offset = gofarg->gotoff;
9172 gofarg->gotoff += gofarg->got_elt_size;
9173 }
9174 else
9175 h->got.offset = (bfd_vma) -1;
9176
9177 return TRUE;
9178}
9179
9180/* And an accompanying bit to work out final got entry offsets once
9181 we're done. Should be called from final_link. */
9182
9183bfd_boolean
9184bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9185 struct bfd_link_info *info)
9186{
9187 bfd *i;
9188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9189 bfd_vma gotoff;
9190 unsigned int got_elt_size = bed->s->arch_size / 8;
9191 struct alloc_got_off_arg gofarg;
9192
9193 if (! is_elf_hash_table (info->hash))
9194 return FALSE;
9195
9196 /* The GOT offset is relative to the .got section, but the GOT header is
9197 put into the .got.plt section, if the backend uses it. */
9198 if (bed->want_got_plt)
9199 gotoff = 0;
9200 else
9201 gotoff = bed->got_header_size;
9202
9203 /* Do the local .got entries first. */
9204 for (i = info->input_bfds; i; i = i->link_next)
9205 {
9206 bfd_signed_vma *local_got;
9207 bfd_size_type j, locsymcount;
9208 Elf_Internal_Shdr *symtab_hdr;
9209
9210 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9211 continue;
9212
9213 local_got = elf_local_got_refcounts (i);
9214 if (!local_got)
9215 continue;
9216
9217 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9218 if (elf_bad_symtab (i))
9219 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9220 else
9221 locsymcount = symtab_hdr->sh_info;
9222
9223 for (j = 0; j < locsymcount; ++j)
9224 {
9225 if (local_got[j] > 0)
9226 {
9227 local_got[j] = gotoff;
9228 gotoff += got_elt_size;
9229 }
9230 else
9231 local_got[j] = (bfd_vma) -1;
9232 }
9233 }
9234
9235 /* Then the global .got entries. .plt refcounts are handled by
9236 adjust_dynamic_symbol */
9237 gofarg.gotoff = gotoff;
9238 gofarg.got_elt_size = got_elt_size;
9239 elf_link_hash_traverse (elf_hash_table (info),
9240 elf_gc_allocate_got_offsets,
9241 &gofarg);
9242 return TRUE;
9243}
9244
9245/* Many folk need no more in the way of final link than this, once
9246 got entry reference counting is enabled. */
9247
9248bfd_boolean
9249bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9250{
9251 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9252 return FALSE;
9253
9254 /* Invoke the regular ELF backend linker to do all the work. */
9255 return bfd_elf_final_link (abfd, info);
9256}
9257
9258bfd_boolean
9259bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9260{
9261 struct elf_reloc_cookie *rcookie = cookie;
9262
9263 if (rcookie->bad_symtab)
9264 rcookie->rel = rcookie->rels;
9265
9266 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9267 {
9268 unsigned long r_symndx;
9269
9270 if (! rcookie->bad_symtab)
9271 if (rcookie->rel->r_offset > offset)
9272 return FALSE;
9273 if (rcookie->rel->r_offset != offset)
9274 continue;
9275
9276 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9277 if (r_symndx == SHN_UNDEF)
9278 return TRUE;
9279
9280 if (r_symndx >= rcookie->locsymcount
9281 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9282 {
9283 struct elf_link_hash_entry *h;
9284
9285 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9286
9287 while (h->root.type == bfd_link_hash_indirect
9288 || h->root.type == bfd_link_hash_warning)
9289 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9290
9291 if ((h->root.type == bfd_link_hash_defined
9292 || h->root.type == bfd_link_hash_defweak)
9293 && elf_discarded_section (h->root.u.def.section))
9294 return TRUE;
9295 else
9296 return FALSE;
9297 }
9298 else
9299 {
9300 /* It's not a relocation against a global symbol,
9301 but it could be a relocation against a local
9302 symbol for a discarded section. */
9303 asection *isec;
9304 Elf_Internal_Sym *isym;
9305
9306 /* Need to: get the symbol; get the section. */
9307 isym = &rcookie->locsyms[r_symndx];
9308 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9309 {
9310 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9311 if (isec != NULL && elf_discarded_section (isec))
9312 return TRUE;
9313 }
9314 }
9315 return FALSE;
9316 }
9317 return FALSE;
9318}
9319
9320/* Discard unneeded references to discarded sections.
9321 Returns TRUE if any section's size was changed. */
9322/* This function assumes that the relocations are in sorted order,
9323 which is true for all known assemblers. */
9324
9325bfd_boolean
9326bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9327{
9328 struct elf_reloc_cookie cookie;
9329 asection *stab, *eh;
9330 Elf_Internal_Shdr *symtab_hdr;
9331 const struct elf_backend_data *bed;
9332 bfd *abfd;
9333 unsigned int count;
9334 bfd_boolean ret = FALSE;
9335
9336 if (info->traditional_format
9337 || !is_elf_hash_table (info->hash))
9338 return FALSE;
9339
9340 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9341 {
9342 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9343 continue;
9344
9345 bed = get_elf_backend_data (abfd);
9346
9347 if ((abfd->flags & DYNAMIC) != 0)
9348 continue;
9349
9350 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9351 if (info->relocatable
9352 || (eh != NULL
eea6121a 9353 && (eh->size == 0
c152c796
AM
9354 || bfd_is_abs_section (eh->output_section))))
9355 eh = NULL;
9356
9357 stab = bfd_get_section_by_name (abfd, ".stab");
9358 if (stab != NULL
eea6121a 9359 && (stab->size == 0
c152c796
AM
9360 || bfd_is_abs_section (stab->output_section)
9361 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9362 stab = NULL;
9363
9364 if (stab == NULL
9365 && eh == NULL
9366 && bed->elf_backend_discard_info == NULL)
9367 continue;
9368
9369 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9370 cookie.abfd = abfd;
9371 cookie.sym_hashes = elf_sym_hashes (abfd);
9372 cookie.bad_symtab = elf_bad_symtab (abfd);
9373 if (cookie.bad_symtab)
9374 {
9375 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9376 cookie.extsymoff = 0;
9377 }
9378 else
9379 {
9380 cookie.locsymcount = symtab_hdr->sh_info;
9381 cookie.extsymoff = symtab_hdr->sh_info;
9382 }
9383
9384 if (bed->s->arch_size == 32)
9385 cookie.r_sym_shift = 8;
9386 else
9387 cookie.r_sym_shift = 32;
9388
9389 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9390 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9391 {
9392 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9393 cookie.locsymcount, 0,
9394 NULL, NULL, NULL);
9395 if (cookie.locsyms == NULL)
9396 return FALSE;
9397 }
9398
9399 if (stab != NULL)
9400 {
9401 cookie.rels = NULL;
9402 count = stab->reloc_count;
9403 if (count != 0)
9404 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9405 info->keep_memory);
9406 if (cookie.rels != NULL)
9407 {
9408 cookie.rel = cookie.rels;
9409 cookie.relend = cookie.rels;
9410 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9411 if (_bfd_discard_section_stabs (abfd, stab,
9412 elf_section_data (stab)->sec_info,
9413 bfd_elf_reloc_symbol_deleted_p,
9414 &cookie))
9415 ret = TRUE;
9416 if (elf_section_data (stab)->relocs != cookie.rels)
9417 free (cookie.rels);
9418 }
9419 }
9420
9421 if (eh != NULL)
9422 {
9423 cookie.rels = NULL;
9424 count = eh->reloc_count;
9425 if (count != 0)
9426 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9427 info->keep_memory);
9428 cookie.rel = cookie.rels;
9429 cookie.relend = cookie.rels;
9430 if (cookie.rels != NULL)
9431 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9432
9433 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9434 bfd_elf_reloc_symbol_deleted_p,
9435 &cookie))
9436 ret = TRUE;
9437
9438 if (cookie.rels != NULL
9439 && elf_section_data (eh)->relocs != cookie.rels)
9440 free (cookie.rels);
9441 }
9442
9443 if (bed->elf_backend_discard_info != NULL
9444 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9445 ret = TRUE;
9446
9447 if (cookie.locsyms != NULL
9448 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9449 {
9450 if (! info->keep_memory)
9451 free (cookie.locsyms);
9452 else
9453 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9454 }
9455 }
9456
9457 if (info->eh_frame_hdr
9458 && !info->relocatable
9459 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9460 ret = TRUE;
9461
9462 return ret;
9463}
082b7297
L
9464
9465void
9466_bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9467{
9468 flagword flags;
6d2cd210 9469 const char *name, *p;
082b7297
L
9470 struct bfd_section_already_linked *l;
9471 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666
L
9472 asection *group;
9473
9474 /* A single member comdat group section may be discarded by a
9475 linkonce section. See below. */
9476 if (sec->output_section == bfd_abs_section_ptr)
9477 return;
082b7297
L
9478
9479 flags = sec->flags;
3d7f7666
L
9480
9481 /* Check if it belongs to a section group. */
9482 group = elf_sec_group (sec);
9483
9484 /* Return if it isn't a linkonce section nor a member of a group. A
9485 comdat group section also has SEC_LINK_ONCE set. */
9486 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
082b7297
L
9487 return;
9488
3d7f7666
L
9489 if (group)
9490 {
9491 /* If this is the member of a single member comdat group, check if
9492 the group should be discarded. */
9493 if (elf_next_in_group (sec) == sec
9494 && (group->flags & SEC_LINK_ONCE) != 0)
9495 sec = group;
9496 else
9497 return;
9498 }
9499
082b7297
L
9500 /* FIXME: When doing a relocatable link, we may have trouble
9501 copying relocations in other sections that refer to local symbols
9502 in the section being discarded. Those relocations will have to
9503 be converted somehow; as of this writing I'm not sure that any of
9504 the backends handle that correctly.
9505
9506 It is tempting to instead not discard link once sections when
9507 doing a relocatable link (technically, they should be discarded
9508 whenever we are building constructors). However, that fails,
9509 because the linker winds up combining all the link once sections
9510 into a single large link once section, which defeats the purpose
9511 of having link once sections in the first place.
9512
9513 Also, not merging link once sections in a relocatable link
9514 causes trouble for MIPS ELF, which relies on link once semantics
9515 to handle the .reginfo section correctly. */
9516
9517 name = bfd_get_section_name (abfd, sec);
9518
6d2cd210
JJ
9519 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9520 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9521 p++;
9522 else
9523 p = name;
9524
9525 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
9526
9527 for (l = already_linked_list->entry; l != NULL; l = l->next)
9528 {
9529 /* We may have 3 different sections on the list: group section,
9530 comdat section and linkonce section. SEC may be a linkonce or
9531 group section. We match a group section with a group section,
9532 a linkonce section with a linkonce section, and ignore comdat
9533 section. */
3d7f7666 9534 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
6d2cd210 9535 && strcmp (name, l->sec->name) == 0
082b7297
L
9536 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9537 {
9538 /* The section has already been linked. See if we should
6d2cd210 9539 issue a warning. */
082b7297
L
9540 switch (flags & SEC_LINK_DUPLICATES)
9541 {
9542 default:
9543 abort ();
9544
9545 case SEC_LINK_DUPLICATES_DISCARD:
9546 break;
9547
9548 case SEC_LINK_DUPLICATES_ONE_ONLY:
9549 (*_bfd_error_handler)
d003868e
AM
9550 (_("%B: ignoring duplicate section `%A'\n"),
9551 abfd, sec);
082b7297
L
9552 break;
9553
9554 case SEC_LINK_DUPLICATES_SAME_SIZE:
9555 if (sec->size != l->sec->size)
9556 (*_bfd_error_handler)
d003868e
AM
9557 (_("%B: duplicate section `%A' has different size\n"),
9558 abfd, sec);
082b7297 9559 break;
ea5158d8
DJ
9560
9561 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9562 if (sec->size != l->sec->size)
9563 (*_bfd_error_handler)
9564 (_("%B: duplicate section `%A' has different size\n"),
9565 abfd, sec);
9566 else if (sec->size != 0)
9567 {
9568 bfd_byte *sec_contents, *l_sec_contents;
9569
9570 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9571 (*_bfd_error_handler)
9572 (_("%B: warning: could not read contents of section `%A'\n"),
9573 abfd, sec);
9574 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9575 &l_sec_contents))
9576 (*_bfd_error_handler)
9577 (_("%B: warning: could not read contents of section `%A'\n"),
9578 l->sec->owner, l->sec);
9579 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9580 (*_bfd_error_handler)
9581 (_("%B: warning: duplicate section `%A' has different contents\n"),
9582 abfd, sec);
9583
9584 if (sec_contents)
9585 free (sec_contents);
9586 if (l_sec_contents)
9587 free (l_sec_contents);
9588 }
9589 break;
082b7297
L
9590 }
9591
9592 /* Set the output_section field so that lang_add_section
9593 does not create a lang_input_section structure for this
9594 section. Since there might be a symbol in the section
9595 being discarded, we must retain a pointer to the section
9596 which we are really going to use. */
9597 sec->output_section = bfd_abs_section_ptr;
9598 sec->kept_section = l->sec;
9599
9600 if (flags & SEC_GROUP)
3d7f7666
L
9601 {
9602 asection *first = elf_next_in_group (sec);
9603 asection *s = first;
9604
9605 while (s != NULL)
9606 {
9607 s->output_section = bfd_abs_section_ptr;
9608 /* Record which group discards it. */
9609 s->kept_section = l->sec;
9610 s = elf_next_in_group (s);
9611 /* These lists are circular. */
9612 if (s == first)
9613 break;
9614 }
9615 }
082b7297
L
9616
9617 return;
9618 }
9619 }
9620
3d7f7666
L
9621 if (group)
9622 {
9623 /* If this is the member of a single member comdat group and the
9624 group hasn't be discarded, we check if it matches a linkonce
9625 section. We only record the discarded comdat group. Otherwise
9626 the undiscarded group will be discarded incorrectly later since
9627 itself has been recorded. */
6d2cd210
JJ
9628 for (l = already_linked_list->entry; l != NULL; l = l->next)
9629 if ((l->sec->flags & SEC_GROUP) == 0
9630 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9631 && bfd_elf_match_symbols_in_sections (l->sec,
9632 elf_next_in_group (sec)))
9633 {
9634 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9635 elf_next_in_group (sec)->kept_section = l->sec;
9636 group->output_section = bfd_abs_section_ptr;
9637 break;
9638 }
9639 if (l == NULL)
3d7f7666
L
9640 return;
9641 }
9642 else
9643 /* There is no direct match. But for linkonce section, we should
9644 check if there is a match with comdat group member. We always
9645 record the linkonce section, discarded or not. */
6d2cd210
JJ
9646 for (l = already_linked_list->entry; l != NULL; l = l->next)
9647 if (l->sec->flags & SEC_GROUP)
9648 {
9649 asection *first = elf_next_in_group (l->sec);
9650
9651 if (first != NULL
9652 && elf_next_in_group (first) == first
9653 && bfd_elf_match_symbols_in_sections (first, sec))
9654 {
9655 sec->output_section = bfd_abs_section_ptr;
9656 sec->kept_section = l->sec;
9657 break;
9658 }
9659 }
9660
082b7297
L
9661 /* This is the first section with this name. Record it. */
9662 bfd_section_already_linked_table_insert (already_linked_list, sec);
9663}
This page took 1.078874 seconds and 4 git commands to generate.