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