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