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