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