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