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