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