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