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