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