* elf64-ppc.c (bfd_elf64_mkobject): Define.
[deliverable/binutils-gdb.git] / bfd / elflink.h
1 /* ELF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* ELF linker code. */
22
23 static bfd_boolean is_global_data_symbol_definition
24 PARAMS ((bfd *, Elf_Internal_Sym *));
25 static bfd_boolean elf_link_is_defined_archive_symbol
26 PARAMS ((bfd *, carsym *));
27 static bfd_boolean elf_link_add_object_symbols
28 PARAMS ((bfd *, struct bfd_link_info *));
29 static bfd_boolean elf_link_add_archive_symbols
30 PARAMS ((bfd *, struct bfd_link_info *));
31 static bfd_boolean elf_finalize_dynstr
32 PARAMS ((bfd *, struct bfd_link_info *));
33 static bfd_boolean elf_collect_hash_codes
34 PARAMS ((struct elf_link_hash_entry *, PTR));
35 static size_t compute_bucket_count
36 PARAMS ((struct bfd_link_info *));
37 static void elf_link_adjust_relocs
38 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
39 struct elf_link_hash_entry **));
40 static int elf_link_sort_cmp1
41 PARAMS ((const void *, const void *));
42 static int elf_link_sort_cmp2
43 PARAMS ((const void *, const void *));
44 static size_t elf_link_sort_relocs
45 PARAMS ((bfd *, struct bfd_link_info *, asection **));
46 static bfd_boolean elf_section_ignore_discarded_relocs
47 PARAMS ((asection *));
48
49 /* Given an ELF BFD, add symbols to the global hash table as
50 appropriate. */
51
52 bfd_boolean
53 elf_bfd_link_add_symbols (abfd, info)
54 bfd *abfd;
55 struct bfd_link_info *info;
56 {
57 switch (bfd_get_format (abfd))
58 {
59 case bfd_object:
60 return elf_link_add_object_symbols (abfd, info);
61 case bfd_archive:
62 return elf_link_add_archive_symbols (abfd, info);
63 default:
64 bfd_set_error (bfd_error_wrong_format);
65 return FALSE;
66 }
67 }
68 \f
69 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
70 static bfd_boolean
71 is_global_data_symbol_definition (abfd, sym)
72 bfd * abfd ATTRIBUTE_UNUSED;
73 Elf_Internal_Sym * sym;
74 {
75 /* Local symbols do not count, but target specific ones might. */
76 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
77 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
78 return FALSE;
79
80 /* Function symbols do not count. */
81 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
82 return FALSE;
83
84 /* If the section is undefined, then so is the symbol. */
85 if (sym->st_shndx == SHN_UNDEF)
86 return FALSE;
87
88 /* If the symbol is defined in the common section, then
89 it is a common definition and so does not count. */
90 if (sym->st_shndx == SHN_COMMON)
91 return FALSE;
92
93 /* If the symbol is in a target specific section then we
94 must rely upon the backend to tell us what it is. */
95 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
96 /* FIXME - this function is not coded yet:
97
98 return _bfd_is_global_symbol_definition (abfd, sym);
99
100 Instead for now assume that the definition is not global,
101 Even if this is wrong, at least the linker will behave
102 in the same way that it used to do. */
103 return FALSE;
104
105 return TRUE;
106 }
107
108 /* Search the symbol table of the archive element of the archive ABFD
109 whose archive map contains a mention of SYMDEF, and determine if
110 the symbol is defined in this element. */
111 static bfd_boolean
112 elf_link_is_defined_archive_symbol (abfd, symdef)
113 bfd * abfd;
114 carsym * symdef;
115 {
116 Elf_Internal_Shdr * hdr;
117 bfd_size_type symcount;
118 bfd_size_type extsymcount;
119 bfd_size_type extsymoff;
120 Elf_Internal_Sym *isymbuf;
121 Elf_Internal_Sym *isym;
122 Elf_Internal_Sym *isymend;
123 bfd_boolean result;
124
125 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
126 if (abfd == (bfd *) NULL)
127 return FALSE;
128
129 if (! bfd_check_format (abfd, bfd_object))
130 return FALSE;
131
132 /* If we have already included the element containing this symbol in the
133 link then we do not need to include it again. Just claim that any symbol
134 it contains is not a definition, so that our caller will not decide to
135 (re)include this element. */
136 if (abfd->archive_pass)
137 return FALSE;
138
139 /* Select the appropriate symbol table. */
140 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
141 hdr = &elf_tdata (abfd)->symtab_hdr;
142 else
143 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
144
145 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
146
147 /* The sh_info field of the symtab header tells us where the
148 external symbols start. We don't care about the local symbols. */
149 if (elf_bad_symtab (abfd))
150 {
151 extsymcount = symcount;
152 extsymoff = 0;
153 }
154 else
155 {
156 extsymcount = symcount - hdr->sh_info;
157 extsymoff = hdr->sh_info;
158 }
159
160 if (extsymcount == 0)
161 return FALSE;
162
163 /* Read in the symbol table. */
164 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
165 NULL, NULL, NULL);
166 if (isymbuf == NULL)
167 return FALSE;
168
169 /* Scan the symbol table looking for SYMDEF. */
170 result = FALSE;
171 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
172 {
173 const char *name;
174
175 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
176 isym->st_name);
177 if (name == (const char *) NULL)
178 break;
179
180 if (strcmp (name, symdef->name) == 0)
181 {
182 result = is_global_data_symbol_definition (abfd, isym);
183 break;
184 }
185 }
186
187 free (isymbuf);
188
189 return result;
190 }
191 \f
192 /* Add symbols from an ELF archive file to the linker hash table. We
193 don't use _bfd_generic_link_add_archive_symbols because of a
194 problem which arises on UnixWare. The UnixWare libc.so is an
195 archive which includes an entry libc.so.1 which defines a bunch of
196 symbols. The libc.so archive also includes a number of other
197 object files, which also define symbols, some of which are the same
198 as those defined in libc.so.1. Correct linking requires that we
199 consider each object file in turn, and include it if it defines any
200 symbols we need. _bfd_generic_link_add_archive_symbols does not do
201 this; it looks through the list of undefined symbols, and includes
202 any object file which defines them. When this algorithm is used on
203 UnixWare, it winds up pulling in libc.so.1 early and defining a
204 bunch of symbols. This means that some of the other objects in the
205 archive are not included in the link, which is incorrect since they
206 precede libc.so.1 in the archive.
207
208 Fortunately, ELF archive handling is simpler than that done by
209 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
210 oddities. In ELF, if we find a symbol in the archive map, and the
211 symbol is currently undefined, we know that we must pull in that
212 object file.
213
214 Unfortunately, we do have to make multiple passes over the symbol
215 table until nothing further is resolved. */
216
217 static bfd_boolean
218 elf_link_add_archive_symbols (abfd, info)
219 bfd *abfd;
220 struct bfd_link_info *info;
221 {
222 symindex c;
223 bfd_boolean *defined = NULL;
224 bfd_boolean *included = NULL;
225 carsym *symdefs;
226 bfd_boolean loop;
227 bfd_size_type amt;
228
229 if (! bfd_has_map (abfd))
230 {
231 /* An empty archive is a special case. */
232 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
233 return TRUE;
234 bfd_set_error (bfd_error_no_armap);
235 return FALSE;
236 }
237
238 /* Keep track of all symbols we know to be already defined, and all
239 files we know to be already included. This is to speed up the
240 second and subsequent passes. */
241 c = bfd_ardata (abfd)->symdef_count;
242 if (c == 0)
243 return TRUE;
244 amt = c;
245 amt *= sizeof (bfd_boolean);
246 defined = (bfd_boolean *) bfd_zmalloc (amt);
247 included = (bfd_boolean *) bfd_zmalloc (amt);
248 if (defined == (bfd_boolean *) NULL || included == (bfd_boolean *) NULL)
249 goto error_return;
250
251 symdefs = bfd_ardata (abfd)->symdefs;
252
253 do
254 {
255 file_ptr last;
256 symindex i;
257 carsym *symdef;
258 carsym *symdefend;
259
260 loop = FALSE;
261 last = -1;
262
263 symdef = symdefs;
264 symdefend = symdef + c;
265 for (i = 0; symdef < symdefend; symdef++, i++)
266 {
267 struct elf_link_hash_entry *h;
268 bfd *element;
269 struct bfd_link_hash_entry *undefs_tail;
270 symindex mark;
271
272 if (defined[i] || included[i])
273 continue;
274 if (symdef->file_offset == last)
275 {
276 included[i] = TRUE;
277 continue;
278 }
279
280 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
281 FALSE, FALSE, FALSE);
282
283 if (h == NULL)
284 {
285 char *p, *copy;
286 size_t len, first;
287
288 /* If this is a default version (the name contains @@),
289 look up the symbol again with only one `@' as well
290 as without the version. The effect is that references
291 to the symbol with and without the version will be
292 matched by the default symbol in the archive. */
293
294 p = strchr (symdef->name, ELF_VER_CHR);
295 if (p == NULL || p[1] != ELF_VER_CHR)
296 continue;
297
298 /* First check with only one `@'. */
299 len = strlen (symdef->name);
300 copy = bfd_alloc (abfd, (bfd_size_type) len);
301 if (copy == NULL)
302 goto error_return;
303 first = p - symdef->name + 1;
304 memcpy (copy, symdef->name, first);
305 memcpy (copy + first, symdef->name + first + 1, len - first);
306
307 h = elf_link_hash_lookup (elf_hash_table (info), copy,
308 FALSE, FALSE, FALSE);
309
310 if (h == NULL)
311 {
312 /* We also need to check references to the symbol
313 without the version. */
314
315 copy[first - 1] = '\0';
316 h = elf_link_hash_lookup (elf_hash_table (info),
317 copy, FALSE, FALSE, FALSE);
318 }
319
320 bfd_release (abfd, copy);
321 }
322
323 if (h == NULL)
324 continue;
325
326 if (h->root.type == bfd_link_hash_common)
327 {
328 /* We currently have a common symbol. The archive map contains
329 a reference to this symbol, so we may want to include it. We
330 only want to include it however, if this archive element
331 contains a definition of the symbol, not just another common
332 declaration of it.
333
334 Unfortunately some archivers (including GNU ar) will put
335 declarations of common symbols into their archive maps, as
336 well as real definitions, so we cannot just go by the archive
337 map alone. Instead we must read in the element's symbol
338 table and check that to see what kind of symbol definition
339 this is. */
340 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
341 continue;
342 }
343 else if (h->root.type != bfd_link_hash_undefined)
344 {
345 if (h->root.type != bfd_link_hash_undefweak)
346 defined[i] = TRUE;
347 continue;
348 }
349
350 /* We need to include this archive member. */
351 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
352 if (element == (bfd *) NULL)
353 goto error_return;
354
355 if (! bfd_check_format (element, bfd_object))
356 goto error_return;
357
358 /* Doublecheck that we have not included this object
359 already--it should be impossible, but there may be
360 something wrong with the archive. */
361 if (element->archive_pass != 0)
362 {
363 bfd_set_error (bfd_error_bad_value);
364 goto error_return;
365 }
366 element->archive_pass = 1;
367
368 undefs_tail = info->hash->undefs_tail;
369
370 if (! (*info->callbacks->add_archive_element) (info, element,
371 symdef->name))
372 goto error_return;
373 if (! elf_link_add_object_symbols (element, info))
374 goto error_return;
375
376 /* If there are any new undefined symbols, we need to make
377 another pass through the archive in order to see whether
378 they can be defined. FIXME: This isn't perfect, because
379 common symbols wind up on undefs_tail and because an
380 undefined symbol which is defined later on in this pass
381 does not require another pass. This isn't a bug, but it
382 does make the code less efficient than it could be. */
383 if (undefs_tail != info->hash->undefs_tail)
384 loop = TRUE;
385
386 /* Look backward to mark all symbols from this object file
387 which we have already seen in this pass. */
388 mark = i;
389 do
390 {
391 included[mark] = TRUE;
392 if (mark == 0)
393 break;
394 --mark;
395 }
396 while (symdefs[mark].file_offset == symdef->file_offset);
397
398 /* We mark subsequent symbols from this object file as we go
399 on through the loop. */
400 last = symdef->file_offset;
401 }
402 }
403 while (loop);
404
405 free (defined);
406 free (included);
407
408 return TRUE;
409
410 error_return:
411 if (defined != (bfd_boolean *) NULL)
412 free (defined);
413 if (included != (bfd_boolean *) NULL)
414 free (included);
415 return FALSE;
416 }
417
418 /* Add symbols from an ELF object file to the linker hash table. */
419
420 static bfd_boolean
421 elf_link_add_object_symbols (abfd, info)
422 bfd *abfd;
423 struct bfd_link_info *info;
424 {
425 bfd_boolean (*add_symbol_hook)
426 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
427 const char **, flagword *, asection **, bfd_vma *));
428 bfd_boolean (*check_relocs)
429 PARAMS ((bfd *, struct bfd_link_info *, asection *,
430 const Elf_Internal_Rela *));
431 bfd_boolean collect;
432 Elf_Internal_Shdr *hdr;
433 bfd_size_type symcount;
434 bfd_size_type extsymcount;
435 bfd_size_type extsymoff;
436 struct elf_link_hash_entry **sym_hash;
437 bfd_boolean dynamic;
438 Elf_External_Versym *extversym = NULL;
439 Elf_External_Versym *ever;
440 struct elf_link_hash_entry *weaks;
441 struct elf_link_hash_entry **nondeflt_vers = NULL;
442 bfd_size_type nondeflt_vers_cnt = 0;
443 Elf_Internal_Sym *isymbuf = NULL;
444 Elf_Internal_Sym *isym;
445 Elf_Internal_Sym *isymend;
446 struct elf_backend_data *bed;
447 bfd_boolean dt_needed;
448 struct elf_link_hash_table * hash_table;
449 bfd_size_type amt;
450
451 hash_table = elf_hash_table (info);
452
453 bed = get_elf_backend_data (abfd);
454 add_symbol_hook = bed->elf_add_symbol_hook;
455 collect = bed->collect;
456
457 if ((abfd->flags & DYNAMIC) == 0)
458 dynamic = FALSE;
459 else
460 {
461 dynamic = TRUE;
462
463 /* You can't use -r against a dynamic object. Also, there's no
464 hope of using a dynamic object which does not exactly match
465 the format of the output file. */
466 if (info->relocatable || info->hash->creator != abfd->xvec)
467 {
468 bfd_set_error (bfd_error_invalid_operation);
469 goto error_return;
470 }
471 }
472
473 /* As a GNU extension, any input sections which are named
474 .gnu.warning.SYMBOL are treated as warning symbols for the given
475 symbol. This differs from .gnu.warning sections, which generate
476 warnings when they are included in an output file. */
477 if (info->executable)
478 {
479 asection *s;
480
481 for (s = abfd->sections; s != NULL; s = s->next)
482 {
483 const char *name;
484
485 name = bfd_get_section_name (abfd, s);
486 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
487 {
488 char *msg;
489 bfd_size_type sz;
490
491 name += sizeof ".gnu.warning." - 1;
492
493 /* If this is a shared object, then look up the symbol
494 in the hash table. If it is there, and it is already
495 been defined, then we will not be using the entry
496 from this shared object, so we don't need to warn.
497 FIXME: If we see the definition in a regular object
498 later on, we will warn, but we shouldn't. The only
499 fix is to keep track of what warnings we are supposed
500 to emit, and then handle them all at the end of the
501 link. */
502 if (dynamic && abfd->xvec == info->hash->creator)
503 {
504 struct elf_link_hash_entry *h;
505
506 h = elf_link_hash_lookup (hash_table, name,
507 FALSE, FALSE, TRUE);
508
509 /* FIXME: What about bfd_link_hash_common? */
510 if (h != NULL
511 && (h->root.type == bfd_link_hash_defined
512 || h->root.type == bfd_link_hash_defweak))
513 {
514 /* We don't want to issue this warning. Clobber
515 the section size so that the warning does not
516 get copied into the output file. */
517 s->_raw_size = 0;
518 continue;
519 }
520 }
521
522 sz = bfd_section_size (abfd, s);
523 msg = (char *) bfd_alloc (abfd, sz + 1);
524 if (msg == NULL)
525 goto error_return;
526
527 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
528 goto error_return;
529
530 msg[sz] = '\0';
531
532 if (! (_bfd_generic_link_add_one_symbol
533 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
534 FALSE, collect, (struct bfd_link_hash_entry **) NULL)))
535 goto error_return;
536
537 if (! info->relocatable)
538 {
539 /* Clobber the section size so that the warning does
540 not get copied into the output file. */
541 s->_raw_size = 0;
542 }
543 }
544 }
545 }
546
547 dt_needed = FALSE;
548 if (! dynamic)
549 {
550 /* If we are creating a shared library, create all the dynamic
551 sections immediately. We need to attach them to something,
552 so we attach them to this BFD, provided it is the right
553 format. FIXME: If there are no input BFD's of the same
554 format as the output, we can't make a shared library. */
555 if (info->shared
556 && is_elf_hash_table (info)
557 && ! hash_table->dynamic_sections_created
558 && abfd->xvec == info->hash->creator)
559 {
560 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
561 goto error_return;
562 }
563 }
564 else if (! is_elf_hash_table (info))
565 goto error_return;
566 else
567 {
568 asection *s;
569 bfd_boolean add_needed;
570 const char *name;
571 bfd_size_type oldsize;
572 bfd_size_type strindex;
573 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
574
575 /* ld --just-symbols and dynamic objects don't mix very well.
576 Test for --just-symbols by looking at info set up by
577 _bfd_elf_link_just_syms. */
578 if ((s = abfd->sections) != NULL
579 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
580 goto error_return;
581
582 /* Find the name to use in a DT_NEEDED entry that refers to this
583 object. If the object has a DT_SONAME entry, we use it.
584 Otherwise, if the generic linker stuck something in
585 elf_dt_name, we use that. Otherwise, we just use the file
586 name. If the generic linker put a null string into
587 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
588 there is a DT_SONAME entry. */
589 add_needed = TRUE;
590 name = bfd_get_filename (abfd);
591 if (elf_dt_name (abfd) != NULL)
592 {
593 name = elf_dt_name (abfd);
594 if (*name == '\0')
595 {
596 if (elf_dt_soname (abfd) != NULL)
597 dt_needed = TRUE;
598
599 add_needed = FALSE;
600 }
601 }
602 s = bfd_get_section_by_name (abfd, ".dynamic");
603 if (s != NULL)
604 {
605 Elf_External_Dyn *dynbuf = NULL;
606 Elf_External_Dyn *extdyn;
607 Elf_External_Dyn *extdynend;
608 int elfsec;
609 unsigned long shlink;
610
611 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
612 if (dynbuf == NULL)
613 goto error_return;
614
615 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
616 (file_ptr) 0, s->_raw_size))
617 goto error_free_dyn;
618
619 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
620 if (elfsec == -1)
621 goto error_free_dyn;
622 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
623
624 extdyn = dynbuf;
625 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
626 for (; extdyn < extdynend; extdyn++)
627 {
628 Elf_Internal_Dyn dyn;
629
630 elf_swap_dyn_in (abfd, extdyn, &dyn);
631 if (dyn.d_tag == DT_SONAME)
632 {
633 unsigned int tagv = dyn.d_un.d_val;
634 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
635 if (name == NULL)
636 goto error_free_dyn;
637 }
638 if (dyn.d_tag == DT_NEEDED)
639 {
640 struct bfd_link_needed_list *n, **pn;
641 char *fnm, *anm;
642 unsigned int tagv = dyn.d_un.d_val;
643
644 amt = sizeof (struct bfd_link_needed_list);
645 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
646 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
647 if (n == NULL || fnm == NULL)
648 goto error_free_dyn;
649 amt = strlen (fnm) + 1;
650 anm = bfd_alloc (abfd, amt);
651 if (anm == NULL)
652 goto error_free_dyn;
653 memcpy (anm, fnm, (size_t) amt);
654 n->name = anm;
655 n->by = abfd;
656 n->next = NULL;
657 for (pn = & hash_table->needed;
658 *pn != NULL;
659 pn = &(*pn)->next)
660 ;
661 *pn = n;
662 }
663 if (dyn.d_tag == DT_RUNPATH)
664 {
665 struct bfd_link_needed_list *n, **pn;
666 char *fnm, *anm;
667 unsigned int tagv = dyn.d_un.d_val;
668
669 amt = sizeof (struct bfd_link_needed_list);
670 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
671 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
672 if (n == NULL || fnm == NULL)
673 goto error_free_dyn;
674 amt = strlen (fnm) + 1;
675 anm = bfd_alloc (abfd, amt);
676 if (anm == NULL)
677 goto error_free_dyn;
678 memcpy (anm, fnm, (size_t) amt);
679 n->name = anm;
680 n->by = abfd;
681 n->next = NULL;
682 for (pn = & runpath;
683 *pn != NULL;
684 pn = &(*pn)->next)
685 ;
686 *pn = n;
687 }
688 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
689 if (!runpath && dyn.d_tag == DT_RPATH)
690 {
691 struct bfd_link_needed_list *n, **pn;
692 char *fnm, *anm;
693 unsigned int tagv = dyn.d_un.d_val;
694
695 amt = sizeof (struct bfd_link_needed_list);
696 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
697 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
698 if (n == NULL || fnm == NULL)
699 goto error_free_dyn;
700 amt = strlen (fnm) + 1;
701 anm = bfd_alloc (abfd, amt);
702 if (anm == NULL)
703 {
704 error_free_dyn:
705 free (dynbuf);
706 goto error_return;
707 }
708 memcpy (anm, fnm, (size_t) amt);
709 n->name = anm;
710 n->by = abfd;
711 n->next = NULL;
712 for (pn = & rpath;
713 *pn != NULL;
714 pn = &(*pn)->next)
715 ;
716 *pn = n;
717 }
718 }
719
720 free (dynbuf);
721 }
722
723 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
724 frees all more recently bfd_alloc'd blocks as well. */
725 if (runpath)
726 rpath = runpath;
727
728 if (rpath)
729 {
730 struct bfd_link_needed_list **pn;
731 for (pn = & hash_table->runpath;
732 *pn != NULL;
733 pn = &(*pn)->next)
734 ;
735 *pn = rpath;
736 }
737
738 /* We do not want to include any of the sections in a dynamic
739 object in the output file. We hack by simply clobbering the
740 list of sections in the BFD. This could be handled more
741 cleanly by, say, a new section flag; the existing
742 SEC_NEVER_LOAD flag is not the one we want, because that one
743 still implies that the section takes up space in the output
744 file. */
745 bfd_section_list_clear (abfd);
746
747 /* If this is the first dynamic object found in the link, create
748 the special sections required for dynamic linking. */
749 if (! hash_table->dynamic_sections_created)
750 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
751 goto error_return;
752
753 if (add_needed)
754 {
755 /* Add a DT_NEEDED entry for this dynamic object. */
756 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
757 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, FALSE);
758 if (strindex == (bfd_size_type) -1)
759 goto error_return;
760
761 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
762 {
763 asection *sdyn;
764 Elf_External_Dyn *dyncon, *dynconend;
765
766 /* The hash table size did not change, which means that
767 the dynamic object name was already entered. If we
768 have already included this dynamic object in the
769 link, just ignore it. There is no reason to include
770 a particular dynamic object more than once. */
771 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
772 BFD_ASSERT (sdyn != NULL);
773
774 dyncon = (Elf_External_Dyn *) sdyn->contents;
775 dynconend = (Elf_External_Dyn *) (sdyn->contents +
776 sdyn->_raw_size);
777 for (; dyncon < dynconend; dyncon++)
778 {
779 Elf_Internal_Dyn dyn;
780
781 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
782 if (dyn.d_tag == DT_NEEDED
783 && dyn.d_un.d_val == strindex)
784 {
785 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
786 return TRUE;
787 }
788 }
789 }
790
791 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
792 goto error_return;
793 }
794
795 /* Save the SONAME, if there is one, because sometimes the
796 linker emulation code will need to know it. */
797 if (*name == '\0')
798 name = basename (bfd_get_filename (abfd));
799 elf_dt_name (abfd) = name;
800 }
801
802 /* If this is a dynamic object, we always link against the .dynsym
803 symbol table, not the .symtab symbol table. The dynamic linker
804 will only see the .dynsym symbol table, so there is no reason to
805 look at .symtab for a dynamic object. */
806
807 if (! dynamic || elf_dynsymtab (abfd) == 0)
808 hdr = &elf_tdata (abfd)->symtab_hdr;
809 else
810 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
811
812 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
813
814 /* The sh_info field of the symtab header tells us where the
815 external symbols start. We don't care about the local symbols at
816 this point. */
817 if (elf_bad_symtab (abfd))
818 {
819 extsymcount = symcount;
820 extsymoff = 0;
821 }
822 else
823 {
824 extsymcount = symcount - hdr->sh_info;
825 extsymoff = hdr->sh_info;
826 }
827
828 sym_hash = NULL;
829 if (extsymcount != 0)
830 {
831 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
832 NULL, NULL, NULL);
833 if (isymbuf == NULL)
834 goto error_return;
835
836 /* We store a pointer to the hash table entry for each external
837 symbol. */
838 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
839 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
840 if (sym_hash == NULL)
841 goto error_free_sym;
842 elf_sym_hashes (abfd) = sym_hash;
843 }
844
845 if (dynamic)
846 {
847 /* Read in any version definitions. */
848 if (! _bfd_elf_slurp_version_tables (abfd))
849 goto error_free_sym;
850
851 /* Read in the symbol versions, but don't bother to convert them
852 to internal format. */
853 if (elf_dynversym (abfd) != 0)
854 {
855 Elf_Internal_Shdr *versymhdr;
856
857 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
858 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
859 if (extversym == NULL)
860 goto error_free_sym;
861 amt = versymhdr->sh_size;
862 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
863 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
864 goto error_free_vers;
865 }
866 }
867
868 weaks = NULL;
869
870 ever = extversym != NULL ? extversym + extsymoff : NULL;
871 for (isym = isymbuf, isymend = isymbuf + extsymcount;
872 isym < isymend;
873 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
874 {
875 int bind;
876 bfd_vma value;
877 asection *sec;
878 flagword flags;
879 const char *name;
880 struct elf_link_hash_entry *h;
881 bfd_boolean definition;
882 bfd_boolean size_change_ok;
883 bfd_boolean type_change_ok;
884 bfd_boolean new_weakdef;
885 bfd_boolean override;
886 unsigned int old_alignment;
887 bfd *old_bfd;
888
889 override = FALSE;
890
891 flags = BSF_NO_FLAGS;
892 sec = NULL;
893 value = isym->st_value;
894 *sym_hash = NULL;
895
896 bind = ELF_ST_BIND (isym->st_info);
897 if (bind == STB_LOCAL)
898 {
899 /* This should be impossible, since ELF requires that all
900 global symbols follow all local symbols, and that sh_info
901 point to the first global symbol. Unfortunatealy, Irix 5
902 screws this up. */
903 continue;
904 }
905 else if (bind == STB_GLOBAL)
906 {
907 if (isym->st_shndx != SHN_UNDEF
908 && isym->st_shndx != SHN_COMMON)
909 flags = BSF_GLOBAL;
910 }
911 else if (bind == STB_WEAK)
912 flags = BSF_WEAK;
913 else
914 {
915 /* Leave it up to the processor backend. */
916 }
917
918 if (isym->st_shndx == SHN_UNDEF)
919 sec = bfd_und_section_ptr;
920 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
921 {
922 sec = section_from_elf_index (abfd, isym->st_shndx);
923 if (sec == NULL)
924 sec = bfd_abs_section_ptr;
925 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
926 value -= sec->vma;
927 }
928 else if (isym->st_shndx == SHN_ABS)
929 sec = bfd_abs_section_ptr;
930 else if (isym->st_shndx == SHN_COMMON)
931 {
932 sec = bfd_com_section_ptr;
933 /* What ELF calls the size we call the value. What ELF
934 calls the value we call the alignment. */
935 value = isym->st_size;
936 }
937 else
938 {
939 /* Leave it up to the processor backend. */
940 }
941
942 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
943 isym->st_name);
944 if (name == (const char *) NULL)
945 goto error_free_vers;
946
947 if (isym->st_shndx == SHN_COMMON
948 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
949 {
950 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
951
952 if (tcomm == NULL)
953 {
954 tcomm = bfd_make_section (abfd, ".tcommon");
955 if (tcomm == NULL
956 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
957 | SEC_IS_COMMON
958 | SEC_LINKER_CREATED
959 | SEC_THREAD_LOCAL)))
960 goto error_free_vers;
961 }
962 sec = tcomm;
963 }
964 else if (add_symbol_hook)
965 {
966 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
967 &value))
968 goto error_free_vers;
969
970 /* The hook function sets the name to NULL if this symbol
971 should be skipped for some reason. */
972 if (name == (const char *) NULL)
973 continue;
974 }
975
976 /* Sanity check that all possibilities were handled. */
977 if (sec == (asection *) NULL)
978 {
979 bfd_set_error (bfd_error_bad_value);
980 goto error_free_vers;
981 }
982
983 if (bfd_is_und_section (sec)
984 || bfd_is_com_section (sec))
985 definition = FALSE;
986 else
987 definition = TRUE;
988
989 size_change_ok = FALSE;
990 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
991 old_alignment = 0;
992 old_bfd = NULL;
993
994 if (info->hash->creator->flavour == bfd_target_elf_flavour)
995 {
996 Elf_Internal_Versym iver;
997 unsigned int vernum = 0;
998 bfd_boolean skip;
999
1000 if (ever != NULL)
1001 {
1002 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1003 vernum = iver.vs_vers & VERSYM_VERSION;
1004
1005 /* If this is a hidden symbol, or if it is not version
1006 1, we append the version name to the symbol name.
1007 However, we do not modify a non-hidden absolute
1008 symbol, because it might be the version symbol
1009 itself. FIXME: What if it isn't? */
1010 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1011 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1012 {
1013 const char *verstr;
1014 size_t namelen, verlen, newlen;
1015 char *newname, *p;
1016
1017 if (isym->st_shndx != SHN_UNDEF)
1018 {
1019 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1020 {
1021 (*_bfd_error_handler)
1022 (_("%s: %s: invalid version %u (max %d)"),
1023 bfd_archive_filename (abfd), name, vernum,
1024 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1025 bfd_set_error (bfd_error_bad_value);
1026 goto error_free_vers;
1027 }
1028 else if (vernum > 1)
1029 verstr =
1030 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1031 else
1032 verstr = "";
1033 }
1034 else
1035 {
1036 /* We cannot simply test for the number of
1037 entries in the VERNEED section since the
1038 numbers for the needed versions do not start
1039 at 0. */
1040 Elf_Internal_Verneed *t;
1041
1042 verstr = NULL;
1043 for (t = elf_tdata (abfd)->verref;
1044 t != NULL;
1045 t = t->vn_nextref)
1046 {
1047 Elf_Internal_Vernaux *a;
1048
1049 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1050 {
1051 if (a->vna_other == vernum)
1052 {
1053 verstr = a->vna_nodename;
1054 break;
1055 }
1056 }
1057 if (a != NULL)
1058 break;
1059 }
1060 if (verstr == NULL)
1061 {
1062 (*_bfd_error_handler)
1063 (_("%s: %s: invalid needed version %d"),
1064 bfd_archive_filename (abfd), name, vernum);
1065 bfd_set_error (bfd_error_bad_value);
1066 goto error_free_vers;
1067 }
1068 }
1069
1070 namelen = strlen (name);
1071 verlen = strlen (verstr);
1072 newlen = namelen + verlen + 2;
1073 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1074 && isym->st_shndx != SHN_UNDEF)
1075 ++newlen;
1076
1077 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
1078 if (newname == NULL)
1079 goto error_free_vers;
1080 memcpy (newname, name, namelen);
1081 p = newname + namelen;
1082 *p++ = ELF_VER_CHR;
1083 /* If this is a defined non-hidden version symbol,
1084 we add another @ to the name. This indicates the
1085 default version of the symbol. */
1086 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1087 && isym->st_shndx != SHN_UNDEF)
1088 *p++ = ELF_VER_CHR;
1089 memcpy (p, verstr, verlen + 1);
1090
1091 name = newname;
1092 }
1093 }
1094
1095 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
1096 sym_hash, &skip, &override,
1097 &type_change_ok, &size_change_ok,
1098 dt_needed))
1099 goto error_free_vers;
1100
1101 if (skip)
1102 continue;
1103
1104 if (override)
1105 definition = FALSE;
1106
1107 h = *sym_hash;
1108 while (h->root.type == bfd_link_hash_indirect
1109 || h->root.type == bfd_link_hash_warning)
1110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1111
1112 /* Remember the old alignment if this is a common symbol, so
1113 that we don't reduce the alignment later on. We can't
1114 check later, because _bfd_generic_link_add_one_symbol
1115 will set a default for the alignment which we want to
1116 override. We also remember the old bfd where the existing
1117 definition comes from. */
1118 switch (h->root.type)
1119 {
1120 default:
1121 break;
1122
1123 case bfd_link_hash_defined:
1124 case bfd_link_hash_defweak:
1125 old_bfd = h->root.u.def.section->owner;
1126 break;
1127
1128 case bfd_link_hash_common:
1129 old_bfd = h->root.u.c.p->section->owner;
1130 old_alignment = h->root.u.c.p->alignment_power;
1131 break;
1132 }
1133
1134 if (elf_tdata (abfd)->verdef != NULL
1135 && ! override
1136 && vernum > 1
1137 && definition)
1138 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1139 }
1140
1141 if (! (_bfd_generic_link_add_one_symbol
1142 (info, abfd, name, flags, sec, value, (const char *) NULL,
1143 FALSE, collect, (struct bfd_link_hash_entry **) sym_hash)))
1144 goto error_free_vers;
1145
1146 h = *sym_hash;
1147 while (h->root.type == bfd_link_hash_indirect
1148 || h->root.type == bfd_link_hash_warning)
1149 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1150 *sym_hash = h;
1151
1152 new_weakdef = FALSE;
1153 if (dynamic
1154 && definition
1155 && (flags & BSF_WEAK) != 0
1156 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
1157 && info->hash->creator->flavour == bfd_target_elf_flavour
1158 && h->weakdef == NULL)
1159 {
1160 /* Keep a list of all weak defined non function symbols from
1161 a dynamic object, using the weakdef field. Later in this
1162 function we will set the weakdef field to the correct
1163 value. We only put non-function symbols from dynamic
1164 objects on this list, because that happens to be the only
1165 time we need to know the normal symbol corresponding to a
1166 weak symbol, and the information is time consuming to
1167 figure out. If the weakdef field is not already NULL,
1168 then this symbol was already defined by some previous
1169 dynamic object, and we will be using that previous
1170 definition anyhow. */
1171
1172 h->weakdef = weaks;
1173 weaks = h;
1174 new_weakdef = TRUE;
1175 }
1176
1177 /* Set the alignment of a common symbol. */
1178 if (isym->st_shndx == SHN_COMMON
1179 && h->root.type == bfd_link_hash_common)
1180 {
1181 unsigned int align;
1182
1183 align = bfd_log2 (isym->st_value);
1184 if (align > old_alignment
1185 /* Permit an alignment power of zero if an alignment of one
1186 is specified and no other alignments have been specified. */
1187 || (isym->st_value == 1 && old_alignment == 0))
1188 h->root.u.c.p->alignment_power = align;
1189 else
1190 h->root.u.c.p->alignment_power = old_alignment;
1191 }
1192
1193 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1194 {
1195 int old_flags;
1196 bfd_boolean dynsym;
1197 int new_flag;
1198
1199 /* Check the alignment when a common symbol is involved. This
1200 can change when a common symbol is overriden by a normal
1201 definition or a common symbol is ignored due to the old
1202 normal definition. We need to make sure the maximum
1203 alignment is maintained. */
1204 if ((old_alignment || isym->st_shndx == SHN_COMMON)
1205 && h->root.type != bfd_link_hash_common)
1206 {
1207 unsigned int common_align;
1208 unsigned int normal_align;
1209 unsigned int symbol_align;
1210 bfd *normal_bfd;
1211 bfd *common_bfd;
1212
1213 symbol_align = ffs (h->root.u.def.value) - 1;
1214 if ((h->root.u.def.section->owner->flags & DYNAMIC) == 0)
1215 {
1216 normal_align = h->root.u.def.section->alignment_power;
1217 if (normal_align > symbol_align)
1218 normal_align = symbol_align;
1219 }
1220 else
1221 normal_align = symbol_align;
1222
1223 if (old_alignment)
1224 {
1225 common_align = old_alignment;
1226 common_bfd = old_bfd;
1227 normal_bfd = abfd;
1228 }
1229 else
1230 {
1231 common_align = bfd_log2 (isym->st_value);
1232 common_bfd = abfd;
1233 normal_bfd = old_bfd;
1234 }
1235
1236 if (normal_align < common_align)
1237 (*_bfd_error_handler)
1238 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
1239 1 << normal_align,
1240 name,
1241 bfd_archive_filename (normal_bfd),
1242 1 << common_align,
1243 bfd_archive_filename (common_bfd));
1244 }
1245
1246 /* Remember the symbol size and type. */
1247 if (isym->st_size != 0
1248 && (definition || h->size == 0))
1249 {
1250 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
1251 (*_bfd_error_handler)
1252 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
1253 name, (unsigned long) h->size,
1254 bfd_archive_filename (old_bfd),
1255 (unsigned long) isym->st_size,
1256 bfd_archive_filename (abfd));
1257
1258 h->size = isym->st_size;
1259 }
1260
1261 /* If this is a common symbol, then we always want H->SIZE
1262 to be the size of the common symbol. The code just above
1263 won't fix the size if a common symbol becomes larger. We
1264 don't warn about a size change here, because that is
1265 covered by --warn-common. */
1266 if (h->root.type == bfd_link_hash_common)
1267 h->size = h->root.u.c.size;
1268
1269 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
1270 && (definition || h->type == STT_NOTYPE))
1271 {
1272 if (h->type != STT_NOTYPE
1273 && h->type != ELF_ST_TYPE (isym->st_info)
1274 && ! type_change_ok)
1275 (*_bfd_error_handler)
1276 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1277 name, h->type, ELF_ST_TYPE (isym->st_info),
1278 bfd_archive_filename (abfd));
1279
1280 h->type = ELF_ST_TYPE (isym->st_info);
1281 }
1282
1283 /* If st_other has a processor-specific meaning, specific
1284 code might be needed here. We never merge the visibility
1285 attribute with the one from a dynamic object. */
1286 if (isym->st_other != 0 && !dynamic)
1287 {
1288 unsigned char hvis, symvis, other, nvis;
1289
1290 /* Take the balance of OTHER from the definition. */
1291 other = (definition ? isym->st_other : h->other);
1292 other &= ~ ELF_ST_VISIBILITY (-1);
1293
1294 /* Combine visibilities, using the most constraining one. */
1295 hvis = ELF_ST_VISIBILITY (h->other);
1296 symvis = ELF_ST_VISIBILITY (isym->st_other);
1297 if (! hvis)
1298 nvis = symvis;
1299 else if (! symvis)
1300 nvis = hvis;
1301 else
1302 nvis = hvis < symvis ? hvis : symvis;
1303
1304 h->other = other | nvis;
1305 }
1306
1307 /* Set a flag in the hash table entry indicating the type of
1308 reference or definition we just found. Keep a count of
1309 the number of dynamic symbols we find. A dynamic symbol
1310 is one which is referenced or defined by both a regular
1311 object and a shared object. */
1312 old_flags = h->elf_link_hash_flags;
1313 dynsym = FALSE;
1314 if (! dynamic)
1315 {
1316 if (! definition)
1317 {
1318 new_flag = ELF_LINK_HASH_REF_REGULAR;
1319 if (bind != STB_WEAK)
1320 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
1321 }
1322 else
1323 new_flag = ELF_LINK_HASH_DEF_REGULAR;
1324 if (! info->executable
1325 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1326 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
1327 dynsym = TRUE;
1328 }
1329 else
1330 {
1331 if (! definition)
1332 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
1333 else
1334 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
1335 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1336 | ELF_LINK_HASH_REF_REGULAR)) != 0
1337 || (h->weakdef != NULL
1338 && ! new_weakdef
1339 && h->weakdef->dynindx != -1))
1340 dynsym = TRUE;
1341 }
1342
1343 h->elf_link_hash_flags |= new_flag;
1344
1345 /* Check to see if we need to add an indirect symbol for
1346 the default name. */
1347 if (definition || h->root.type == bfd_link_hash_common)
1348 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
1349 &sec, &value, &dynsym,
1350 override, dt_needed))
1351 goto error_free_vers;
1352
1353 if (definition && !dynamic)
1354 {
1355 char *p = strchr (name, ELF_VER_CHR);
1356 if (p != NULL && p[1] != ELF_VER_CHR)
1357 {
1358 /* Queue non-default versions so that .symver x, x@FOO
1359 aliases can be checked. */
1360 if (! nondeflt_vers)
1361 {
1362 amt = (isymend - isym + 1)
1363 * sizeof (struct elf_link_hash_entry *);
1364 nondeflt_vers = bfd_malloc (amt);
1365 }
1366 nondeflt_vers [nondeflt_vers_cnt++] = h;
1367 }
1368 }
1369
1370 if (dynsym && h->dynindx == -1)
1371 {
1372 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1373 goto error_free_vers;
1374 if (h->weakdef != NULL
1375 && ! new_weakdef
1376 && h->weakdef->dynindx == -1)
1377 {
1378 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1379 goto error_free_vers;
1380 }
1381 }
1382 else if (dynsym && h->dynindx != -1)
1383 /* If the symbol already has a dynamic index, but
1384 visibility says it should not be visible, turn it into
1385 a local symbol. */
1386 switch (ELF_ST_VISIBILITY (h->other))
1387 {
1388 case STV_INTERNAL:
1389 case STV_HIDDEN:
1390 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1391 break;
1392 }
1393
1394 if (dt_needed && definition
1395 && (h->elf_link_hash_flags
1396 & ELF_LINK_HASH_REF_REGULAR) != 0)
1397 {
1398 bfd_size_type oldsize;
1399 bfd_size_type strindex;
1400
1401 if (! is_elf_hash_table (info))
1402 goto error_free_vers;
1403
1404 /* The symbol from a DT_NEEDED object is referenced from
1405 the regular object to create a dynamic executable. We
1406 have to make sure there is a DT_NEEDED entry for it. */
1407
1408 dt_needed = FALSE;
1409 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1410 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
1411 elf_dt_soname (abfd), FALSE);
1412 if (strindex == (bfd_size_type) -1)
1413 goto error_free_vers;
1414
1415 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
1416 {
1417 asection *sdyn;
1418 Elf_External_Dyn *dyncon, *dynconend;
1419
1420 sdyn = bfd_get_section_by_name (hash_table->dynobj,
1421 ".dynamic");
1422 BFD_ASSERT (sdyn != NULL);
1423
1424 dyncon = (Elf_External_Dyn *) sdyn->contents;
1425 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1426 sdyn->_raw_size);
1427 for (; dyncon < dynconend; dyncon++)
1428 {
1429 Elf_Internal_Dyn dyn;
1430
1431 elf_swap_dyn_in (hash_table->dynobj,
1432 dyncon, &dyn);
1433 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
1434 dyn.d_un.d_val != strindex);
1435 }
1436 }
1437
1438 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
1439 goto error_free_vers;
1440 }
1441 }
1442 }
1443
1444 /* Now that all the symbols from this input file are created, handle
1445 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
1446 if (nondeflt_vers != NULL)
1447 {
1448 bfd_size_type cnt, symidx;
1449
1450 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
1451 {
1452 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
1453 char *shortname, *p;
1454
1455 p = strchr (h->root.root.string, ELF_VER_CHR);
1456 if (p == NULL
1457 || (h->root.type != bfd_link_hash_defined
1458 && h->root.type != bfd_link_hash_defweak))
1459 continue;
1460
1461 amt = p - h->root.root.string;
1462 shortname = bfd_malloc (amt + 1);
1463 memcpy (shortname, h->root.root.string, amt);
1464 shortname[amt] = '\0';
1465
1466 hi = (struct elf_link_hash_entry *)
1467 bfd_link_hash_lookup (info->hash, shortname,
1468 FALSE, FALSE, FALSE);
1469 if (hi != NULL
1470 && hi->root.type == h->root.type
1471 && hi->root.u.def.value == h->root.u.def.value
1472 && hi->root.u.def.section == h->root.u.def.section)
1473 {
1474 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1475 hi->root.type = bfd_link_hash_indirect;
1476 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
1477 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1478 sym_hash = elf_sym_hashes (abfd);
1479 if (sym_hash)
1480 for (symidx = 0; symidx < extsymcount; ++symidx)
1481 if (sym_hash[symidx] == hi)
1482 {
1483 sym_hash[symidx] = h;
1484 break;
1485 }
1486 }
1487 free (shortname);
1488 }
1489 free (nondeflt_vers);
1490 nondeflt_vers = NULL;
1491 }
1492
1493 if (extversym != NULL)
1494 {
1495 free (extversym);
1496 extversym = NULL;
1497 }
1498
1499 if (isymbuf != NULL)
1500 free (isymbuf);
1501 isymbuf = NULL;
1502
1503 /* Now set the weakdefs field correctly for all the weak defined
1504 symbols we found. The only way to do this is to search all the
1505 symbols. Since we only need the information for non functions in
1506 dynamic objects, that's the only time we actually put anything on
1507 the list WEAKS. We need this information so that if a regular
1508 object refers to a symbol defined weakly in a dynamic object, the
1509 real symbol in the dynamic object is also put in the dynamic
1510 symbols; we also must arrange for both symbols to point to the
1511 same memory location. We could handle the general case of symbol
1512 aliasing, but a general symbol alias can only be generated in
1513 assembler code, handling it correctly would be very time
1514 consuming, and other ELF linkers don't handle general aliasing
1515 either. */
1516 while (weaks != NULL)
1517 {
1518 struct elf_link_hash_entry *hlook;
1519 asection *slook;
1520 bfd_vma vlook;
1521 struct elf_link_hash_entry **hpp;
1522 struct elf_link_hash_entry **hppend;
1523
1524 hlook = weaks;
1525 weaks = hlook->weakdef;
1526 hlook->weakdef = NULL;
1527
1528 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1529 || hlook->root.type == bfd_link_hash_defweak
1530 || hlook->root.type == bfd_link_hash_common
1531 || hlook->root.type == bfd_link_hash_indirect);
1532 slook = hlook->root.u.def.section;
1533 vlook = hlook->root.u.def.value;
1534
1535 hpp = elf_sym_hashes (abfd);
1536 hppend = hpp + extsymcount;
1537 for (; hpp < hppend; hpp++)
1538 {
1539 struct elf_link_hash_entry *h;
1540
1541 h = *hpp;
1542 if (h != NULL && h != hlook
1543 && h->root.type == bfd_link_hash_defined
1544 && h->root.u.def.section == slook
1545 && h->root.u.def.value == vlook)
1546 {
1547 hlook->weakdef = h;
1548
1549 /* If the weak definition is in the list of dynamic
1550 symbols, make sure the real definition is put there
1551 as well. */
1552 if (hlook->dynindx != -1
1553 && h->dynindx == -1)
1554 {
1555 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1556 goto error_return;
1557 }
1558
1559 /* If the real definition is in the list of dynamic
1560 symbols, make sure the weak definition is put there
1561 as well. If we don't do this, then the dynamic
1562 loader might not merge the entries for the real
1563 definition and the weak definition. */
1564 if (h->dynindx != -1
1565 && hlook->dynindx == -1)
1566 {
1567 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1568 goto error_return;
1569 }
1570 break;
1571 }
1572 }
1573 }
1574
1575 /* If this object is the same format as the output object, and it is
1576 not a shared library, then let the backend look through the
1577 relocs.
1578
1579 This is required to build global offset table entries and to
1580 arrange for dynamic relocs. It is not required for the
1581 particular common case of linking non PIC code, even when linking
1582 against shared libraries, but unfortunately there is no way of
1583 knowing whether an object file has been compiled PIC or not.
1584 Looking through the relocs is not particularly time consuming.
1585 The problem is that we must either (1) keep the relocs in memory,
1586 which causes the linker to require additional runtime memory or
1587 (2) read the relocs twice from the input file, which wastes time.
1588 This would be a good case for using mmap.
1589
1590 I have no idea how to handle linking PIC code into a file of a
1591 different format. It probably can't be done. */
1592 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1593 if (! dynamic
1594 && abfd->xvec == info->hash->creator
1595 && check_relocs != NULL)
1596 {
1597 asection *o;
1598
1599 for (o = abfd->sections; o != NULL; o = o->next)
1600 {
1601 Elf_Internal_Rela *internal_relocs;
1602 bfd_boolean ok;
1603
1604 if ((o->flags & SEC_RELOC) == 0
1605 || o->reloc_count == 0
1606 || ((info->strip == strip_all || info->strip == strip_debugger)
1607 && (o->flags & SEC_DEBUGGING) != 0)
1608 || bfd_is_abs_section (o->output_section))
1609 continue;
1610
1611 internal_relocs
1612 = _bfd_elf_link_read_relocs (abfd, o, (PTR) NULL,
1613 (Elf_Internal_Rela *) NULL,
1614 info->keep_memory);
1615 if (internal_relocs == NULL)
1616 goto error_return;
1617
1618 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1619
1620 if (elf_section_data (o)->relocs != internal_relocs)
1621 free (internal_relocs);
1622
1623 if (! ok)
1624 goto error_return;
1625 }
1626 }
1627
1628 /* If this is a non-traditional link, try to optimize the handling
1629 of the .stab/.stabstr sections. */
1630 if (! dynamic
1631 && ! info->traditional_format
1632 && info->hash->creator->flavour == bfd_target_elf_flavour
1633 && is_elf_hash_table (info)
1634 && (info->strip != strip_all && info->strip != strip_debugger))
1635 {
1636 asection *stab, *stabstr;
1637
1638 stab = bfd_get_section_by_name (abfd, ".stab");
1639 if (stab != NULL
1640 && (stab->flags & SEC_MERGE) == 0
1641 && !bfd_is_abs_section (stab->output_section))
1642 {
1643 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1644
1645 if (stabstr != NULL)
1646 {
1647 struct bfd_elf_section_data *secdata;
1648
1649 secdata = elf_section_data (stab);
1650 if (! _bfd_link_section_stabs (abfd,
1651 & hash_table->stab_info,
1652 stab, stabstr,
1653 &secdata->sec_info))
1654 goto error_return;
1655 if (secdata->sec_info)
1656 stab->sec_info_type = ELF_INFO_TYPE_STABS;
1657 }
1658 }
1659 }
1660
1661 if (! info->relocatable && ! dynamic
1662 && is_elf_hash_table (info))
1663 {
1664 asection *s;
1665
1666 for (s = abfd->sections; s != NULL; s = s->next)
1667 if ((s->flags & SEC_MERGE) != 0
1668 && !bfd_is_abs_section (s->output_section))
1669 {
1670 struct bfd_elf_section_data *secdata;
1671
1672 secdata = elf_section_data (s);
1673 if (! _bfd_merge_section (abfd,
1674 & hash_table->merge_info,
1675 s, &secdata->sec_info))
1676 goto error_return;
1677 else if (secdata->sec_info)
1678 s->sec_info_type = ELF_INFO_TYPE_MERGE;
1679 }
1680 }
1681
1682 if (is_elf_hash_table (info))
1683 {
1684 /* Add this bfd to the loaded list. */
1685 struct elf_link_loaded_list *n;
1686
1687 n = ((struct elf_link_loaded_list *)
1688 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
1689 if (n == NULL)
1690 goto error_return;
1691 n->abfd = abfd;
1692 n->next = hash_table->loaded;
1693 hash_table->loaded = n;
1694 }
1695
1696 return TRUE;
1697
1698 error_free_vers:
1699 if (nondeflt_vers != NULL)
1700 free (nondeflt_vers);
1701 if (extversym != NULL)
1702 free (extversym);
1703 error_free_sym:
1704 if (isymbuf != NULL)
1705 free (isymbuf);
1706 error_return:
1707 return FALSE;
1708 }
1709
1710 /* Add an entry to the .dynamic table. */
1711
1712 bfd_boolean
1713 elf_add_dynamic_entry (info, tag, val)
1714 struct bfd_link_info *info;
1715 bfd_vma tag;
1716 bfd_vma val;
1717 {
1718 Elf_Internal_Dyn dyn;
1719 bfd *dynobj;
1720 asection *s;
1721 bfd_size_type newsize;
1722 bfd_byte *newcontents;
1723
1724 if (! is_elf_hash_table (info))
1725 return FALSE;
1726
1727 dynobj = elf_hash_table (info)->dynobj;
1728
1729 s = bfd_get_section_by_name (dynobj, ".dynamic");
1730 BFD_ASSERT (s != NULL);
1731
1732 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1733 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
1734 if (newcontents == NULL)
1735 return FALSE;
1736
1737 dyn.d_tag = tag;
1738 dyn.d_un.d_val = val;
1739 elf_swap_dyn_out (dynobj, &dyn,
1740 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1741
1742 s->_raw_size = newsize;
1743 s->contents = newcontents;
1744
1745 return TRUE;
1746 }
1747 \f
1748 /* Array used to determine the number of hash table buckets to use
1749 based on the number of symbols there are. If there are fewer than
1750 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1751 fewer than 37 we use 17 buckets, and so forth. We never use more
1752 than 32771 buckets. */
1753
1754 static const size_t elf_buckets[] =
1755 {
1756 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1757 16411, 32771, 0
1758 };
1759
1760 /* Compute bucket count for hashing table. We do not use a static set
1761 of possible tables sizes anymore. Instead we determine for all
1762 possible reasonable sizes of the table the outcome (i.e., the
1763 number of collisions etc) and choose the best solution. The
1764 weighting functions are not too simple to allow the table to grow
1765 without bounds. Instead one of the weighting factors is the size.
1766 Therefore the result is always a good payoff between few collisions
1767 (= short chain lengths) and table size. */
1768 static size_t
1769 compute_bucket_count (info)
1770 struct bfd_link_info *info;
1771 {
1772 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
1773 size_t best_size = 0;
1774 unsigned long int *hashcodes;
1775 unsigned long int *hashcodesp;
1776 unsigned long int i;
1777 bfd_size_type amt;
1778
1779 /* Compute the hash values for all exported symbols. At the same
1780 time store the values in an array so that we could use them for
1781 optimizations. */
1782 amt = dynsymcount;
1783 amt *= sizeof (unsigned long int);
1784 hashcodes = (unsigned long int *) bfd_malloc (amt);
1785 if (hashcodes == NULL)
1786 return 0;
1787 hashcodesp = hashcodes;
1788
1789 /* Put all hash values in HASHCODES. */
1790 elf_link_hash_traverse (elf_hash_table (info),
1791 elf_collect_hash_codes, &hashcodesp);
1792
1793 /* We have a problem here. The following code to optimize the table
1794 size requires an integer type with more the 32 bits. If
1795 BFD_HOST_U_64_BIT is set we know about such a type. */
1796 #ifdef BFD_HOST_U_64_BIT
1797 if (info->optimize)
1798 {
1799 unsigned long int nsyms = hashcodesp - hashcodes;
1800 size_t minsize;
1801 size_t maxsize;
1802 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
1803 unsigned long int *counts ;
1804
1805 /* Possible optimization parameters: if we have NSYMS symbols we say
1806 that the hashing table must at least have NSYMS/4 and at most
1807 2*NSYMS buckets. */
1808 minsize = nsyms / 4;
1809 if (minsize == 0)
1810 minsize = 1;
1811 best_size = maxsize = nsyms * 2;
1812
1813 /* Create array where we count the collisions in. We must use bfd_malloc
1814 since the size could be large. */
1815 amt = maxsize;
1816 amt *= sizeof (unsigned long int);
1817 counts = (unsigned long int *) bfd_malloc (amt);
1818 if (counts == NULL)
1819 {
1820 free (hashcodes);
1821 return 0;
1822 }
1823
1824 /* Compute the "optimal" size for the hash table. The criteria is a
1825 minimal chain length. The minor criteria is (of course) the size
1826 of the table. */
1827 for (i = minsize; i < maxsize; ++i)
1828 {
1829 /* Walk through the array of hashcodes and count the collisions. */
1830 BFD_HOST_U_64_BIT max;
1831 unsigned long int j;
1832 unsigned long int fact;
1833
1834 memset (counts, '\0', i * sizeof (unsigned long int));
1835
1836 /* Determine how often each hash bucket is used. */
1837 for (j = 0; j < nsyms; ++j)
1838 ++counts[hashcodes[j] % i];
1839
1840 /* For the weight function we need some information about the
1841 pagesize on the target. This is information need not be 100%
1842 accurate. Since this information is not available (so far) we
1843 define it here to a reasonable default value. If it is crucial
1844 to have a better value some day simply define this value. */
1845 # ifndef BFD_TARGET_PAGESIZE
1846 # define BFD_TARGET_PAGESIZE (4096)
1847 # endif
1848
1849 /* We in any case need 2 + NSYMS entries for the size values and
1850 the chains. */
1851 max = (2 + nsyms) * (ARCH_SIZE / 8);
1852
1853 # if 1
1854 /* Variant 1: optimize for short chains. We add the squares
1855 of all the chain lengths (which favous many small chain
1856 over a few long chains). */
1857 for (j = 0; j < i; ++j)
1858 max += counts[j] * counts[j];
1859
1860 /* This adds penalties for the overall size of the table. */
1861 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1862 max *= fact * fact;
1863 # else
1864 /* Variant 2: Optimize a lot more for small table. Here we
1865 also add squares of the size but we also add penalties for
1866 empty slots (the +1 term). */
1867 for (j = 0; j < i; ++j)
1868 max += (1 + counts[j]) * (1 + counts[j]);
1869
1870 /* The overall size of the table is considered, but not as
1871 strong as in variant 1, where it is squared. */
1872 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
1873 max *= fact;
1874 # endif
1875
1876 /* Compare with current best results. */
1877 if (max < best_chlen)
1878 {
1879 best_chlen = max;
1880 best_size = i;
1881 }
1882 }
1883
1884 free (counts);
1885 }
1886 else
1887 #endif /* defined (BFD_HOST_U_64_BIT) */
1888 {
1889 /* This is the fallback solution if no 64bit type is available or if we
1890 are not supposed to spend much time on optimizations. We select the
1891 bucket count using a fixed set of numbers. */
1892 for (i = 0; elf_buckets[i] != 0; i++)
1893 {
1894 best_size = elf_buckets[i];
1895 if (dynsymcount < elf_buckets[i + 1])
1896 break;
1897 }
1898 }
1899
1900 /* Free the arrays we needed. */
1901 free (hashcodes);
1902
1903 return best_size;
1904 }
1905
1906 /* Set up the sizes and contents of the ELF dynamic sections. This is
1907 called by the ELF linker emulation before_allocation routine. We
1908 must set the sizes of the sections before the linker sets the
1909 addresses of the various sections. */
1910
1911 bfd_boolean
1912 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
1913 filter_shlib,
1914 auxiliary_filters, info, sinterpptr,
1915 verdefs)
1916 bfd *output_bfd;
1917 const char *soname;
1918 const char *rpath;
1919 const char *filter_shlib;
1920 const char * const *auxiliary_filters;
1921 struct bfd_link_info *info;
1922 asection **sinterpptr;
1923 struct bfd_elf_version_tree *verdefs;
1924 {
1925 bfd_size_type soname_indx;
1926 bfd *dynobj;
1927 struct elf_backend_data *bed;
1928 struct elf_assign_sym_version_info asvinfo;
1929
1930 *sinterpptr = NULL;
1931
1932 soname_indx = (bfd_size_type) -1;
1933
1934 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1935 return TRUE;
1936
1937 if (! is_elf_hash_table (info))
1938 return TRUE;
1939
1940 if (info->execstack)
1941 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
1942 else if (info->noexecstack)
1943 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
1944 else
1945 {
1946 bfd *inputobj;
1947 asection *notesec = NULL;
1948 int exec = 0;
1949
1950 for (inputobj = info->input_bfds;
1951 inputobj;
1952 inputobj = inputobj->link_next)
1953 {
1954 asection *s;
1955
1956 if (inputobj->flags & DYNAMIC)
1957 continue;
1958 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
1959 if (s)
1960 {
1961 if (s->flags & SEC_CODE)
1962 exec = PF_X;
1963 notesec = s;
1964 }
1965 else
1966 exec = PF_X;
1967 }
1968 if (notesec)
1969 {
1970 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
1971 if (exec && info->relocatable
1972 && notesec->output_section != bfd_abs_section_ptr)
1973 notesec->output_section->flags |= SEC_CODE;
1974 }
1975 }
1976
1977 /* Any syms created from now on start with -1 in
1978 got.refcount/offset and plt.refcount/offset. */
1979 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
1980
1981 /* The backend may have to create some sections regardless of whether
1982 we're dynamic or not. */
1983 bed = get_elf_backend_data (output_bfd);
1984 if (bed->elf_backend_always_size_sections
1985 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1986 return FALSE;
1987
1988 dynobj = elf_hash_table (info)->dynobj;
1989
1990 /* If there were no dynamic objects in the link, there is nothing to
1991 do here. */
1992 if (dynobj == NULL)
1993 return TRUE;
1994
1995 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
1996 return FALSE;
1997
1998 if (elf_hash_table (info)->dynamic_sections_created)
1999 {
2000 struct elf_info_failed eif;
2001 struct elf_link_hash_entry *h;
2002 asection *dynstr;
2003 struct bfd_elf_version_tree *t;
2004 struct bfd_elf_version_expr *d;
2005 bfd_boolean all_defined;
2006
2007 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
2008 BFD_ASSERT (*sinterpptr != NULL || info->shared);
2009
2010 if (soname != NULL)
2011 {
2012 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2013 soname, TRUE);
2014 if (soname_indx == (bfd_size_type) -1
2015 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
2016 soname_indx))
2017 return FALSE;
2018 }
2019
2020 if (info->symbolic)
2021 {
2022 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
2023 (bfd_vma) 0))
2024 return FALSE;
2025 info->flags |= DF_SYMBOLIC;
2026 }
2027
2028 if (rpath != NULL)
2029 {
2030 bfd_size_type indx;
2031
2032 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
2033 TRUE);
2034 if (info->new_dtags)
2035 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
2036 if (indx == (bfd_size_type) -1
2037 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
2038 || (info->new_dtags
2039 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
2040 indx)))
2041 return FALSE;
2042 }
2043
2044 if (filter_shlib != NULL)
2045 {
2046 bfd_size_type indx;
2047
2048 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2049 filter_shlib, TRUE);
2050 if (indx == (bfd_size_type) -1
2051 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
2052 return FALSE;
2053 }
2054
2055 if (auxiliary_filters != NULL)
2056 {
2057 const char * const *p;
2058
2059 for (p = auxiliary_filters; *p != NULL; p++)
2060 {
2061 bfd_size_type indx;
2062
2063 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2064 *p, TRUE);
2065 if (indx == (bfd_size_type) -1
2066 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
2067 indx))
2068 return FALSE;
2069 }
2070 }
2071
2072 eif.info = info;
2073 eif.verdefs = verdefs;
2074 eif.failed = FALSE;
2075
2076 /* If we are supposed to export all symbols into the dynamic symbol
2077 table (this is not the normal case), then do so. */
2078 if (info->export_dynamic)
2079 {
2080 elf_link_hash_traverse (elf_hash_table (info),
2081 _bfd_elf_export_symbol,
2082 (PTR) &eif);
2083 if (eif.failed)
2084 return FALSE;
2085 }
2086
2087 /* Make all global versions with definiton. */
2088 for (t = verdefs; t != NULL; t = t->next)
2089 for (d = t->globals; d != NULL; d = d->next)
2090 if (!d->symver && strchr (d->pattern, '*') == NULL)
2091 {
2092 const char *verstr, *name;
2093 size_t namelen, verlen, newlen;
2094 char *newname, *p;
2095 struct elf_link_hash_entry *newh;
2096
2097 name = d->pattern;
2098 namelen = strlen (name);
2099 verstr = t->name;
2100 verlen = strlen (verstr);
2101 newlen = namelen + verlen + 3;
2102
2103 newname = (char *) bfd_malloc ((bfd_size_type) newlen);
2104 if (newname == NULL)
2105 return FALSE;
2106 memcpy (newname, name, namelen);
2107
2108 /* Check the hidden versioned definition. */
2109 p = newname + namelen;
2110 *p++ = ELF_VER_CHR;
2111 memcpy (p, verstr, verlen + 1);
2112 newh = elf_link_hash_lookup (elf_hash_table (info),
2113 newname, FALSE, FALSE,
2114 FALSE);
2115 if (newh == NULL
2116 || (newh->root.type != bfd_link_hash_defined
2117 && newh->root.type != bfd_link_hash_defweak))
2118 {
2119 /* Check the default versioned definition. */
2120 *p++ = ELF_VER_CHR;
2121 memcpy (p, verstr, verlen + 1);
2122 newh = elf_link_hash_lookup (elf_hash_table (info),
2123 newname, FALSE, FALSE,
2124 FALSE);
2125 }
2126 free (newname);
2127
2128 /* Mark this version if there is a definition and it is
2129 not defined in a shared object. */
2130 if (newh != NULL
2131 && ((newh->elf_link_hash_flags
2132 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
2133 && (newh->root.type == bfd_link_hash_defined
2134 || newh->root.type == bfd_link_hash_defweak))
2135 d->symver = 1;
2136 }
2137
2138 /* Attach all the symbols to their version information. */
2139 asvinfo.output_bfd = output_bfd;
2140 asvinfo.info = info;
2141 asvinfo.verdefs = verdefs;
2142 asvinfo.failed = FALSE;
2143
2144 elf_link_hash_traverse (elf_hash_table (info),
2145 _bfd_elf_link_assign_sym_version,
2146 (PTR) &asvinfo);
2147 if (asvinfo.failed)
2148 return FALSE;
2149
2150 if (!info->allow_undefined_version)
2151 {
2152 /* Check if all global versions have a definiton. */
2153 all_defined = TRUE;
2154 for (t = verdefs; t != NULL; t = t->next)
2155 for (d = t->globals; d != NULL; d = d->next)
2156 if (!d->symver && !d->script
2157 && strchr (d->pattern, '*') == NULL)
2158 {
2159 (*_bfd_error_handler)
2160 (_("%s: undefined version: %s"),
2161 d->pattern, t->name);
2162 all_defined = FALSE;
2163 }
2164
2165 if (!all_defined)
2166 {
2167 bfd_set_error (bfd_error_bad_value);
2168 return FALSE;
2169 }
2170 }
2171
2172 /* Find all symbols which were defined in a dynamic object and make
2173 the backend pick a reasonable value for them. */
2174 elf_link_hash_traverse (elf_hash_table (info),
2175 _bfd_elf_adjust_dynamic_symbol,
2176 (PTR) &eif);
2177 if (eif.failed)
2178 return FALSE;
2179
2180 /* Add some entries to the .dynamic section. We fill in some of the
2181 values later, in elf_bfd_final_link, but we must add the entries
2182 now so that we know the final size of the .dynamic section. */
2183
2184 /* If there are initialization and/or finalization functions to
2185 call then add the corresponding DT_INIT/DT_FINI entries. */
2186 h = (info->init_function
2187 ? elf_link_hash_lookup (elf_hash_table (info),
2188 info->init_function, FALSE,
2189 FALSE, FALSE)
2190 : NULL);
2191 if (h != NULL
2192 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2193 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2194 {
2195 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
2196 return FALSE;
2197 }
2198 h = (info->fini_function
2199 ? elf_link_hash_lookup (elf_hash_table (info),
2200 info->fini_function, FALSE,
2201 FALSE, FALSE)
2202 : NULL);
2203 if (h != NULL
2204 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
2205 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
2206 {
2207 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
2208 return FALSE;
2209 }
2210
2211 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
2212 {
2213 /* DT_PREINIT_ARRAY is not allowed in shared library. */
2214 if (! info->executable)
2215 {
2216 bfd *sub;
2217 asection *o;
2218
2219 for (sub = info->input_bfds; sub != NULL;
2220 sub = sub->link_next)
2221 for (o = sub->sections; o != NULL; o = o->next)
2222 if (elf_section_data (o)->this_hdr.sh_type
2223 == SHT_PREINIT_ARRAY)
2224 {
2225 (*_bfd_error_handler)
2226 (_("%s: .preinit_array section is not allowed in DSO"),
2227 bfd_archive_filename (sub));
2228 break;
2229 }
2230
2231 bfd_set_error (bfd_error_nonrepresentable_section);
2232 return FALSE;
2233 }
2234
2235 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
2236 (bfd_vma) 0)
2237 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
2238 (bfd_vma) 0))
2239 return FALSE;
2240 }
2241 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
2242 {
2243 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
2244 (bfd_vma) 0)
2245 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
2246 (bfd_vma) 0))
2247 return FALSE;
2248 }
2249 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
2250 {
2251 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
2252 (bfd_vma) 0)
2253 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
2254 (bfd_vma) 0))
2255 return FALSE;
2256 }
2257
2258 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
2259 /* If .dynstr is excluded from the link, we don't want any of
2260 these tags. Strictly, we should be checking each section
2261 individually; This quick check covers for the case where
2262 someone does a /DISCARD/ : { *(*) }. */
2263 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
2264 {
2265 bfd_size_type strsize;
2266
2267 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2268 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
2269 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
2270 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
2271 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
2272 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
2273 (bfd_vma) sizeof (Elf_External_Sym)))
2274 return FALSE;
2275 }
2276 }
2277
2278 /* The backend must work out the sizes of all the other dynamic
2279 sections. */
2280 if (bed->elf_backend_size_dynamic_sections
2281 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
2282 return FALSE;
2283
2284 if (elf_hash_table (info)->dynamic_sections_created)
2285 {
2286 bfd_size_type dynsymcount;
2287 asection *s;
2288 size_t bucketcount = 0;
2289 size_t hash_entry_size;
2290 unsigned int dtagcount;
2291
2292 /* Set up the version definition section. */
2293 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2294 BFD_ASSERT (s != NULL);
2295
2296 /* We may have created additional version definitions if we are
2297 just linking a regular application. */
2298 verdefs = asvinfo.verdefs;
2299
2300 /* Skip anonymous version tag. */
2301 if (verdefs != NULL && verdefs->vernum == 0)
2302 verdefs = verdefs->next;
2303
2304 if (verdefs == NULL)
2305 _bfd_strip_section_from_output (info, s);
2306 else
2307 {
2308 unsigned int cdefs;
2309 bfd_size_type size;
2310 struct bfd_elf_version_tree *t;
2311 bfd_byte *p;
2312 Elf_Internal_Verdef def;
2313 Elf_Internal_Verdaux defaux;
2314
2315 cdefs = 0;
2316 size = 0;
2317
2318 /* Make space for the base version. */
2319 size += sizeof (Elf_External_Verdef);
2320 size += sizeof (Elf_External_Verdaux);
2321 ++cdefs;
2322
2323 for (t = verdefs; t != NULL; t = t->next)
2324 {
2325 struct bfd_elf_version_deps *n;
2326
2327 size += sizeof (Elf_External_Verdef);
2328 size += sizeof (Elf_External_Verdaux);
2329 ++cdefs;
2330
2331 for (n = t->deps; n != NULL; n = n->next)
2332 size += sizeof (Elf_External_Verdaux);
2333 }
2334
2335 s->_raw_size = size;
2336 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2337 if (s->contents == NULL && s->_raw_size != 0)
2338 return FALSE;
2339
2340 /* Fill in the version definition section. */
2341
2342 p = s->contents;
2343
2344 def.vd_version = VER_DEF_CURRENT;
2345 def.vd_flags = VER_FLG_BASE;
2346 def.vd_ndx = 1;
2347 def.vd_cnt = 1;
2348 def.vd_aux = sizeof (Elf_External_Verdef);
2349 def.vd_next = (sizeof (Elf_External_Verdef)
2350 + sizeof (Elf_External_Verdaux));
2351
2352 if (soname_indx != (bfd_size_type) -1)
2353 {
2354 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2355 soname_indx);
2356 def.vd_hash = bfd_elf_hash (soname);
2357 defaux.vda_name = soname_indx;
2358 }
2359 else
2360 {
2361 const char *name;
2362 bfd_size_type indx;
2363
2364 name = basename (output_bfd->filename);
2365 def.vd_hash = bfd_elf_hash (name);
2366 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2367 name, FALSE);
2368 if (indx == (bfd_size_type) -1)
2369 return FALSE;
2370 defaux.vda_name = indx;
2371 }
2372 defaux.vda_next = 0;
2373
2374 _bfd_elf_swap_verdef_out (output_bfd, &def,
2375 (Elf_External_Verdef *) p);
2376 p += sizeof (Elf_External_Verdef);
2377 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2378 (Elf_External_Verdaux *) p);
2379 p += sizeof (Elf_External_Verdaux);
2380
2381 for (t = verdefs; t != NULL; t = t->next)
2382 {
2383 unsigned int cdeps;
2384 struct bfd_elf_version_deps *n;
2385 struct elf_link_hash_entry *h;
2386 struct bfd_link_hash_entry *bh;
2387
2388 cdeps = 0;
2389 for (n = t->deps; n != NULL; n = n->next)
2390 ++cdeps;
2391
2392 /* Add a symbol representing this version. */
2393 bh = NULL;
2394 if (! (_bfd_generic_link_add_one_symbol
2395 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
2396 (bfd_vma) 0, (const char *) NULL, FALSE,
2397 get_elf_backend_data (dynobj)->collect, &bh)))
2398 return FALSE;
2399 h = (struct elf_link_hash_entry *) bh;
2400 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
2401 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2402 h->type = STT_OBJECT;
2403 h->verinfo.vertree = t;
2404
2405 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2406 return FALSE;
2407
2408 def.vd_version = VER_DEF_CURRENT;
2409 def.vd_flags = 0;
2410 if (t->globals == NULL && t->locals == NULL && ! t->used)
2411 def.vd_flags |= VER_FLG_WEAK;
2412 def.vd_ndx = t->vernum + 1;
2413 def.vd_cnt = cdeps + 1;
2414 def.vd_hash = bfd_elf_hash (t->name);
2415 def.vd_aux = sizeof (Elf_External_Verdef);
2416 if (t->next != NULL)
2417 def.vd_next = (sizeof (Elf_External_Verdef)
2418 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
2419 else
2420 def.vd_next = 0;
2421
2422 _bfd_elf_swap_verdef_out (output_bfd, &def,
2423 (Elf_External_Verdef *) p);
2424 p += sizeof (Elf_External_Verdef);
2425
2426 defaux.vda_name = h->dynstr_index;
2427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2428 h->dynstr_index);
2429 if (t->deps == NULL)
2430 defaux.vda_next = 0;
2431 else
2432 defaux.vda_next = sizeof (Elf_External_Verdaux);
2433 t->name_indx = defaux.vda_name;
2434
2435 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2436 (Elf_External_Verdaux *) p);
2437 p += sizeof (Elf_External_Verdaux);
2438
2439 for (n = t->deps; n != NULL; n = n->next)
2440 {
2441 if (n->version_needed == NULL)
2442 {
2443 /* This can happen if there was an error in the
2444 version script. */
2445 defaux.vda_name = 0;
2446 }
2447 else
2448 {
2449 defaux.vda_name = n->version_needed->name_indx;
2450 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
2451 defaux.vda_name);
2452 }
2453 if (n->next == NULL)
2454 defaux.vda_next = 0;
2455 else
2456 defaux.vda_next = sizeof (Elf_External_Verdaux);
2457
2458 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2459 (Elf_External_Verdaux *) p);
2460 p += sizeof (Elf_External_Verdaux);
2461 }
2462 }
2463
2464 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
2465 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
2466 (bfd_vma) cdefs))
2467 return FALSE;
2468
2469 elf_tdata (output_bfd)->cverdefs = cdefs;
2470 }
2471
2472 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
2473 {
2474 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
2475 return FALSE;
2476 }
2477
2478 if (info->flags_1)
2479 {
2480 if (info->executable)
2481 info->flags_1 &= ~ (DF_1_INITFIRST
2482 | DF_1_NODELETE
2483 | DF_1_NOOPEN);
2484 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
2485 info->flags_1))
2486 return FALSE;
2487 }
2488
2489 /* Work out the size of the version reference section. */
2490
2491 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2492 BFD_ASSERT (s != NULL);
2493 {
2494 struct elf_find_verdep_info sinfo;
2495
2496 sinfo.output_bfd = output_bfd;
2497 sinfo.info = info;
2498 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2499 if (sinfo.vers == 0)
2500 sinfo.vers = 1;
2501 sinfo.failed = FALSE;
2502
2503 elf_link_hash_traverse (elf_hash_table (info),
2504 _bfd_elf_link_find_version_dependencies,
2505 (PTR) &sinfo);
2506
2507 if (elf_tdata (output_bfd)->verref == NULL)
2508 _bfd_strip_section_from_output (info, s);
2509 else
2510 {
2511 Elf_Internal_Verneed *t;
2512 unsigned int size;
2513 unsigned int crefs;
2514 bfd_byte *p;
2515
2516 /* Build the version definition section. */
2517 size = 0;
2518 crefs = 0;
2519 for (t = elf_tdata (output_bfd)->verref;
2520 t != NULL;
2521 t = t->vn_nextref)
2522 {
2523 Elf_Internal_Vernaux *a;
2524
2525 size += sizeof (Elf_External_Verneed);
2526 ++crefs;
2527 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2528 size += sizeof (Elf_External_Vernaux);
2529 }
2530
2531 s->_raw_size = size;
2532 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2533 if (s->contents == NULL)
2534 return FALSE;
2535
2536 p = s->contents;
2537 for (t = elf_tdata (output_bfd)->verref;
2538 t != NULL;
2539 t = t->vn_nextref)
2540 {
2541 unsigned int caux;
2542 Elf_Internal_Vernaux *a;
2543 bfd_size_type indx;
2544
2545 caux = 0;
2546 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2547 ++caux;
2548
2549 t->vn_version = VER_NEED_CURRENT;
2550 t->vn_cnt = caux;
2551 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2552 elf_dt_name (t->vn_bfd) != NULL
2553 ? elf_dt_name (t->vn_bfd)
2554 : basename (t->vn_bfd->filename),
2555 FALSE);
2556 if (indx == (bfd_size_type) -1)
2557 return FALSE;
2558 t->vn_file = indx;
2559 t->vn_aux = sizeof (Elf_External_Verneed);
2560 if (t->vn_nextref == NULL)
2561 t->vn_next = 0;
2562 else
2563 t->vn_next = (sizeof (Elf_External_Verneed)
2564 + caux * sizeof (Elf_External_Vernaux));
2565
2566 _bfd_elf_swap_verneed_out (output_bfd, t,
2567 (Elf_External_Verneed *) p);
2568 p += sizeof (Elf_External_Verneed);
2569
2570 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2571 {
2572 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2573 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
2574 a->vna_nodename, FALSE);
2575 if (indx == (bfd_size_type) -1)
2576 return FALSE;
2577 a->vna_name = indx;
2578 if (a->vna_nextptr == NULL)
2579 a->vna_next = 0;
2580 else
2581 a->vna_next = sizeof (Elf_External_Vernaux);
2582
2583 _bfd_elf_swap_vernaux_out (output_bfd, a,
2584 (Elf_External_Vernaux *) p);
2585 p += sizeof (Elf_External_Vernaux);
2586 }
2587 }
2588
2589 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
2590 (bfd_vma) 0)
2591 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
2592 (bfd_vma) crefs))
2593 return FALSE;
2594
2595 elf_tdata (output_bfd)->cverrefs = crefs;
2596 }
2597 }
2598
2599 /* Assign dynsym indicies. In a shared library we generate a
2600 section symbol for each output section, which come first.
2601 Next come all of the back-end allocated local dynamic syms,
2602 followed by the rest of the global symbols. */
2603
2604 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2605
2606 /* Work out the size of the symbol version section. */
2607 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2608 BFD_ASSERT (s != NULL);
2609 if (dynsymcount == 0
2610 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2611 {
2612 _bfd_strip_section_from_output (info, s);
2613 /* The DYNSYMCOUNT might have changed if we were going to
2614 output a dynamic symbol table entry for S. */
2615 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
2616 }
2617 else
2618 {
2619 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2620 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
2621 if (s->contents == NULL)
2622 return FALSE;
2623
2624 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
2625 return FALSE;
2626 }
2627
2628 /* Set the size of the .dynsym and .hash sections. We counted
2629 the number of dynamic symbols in elf_link_add_object_symbols.
2630 We will build the contents of .dynsym and .hash when we build
2631 the final symbol table, because until then we do not know the
2632 correct value to give the symbols. We built the .dynstr
2633 section as we went along in elf_link_add_object_symbols. */
2634 s = bfd_get_section_by_name (dynobj, ".dynsym");
2635 BFD_ASSERT (s != NULL);
2636 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2637 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2638 if (s->contents == NULL && s->_raw_size != 0)
2639 return FALSE;
2640
2641 if (dynsymcount != 0)
2642 {
2643 Elf_Internal_Sym isym;
2644
2645 /* The first entry in .dynsym is a dummy symbol. */
2646 isym.st_value = 0;
2647 isym.st_size = 0;
2648 isym.st_name = 0;
2649 isym.st_info = 0;
2650 isym.st_other = 0;
2651 isym.st_shndx = 0;
2652 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
2653 }
2654
2655 /* Compute the size of the hashing table. As a side effect this
2656 computes the hash values for all the names we export. */
2657 bucketcount = compute_bucket_count (info);
2658
2659 s = bfd_get_section_by_name (dynobj, ".hash");
2660 BFD_ASSERT (s != NULL);
2661 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
2662 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
2663 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
2664 if (s->contents == NULL)
2665 return FALSE;
2666
2667 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
2668 s->contents);
2669 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
2670 s->contents + hash_entry_size);
2671
2672 elf_hash_table (info)->bucketcount = bucketcount;
2673
2674 s = bfd_get_section_by_name (dynobj, ".dynstr");
2675 BFD_ASSERT (s != NULL);
2676
2677 elf_finalize_dynstr (output_bfd, info);
2678
2679 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
2680
2681 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
2682 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
2683 return FALSE;
2684 }
2685
2686 return TRUE;
2687 }
2688 \f
2689 /* This function is used to adjust offsets into .dynstr for
2690 dynamic symbols. This is called via elf_link_hash_traverse. */
2691
2692 static bfd_boolean elf_adjust_dynstr_offsets
2693 PARAMS ((struct elf_link_hash_entry *, PTR));
2694
2695 static bfd_boolean
2696 elf_adjust_dynstr_offsets (h, data)
2697 struct elf_link_hash_entry *h;
2698 PTR data;
2699 {
2700 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
2701
2702 if (h->root.type == bfd_link_hash_warning)
2703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2704
2705 if (h->dynindx != -1)
2706 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2707 return TRUE;
2708 }
2709
2710 /* Assign string offsets in .dynstr, update all structures referencing
2711 them. */
2712
2713 static bfd_boolean
2714 elf_finalize_dynstr (output_bfd, info)
2715 bfd *output_bfd;
2716 struct bfd_link_info *info;
2717 {
2718 struct elf_link_local_dynamic_entry *entry;
2719 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
2720 bfd *dynobj = elf_hash_table (info)->dynobj;
2721 asection *sdyn;
2722 bfd_size_type size;
2723 Elf_External_Dyn *dyncon, *dynconend;
2724
2725 _bfd_elf_strtab_finalize (dynstr);
2726 size = _bfd_elf_strtab_size (dynstr);
2727
2728 /* Update all .dynamic entries referencing .dynstr strings. */
2729 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2730 BFD_ASSERT (sdyn != NULL);
2731
2732 dyncon = (Elf_External_Dyn *) sdyn->contents;
2733 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2734 sdyn->_raw_size);
2735 for (; dyncon < dynconend; dyncon++)
2736 {
2737 Elf_Internal_Dyn dyn;
2738
2739 elf_swap_dyn_in (dynobj, dyncon, & dyn);
2740 switch (dyn.d_tag)
2741 {
2742 case DT_STRSZ:
2743 dyn.d_un.d_val = size;
2744 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2745 break;
2746 case DT_NEEDED:
2747 case DT_SONAME:
2748 case DT_RPATH:
2749 case DT_RUNPATH:
2750 case DT_FILTER:
2751 case DT_AUXILIARY:
2752 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2753 elf_swap_dyn_out (dynobj, & dyn, dyncon);
2754 break;
2755 default:
2756 break;
2757 }
2758 }
2759
2760 /* Now update local dynamic symbols. */
2761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2762 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2763 entry->isym.st_name);
2764
2765 /* And the rest of dynamic symbols. */
2766 elf_link_hash_traverse (elf_hash_table (info),
2767 elf_adjust_dynstr_offsets, dynstr);
2768
2769 /* Adjust version definitions. */
2770 if (elf_tdata (output_bfd)->cverdefs)
2771 {
2772 asection *s;
2773 bfd_byte *p;
2774 bfd_size_type i;
2775 Elf_Internal_Verdef def;
2776 Elf_Internal_Verdaux defaux;
2777
2778 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2779 p = (bfd_byte *) s->contents;
2780 do
2781 {
2782 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2783 &def);
2784 p += sizeof (Elf_External_Verdef);
2785 for (i = 0; i < def.vd_cnt; ++i)
2786 {
2787 _bfd_elf_swap_verdaux_in (output_bfd,
2788 (Elf_External_Verdaux *) p, &defaux);
2789 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2790 defaux.vda_name);
2791 _bfd_elf_swap_verdaux_out (output_bfd,
2792 &defaux, (Elf_External_Verdaux *) p);
2793 p += sizeof (Elf_External_Verdaux);
2794 }
2795 }
2796 while (def.vd_next);
2797 }
2798
2799 /* Adjust version references. */
2800 if (elf_tdata (output_bfd)->verref)
2801 {
2802 asection *s;
2803 bfd_byte *p;
2804 bfd_size_type i;
2805 Elf_Internal_Verneed need;
2806 Elf_Internal_Vernaux needaux;
2807
2808 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2809 p = (bfd_byte *) s->contents;
2810 do
2811 {
2812 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2813 &need);
2814 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2815 _bfd_elf_swap_verneed_out (output_bfd, &need,
2816 (Elf_External_Verneed *) p);
2817 p += sizeof (Elf_External_Verneed);
2818 for (i = 0; i < need.vn_cnt; ++i)
2819 {
2820 _bfd_elf_swap_vernaux_in (output_bfd,
2821 (Elf_External_Vernaux *) p, &needaux);
2822 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2823 needaux.vna_name);
2824 _bfd_elf_swap_vernaux_out (output_bfd,
2825 &needaux,
2826 (Elf_External_Vernaux *) p);
2827 p += sizeof (Elf_External_Vernaux);
2828 }
2829 }
2830 while (need.vn_next);
2831 }
2832
2833 return TRUE;
2834 }
2835 \f
2836 /* Final phase of ELF linker. */
2837
2838 /* A structure we use to avoid passing large numbers of arguments. */
2839
2840 struct elf_final_link_info
2841 {
2842 /* General link information. */
2843 struct bfd_link_info *info;
2844 /* Output BFD. */
2845 bfd *output_bfd;
2846 /* Symbol string table. */
2847 struct bfd_strtab_hash *symstrtab;
2848 /* .dynsym section. */
2849 asection *dynsym_sec;
2850 /* .hash section. */
2851 asection *hash_sec;
2852 /* symbol version section (.gnu.version). */
2853 asection *symver_sec;
2854 /* first SHF_TLS section (if any). */
2855 asection *first_tls_sec;
2856 /* Buffer large enough to hold contents of any section. */
2857 bfd_byte *contents;
2858 /* Buffer large enough to hold external relocs of any section. */
2859 PTR external_relocs;
2860 /* Buffer large enough to hold internal relocs of any section. */
2861 Elf_Internal_Rela *internal_relocs;
2862 /* Buffer large enough to hold external local symbols of any input
2863 BFD. */
2864 Elf_External_Sym *external_syms;
2865 /* And a buffer for symbol section indices. */
2866 Elf_External_Sym_Shndx *locsym_shndx;
2867 /* Buffer large enough to hold internal local symbols of any input
2868 BFD. */
2869 Elf_Internal_Sym *internal_syms;
2870 /* Array large enough to hold a symbol index for each local symbol
2871 of any input BFD. */
2872 long *indices;
2873 /* Array large enough to hold a section pointer for each local
2874 symbol of any input BFD. */
2875 asection **sections;
2876 /* Buffer to hold swapped out symbols. */
2877 Elf_External_Sym *symbuf;
2878 /* And one for symbol section indices. */
2879 Elf_External_Sym_Shndx *symshndxbuf;
2880 /* Number of swapped out symbols in buffer. */
2881 size_t symbuf_count;
2882 /* Number of symbols which fit in symbuf. */
2883 size_t symbuf_size;
2884 /* And same for symshndxbuf. */
2885 size_t shndxbuf_size;
2886 };
2887
2888 static bfd_boolean elf_link_output_sym
2889 PARAMS ((struct elf_final_link_info *, const char *,
2890 Elf_Internal_Sym *, asection *));
2891 static bfd_boolean elf_link_flush_output_syms
2892 PARAMS ((struct elf_final_link_info *));
2893 static bfd_boolean elf_link_output_extsym
2894 PARAMS ((struct elf_link_hash_entry *, PTR));
2895 static bfd_boolean elf_link_check_versioned_symbol
2896 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2897 static bfd_boolean elf_link_input_bfd
2898 PARAMS ((struct elf_final_link_info *, bfd *));
2899 static bfd_boolean elf_reloc_link_order
2900 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2901 struct bfd_link_order *));
2902
2903 /* This struct is used to pass information to elf_link_output_extsym. */
2904
2905 struct elf_outext_info
2906 {
2907 bfd_boolean failed;
2908 bfd_boolean localsyms;
2909 struct elf_final_link_info *finfo;
2910 };
2911
2912 /* When performing a relocatable link, the input relocations are
2913 preserved. But, if they reference global symbols, the indices
2914 referenced must be updated. Update all the relocations in
2915 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
2916
2917 static void
2918 elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
2919 bfd *abfd;
2920 Elf_Internal_Shdr *rel_hdr;
2921 unsigned int count;
2922 struct elf_link_hash_entry **rel_hash;
2923 {
2924 unsigned int i;
2925 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2926 bfd_byte *erela;
2927 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
2928 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
2929
2930 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2931 {
2932 swap_in = bed->s->swap_reloc_in;
2933 swap_out = bed->s->swap_reloc_out;
2934 }
2935 else if (rel_hdr->sh_entsize == sizeof (Elf_External_Rela))
2936 {
2937 swap_in = bed->s->swap_reloca_in;
2938 swap_out = bed->s->swap_reloca_out;
2939 }
2940 else
2941 abort ();
2942
2943 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
2944 abort ();
2945
2946 erela = rel_hdr->contents;
2947 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
2948 {
2949 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
2950 unsigned int j;
2951
2952 if (*rel_hash == NULL)
2953 continue;
2954
2955 BFD_ASSERT ((*rel_hash)->indx >= 0);
2956
2957 (*swap_in) (abfd, erela, irela);
2958 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
2959 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
2960 ELF_R_TYPE (irela[j].r_info));
2961 (*swap_out) (abfd, irela, erela);
2962 }
2963 }
2964
2965 struct elf_link_sort_rela
2966 {
2967 bfd_vma offset;
2968 enum elf_reloc_type_class type;
2969 /* We use this as an array of size int_rels_per_ext_rel. */
2970 Elf_Internal_Rela rela[1];
2971 };
2972
2973 static int
2974 elf_link_sort_cmp1 (A, B)
2975 const PTR A;
2976 const PTR B;
2977 {
2978 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
2979 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
2980 int relativea, relativeb;
2981
2982 relativea = a->type == reloc_class_relative;
2983 relativeb = b->type == reloc_class_relative;
2984
2985 if (relativea < relativeb)
2986 return 1;
2987 if (relativea > relativeb)
2988 return -1;
2989 if (ELF_R_SYM (a->rela->r_info) < ELF_R_SYM (b->rela->r_info))
2990 return -1;
2991 if (ELF_R_SYM (a->rela->r_info) > ELF_R_SYM (b->rela->r_info))
2992 return 1;
2993 if (a->rela->r_offset < b->rela->r_offset)
2994 return -1;
2995 if (a->rela->r_offset > b->rela->r_offset)
2996 return 1;
2997 return 0;
2998 }
2999
3000 static int
3001 elf_link_sort_cmp2 (A, B)
3002 const PTR A;
3003 const PTR B;
3004 {
3005 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
3006 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
3007 int copya, copyb;
3008
3009 if (a->offset < b->offset)
3010 return -1;
3011 if (a->offset > b->offset)
3012 return 1;
3013 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
3014 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
3015 if (copya < copyb)
3016 return -1;
3017 if (copya > copyb)
3018 return 1;
3019 if (a->rela->r_offset < b->rela->r_offset)
3020 return -1;
3021 if (a->rela->r_offset > b->rela->r_offset)
3022 return 1;
3023 return 0;
3024 }
3025
3026 static size_t
3027 elf_link_sort_relocs (abfd, info, psec)
3028 bfd *abfd;
3029 struct bfd_link_info *info;
3030 asection **psec;
3031 {
3032 asection *reldyn;
3033 bfd_size_type count, size;
3034 size_t i, ret, sort_elt, ext_size;
3035 bfd_byte *sort, *s_non_relative, *p;
3036 struct elf_link_sort_rela *sq;
3037 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3038 int i2e = bed->s->int_rels_per_ext_rel;
3039 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
3040 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
3041 struct bfd_link_order *lo;
3042
3043 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
3044 if (reldyn == NULL || reldyn->_raw_size == 0)
3045 {
3046 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
3047 if (reldyn == NULL || reldyn->_raw_size == 0)
3048 return 0;
3049 ext_size = sizeof (Elf_External_Rel);
3050 swap_in = bed->s->swap_reloc_in;
3051 swap_out = bed->s->swap_reloc_out;
3052 }
3053 else
3054 {
3055 ext_size = sizeof (Elf_External_Rela);
3056 swap_in = bed->s->swap_reloca_in;
3057 swap_out = bed->s->swap_reloca_out;
3058 }
3059 count = reldyn->_raw_size / ext_size;
3060
3061 size = 0;
3062 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3063 if (lo->type == bfd_indirect_link_order)
3064 {
3065 asection *o = lo->u.indirect.section;
3066 size += o->_raw_size;
3067 }
3068
3069 if (size != reldyn->_raw_size)
3070 return 0;
3071
3072 sort_elt = (sizeof (struct elf_link_sort_rela)
3073 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3074 sort = bfd_zmalloc (sort_elt * count);
3075 if (sort == NULL)
3076 {
3077 (*info->callbacks->warning)
3078 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
3079 (bfd_vma) 0);
3080 return 0;
3081 }
3082
3083 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3084 if (lo->type == bfd_indirect_link_order)
3085 {
3086 bfd_byte *erel, *erelend;
3087 asection *o = lo->u.indirect.section;
3088
3089 erel = o->contents;
3090 erelend = o->contents + o->_raw_size;
3091 p = sort + o->output_offset / ext_size * sort_elt;
3092 while (erel < erelend)
3093 {
3094 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3095 (*swap_in) (abfd, erel, s->rela);
3096 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
3097 p += sort_elt;
3098 erel += ext_size;
3099 }
3100 }
3101
3102 qsort (sort, (size_t) count, sort_elt, elf_link_sort_cmp1);
3103
3104 for (i = 0, p = sort; i < count; i++, p += sort_elt)
3105 {
3106 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3107 if (s->type != reloc_class_relative)
3108 break;
3109 }
3110 ret = i;
3111 s_non_relative = p;
3112
3113 sq = (struct elf_link_sort_rela *) s_non_relative;
3114 for (; i < count; i++, p += sort_elt)
3115 {
3116 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
3117 if (ELF_R_SYM (sp->rela->r_info) != ELF_R_SYM (sq->rela->r_info))
3118 sq = sp;
3119 sp->offset = sq->rela->r_offset;
3120 }
3121
3122 qsort (s_non_relative, (size_t) count - ret, sort_elt, elf_link_sort_cmp2);
3123
3124 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
3125 if (lo->type == bfd_indirect_link_order)
3126 {
3127 bfd_byte *erel, *erelend;
3128 asection *o = lo->u.indirect.section;
3129
3130 erel = o->contents;
3131 erelend = o->contents + o->_raw_size;
3132 p = sort + o->output_offset / ext_size * sort_elt;
3133 while (erel < erelend)
3134 {
3135 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3136 (*swap_out) (abfd, s->rela, erel);
3137 p += sort_elt;
3138 erel += ext_size;
3139 }
3140 }
3141
3142 free (sort);
3143 *psec = reldyn;
3144 return ret;
3145 }
3146
3147 /* Do the final step of an ELF link. */
3148
3149 bfd_boolean
3150 elf_bfd_final_link (abfd, info)
3151 bfd *abfd;
3152 struct bfd_link_info *info;
3153 {
3154 bfd_boolean dynamic;
3155 bfd_boolean emit_relocs;
3156 bfd *dynobj;
3157 struct elf_final_link_info finfo;
3158 register asection *o;
3159 register struct bfd_link_order *p;
3160 register bfd *sub;
3161 bfd_size_type max_contents_size;
3162 bfd_size_type max_external_reloc_size;
3163 bfd_size_type max_internal_reloc_count;
3164 bfd_size_type max_sym_count;
3165 bfd_size_type max_sym_shndx_count;
3166 file_ptr off;
3167 Elf_Internal_Sym elfsym;
3168 unsigned int i;
3169 Elf_Internal_Shdr *symtab_hdr;
3170 Elf_Internal_Shdr *symtab_shndx_hdr;
3171 Elf_Internal_Shdr *symstrtab_hdr;
3172 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3173 struct elf_outext_info eoinfo;
3174 bfd_boolean merged;
3175 size_t relativecount = 0;
3176 asection *reldyn = 0;
3177 bfd_size_type amt;
3178
3179 if (! is_elf_hash_table (info))
3180 return FALSE;
3181
3182 if (info->shared)
3183 abfd->flags |= DYNAMIC;
3184
3185 dynamic = elf_hash_table (info)->dynamic_sections_created;
3186 dynobj = elf_hash_table (info)->dynobj;
3187
3188 emit_relocs = (info->relocatable
3189 || info->emitrelocations
3190 || bed->elf_backend_emit_relocs);
3191
3192 finfo.info = info;
3193 finfo.output_bfd = abfd;
3194 finfo.symstrtab = elf_stringtab_init ();
3195 if (finfo.symstrtab == NULL)
3196 return FALSE;
3197
3198 if (! dynamic)
3199 {
3200 finfo.dynsym_sec = NULL;
3201 finfo.hash_sec = NULL;
3202 finfo.symver_sec = NULL;
3203 }
3204 else
3205 {
3206 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
3207 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
3208 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
3209 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
3210 /* Note that it is OK if symver_sec is NULL. */
3211 }
3212
3213 finfo.contents = NULL;
3214 finfo.external_relocs = NULL;
3215 finfo.internal_relocs = NULL;
3216 finfo.external_syms = NULL;
3217 finfo.locsym_shndx = NULL;
3218 finfo.internal_syms = NULL;
3219 finfo.indices = NULL;
3220 finfo.sections = NULL;
3221 finfo.symbuf = NULL;
3222 finfo.symshndxbuf = NULL;
3223 finfo.symbuf_count = 0;
3224 finfo.shndxbuf_size = 0;
3225 finfo.first_tls_sec = NULL;
3226 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3227 if ((o->flags & SEC_THREAD_LOCAL) != 0
3228 && (o->flags & SEC_LOAD) != 0)
3229 {
3230 finfo.first_tls_sec = o;
3231 break;
3232 }
3233
3234 /* Count up the number of relocations we will output for each output
3235 section, so that we know the sizes of the reloc sections. We
3236 also figure out some maximum sizes. */
3237 max_contents_size = 0;
3238 max_external_reloc_size = 0;
3239 max_internal_reloc_count = 0;
3240 max_sym_count = 0;
3241 max_sym_shndx_count = 0;
3242 merged = FALSE;
3243 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3244 {
3245 struct bfd_elf_section_data *esdo = elf_section_data (o);
3246 o->reloc_count = 0;
3247
3248 for (p = o->link_order_head; p != NULL; p = p->next)
3249 {
3250 unsigned int reloc_count = 0;
3251 struct bfd_elf_section_data *esdi = NULL;
3252 unsigned int *rel_count1;
3253
3254 if (p->type == bfd_section_reloc_link_order
3255 || p->type == bfd_symbol_reloc_link_order)
3256 reloc_count = 1;
3257 else if (p->type == bfd_indirect_link_order)
3258 {
3259 asection *sec;
3260
3261 sec = p->u.indirect.section;
3262 esdi = elf_section_data (sec);
3263
3264 /* Mark all sections which are to be included in the
3265 link. This will normally be every section. We need
3266 to do this so that we can identify any sections which
3267 the linker has decided to not include. */
3268 sec->linker_mark = TRUE;
3269
3270 if (sec->flags & SEC_MERGE)
3271 merged = TRUE;
3272
3273 if (info->relocatable || info->emitrelocations)
3274 reloc_count = sec->reloc_count;
3275 else if (bed->elf_backend_count_relocs)
3276 {
3277 Elf_Internal_Rela * relocs;
3278
3279 relocs
3280 = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
3281 (Elf_Internal_Rela *) NULL,
3282 info->keep_memory);
3283
3284 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
3285
3286 if (elf_section_data (o)->relocs != relocs)
3287 free (relocs);
3288 }
3289
3290 if (sec->_raw_size > max_contents_size)
3291 max_contents_size = sec->_raw_size;
3292 if (sec->_cooked_size > max_contents_size)
3293 max_contents_size = sec->_cooked_size;
3294
3295 /* We are interested in just local symbols, not all
3296 symbols. */
3297 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
3298 && (sec->owner->flags & DYNAMIC) == 0)
3299 {
3300 size_t sym_count;
3301
3302 if (elf_bad_symtab (sec->owner))
3303 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
3304 / sizeof (Elf_External_Sym));
3305 else
3306 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
3307
3308 if (sym_count > max_sym_count)
3309 max_sym_count = sym_count;
3310
3311 if (sym_count > max_sym_shndx_count
3312 && elf_symtab_shndx (sec->owner) != 0)
3313 max_sym_shndx_count = sym_count;
3314
3315 if ((sec->flags & SEC_RELOC) != 0)
3316 {
3317 size_t ext_size;
3318
3319 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
3320 if (ext_size > max_external_reloc_size)
3321 max_external_reloc_size = ext_size;
3322 if (sec->reloc_count > max_internal_reloc_count)
3323 max_internal_reloc_count = sec->reloc_count;
3324 }
3325 }
3326 }
3327
3328 if (reloc_count == 0)
3329 continue;
3330
3331 o->reloc_count += reloc_count;
3332
3333 /* MIPS may have a mix of REL and RELA relocs on sections.
3334 To support this curious ABI we keep reloc counts in
3335 elf_section_data too. We must be careful to add the
3336 relocations from the input section to the right output
3337 count. FIXME: Get rid of one count. We have
3338 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
3339 rel_count1 = &esdo->rel_count;
3340 if (esdi != NULL)
3341 {
3342 bfd_boolean same_size;
3343 bfd_size_type entsize1;
3344
3345 entsize1 = esdi->rel_hdr.sh_entsize;
3346 BFD_ASSERT (entsize1 == sizeof (Elf_External_Rel)
3347 || entsize1 == sizeof (Elf_External_Rela));
3348 same_size = (!o->use_rela_p
3349 == (entsize1 == sizeof (Elf_External_Rel)));
3350
3351 if (!same_size)
3352 rel_count1 = &esdo->rel_count2;
3353
3354 if (esdi->rel_hdr2 != NULL)
3355 {
3356 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
3357 unsigned int alt_count;
3358 unsigned int *rel_count2;
3359
3360 BFD_ASSERT (entsize2 != entsize1
3361 && (entsize2 == sizeof (Elf_External_Rel)
3362 || entsize2 == sizeof (Elf_External_Rela)));
3363
3364 rel_count2 = &esdo->rel_count2;
3365 if (!same_size)
3366 rel_count2 = &esdo->rel_count;
3367
3368 /* The following is probably too simplistic if the
3369 backend counts output relocs unusually. */
3370 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
3371 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
3372 *rel_count2 += alt_count;
3373 reloc_count -= alt_count;
3374 }
3375 }
3376 *rel_count1 += reloc_count;
3377 }
3378
3379 if (o->reloc_count > 0)
3380 o->flags |= SEC_RELOC;
3381 else
3382 {
3383 /* Explicitly clear the SEC_RELOC flag. The linker tends to
3384 set it (this is probably a bug) and if it is set
3385 assign_section_numbers will create a reloc section. */
3386 o->flags &=~ SEC_RELOC;
3387 }
3388
3389 /* If the SEC_ALLOC flag is not set, force the section VMA to
3390 zero. This is done in elf_fake_sections as well, but forcing
3391 the VMA to 0 here will ensure that relocs against these
3392 sections are handled correctly. */
3393 if ((o->flags & SEC_ALLOC) == 0
3394 && ! o->user_set_vma)
3395 o->vma = 0;
3396 }
3397
3398 if (! info->relocatable && merged)
3399 elf_link_hash_traverse (elf_hash_table (info),
3400 _bfd_elf_link_sec_merge_syms, (PTR) abfd);
3401
3402 /* Figure out the file positions for everything but the symbol table
3403 and the relocs. We set symcount to force assign_section_numbers
3404 to create a symbol table. */
3405 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
3406 BFD_ASSERT (! abfd->output_has_begun);
3407 if (! _bfd_elf_compute_section_file_positions (abfd, info))
3408 goto error_return;
3409
3410 /* That created the reloc sections. Set their sizes, and assign
3411 them file positions, and allocate some buffers. */
3412 for (o = abfd->sections; o != NULL; o = o->next)
3413 {
3414 if ((o->flags & SEC_RELOC) != 0)
3415 {
3416 if (!(_bfd_elf_link_size_reloc_section
3417 (abfd, &elf_section_data (o)->rel_hdr, o)))
3418 goto error_return;
3419
3420 if (elf_section_data (o)->rel_hdr2
3421 && !(_bfd_elf_link_size_reloc_section
3422 (abfd, elf_section_data (o)->rel_hdr2, o)))
3423 goto error_return;
3424 }
3425
3426 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3427 to count upwards while actually outputting the relocations. */
3428 elf_section_data (o)->rel_count = 0;
3429 elf_section_data (o)->rel_count2 = 0;
3430 }
3431
3432 _bfd_elf_assign_file_positions_for_relocs (abfd);
3433
3434 /* We have now assigned file positions for all the sections except
3435 .symtab and .strtab. We start the .symtab section at the current
3436 file position, and write directly to it. We build the .strtab
3437 section in memory. */
3438 bfd_get_symcount (abfd) = 0;
3439 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3440 /* sh_name is set in prep_headers. */
3441 symtab_hdr->sh_type = SHT_SYMTAB;
3442 /* sh_flags, sh_addr and sh_size all start off zero. */
3443 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
3444 /* sh_link is set in assign_section_numbers. */
3445 /* sh_info is set below. */
3446 /* sh_offset is set just below. */
3447 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
3448
3449 off = elf_tdata (abfd)->next_file_pos;
3450 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
3451
3452 /* Note that at this point elf_tdata (abfd)->next_file_pos is
3453 incorrect. We do not yet know the size of the .symtab section.
3454 We correct next_file_pos below, after we do know the size. */
3455
3456 /* Allocate a buffer to hold swapped out symbols. This is to avoid
3457 continuously seeking to the right position in the file. */
3458 if (! info->keep_memory || max_sym_count < 20)
3459 finfo.symbuf_size = 20;
3460 else
3461 finfo.symbuf_size = max_sym_count;
3462 amt = finfo.symbuf_size;
3463 amt *= sizeof (Elf_External_Sym);
3464 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
3465 if (finfo.symbuf == NULL)
3466 goto error_return;
3467 if (elf_numsections (abfd) > SHN_LORESERVE)
3468 {
3469 /* Wild guess at number of output symbols. realloc'd as needed. */
3470 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
3471 finfo.shndxbuf_size = amt;
3472 amt *= sizeof (Elf_External_Sym_Shndx);
3473 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
3474 if (finfo.symshndxbuf == NULL)
3475 goto error_return;
3476 }
3477
3478 /* Start writing out the symbol table. The first symbol is always a
3479 dummy symbol. */
3480 if (info->strip != strip_all
3481 || emit_relocs)
3482 {
3483 elfsym.st_value = 0;
3484 elfsym.st_size = 0;
3485 elfsym.st_info = 0;
3486 elfsym.st_other = 0;
3487 elfsym.st_shndx = SHN_UNDEF;
3488 if (! elf_link_output_sym (&finfo, (const char *) NULL,
3489 &elfsym, bfd_und_section_ptr))
3490 goto error_return;
3491 }
3492
3493 #if 0
3494 /* Some standard ELF linkers do this, but we don't because it causes
3495 bootstrap comparison failures. */
3496 /* Output a file symbol for the output file as the second symbol.
3497 We output this even if we are discarding local symbols, although
3498 I'm not sure if this is correct. */
3499 elfsym.st_value = 0;
3500 elfsym.st_size = 0;
3501 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3502 elfsym.st_other = 0;
3503 elfsym.st_shndx = SHN_ABS;
3504 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
3505 &elfsym, bfd_abs_section_ptr))
3506 goto error_return;
3507 #endif
3508
3509 /* Output a symbol for each section. We output these even if we are
3510 discarding local symbols, since they are used for relocs. These
3511 symbols have no names. We store the index of each one in the
3512 index field of the section, so that we can find it again when
3513 outputting relocs. */
3514 if (info->strip != strip_all
3515 || emit_relocs)
3516 {
3517 elfsym.st_size = 0;
3518 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3519 elfsym.st_other = 0;
3520 for (i = 1; i < elf_numsections (abfd); i++)
3521 {
3522 o = section_from_elf_index (abfd, i);
3523 if (o != NULL)
3524 o->target_index = bfd_get_symcount (abfd);
3525 elfsym.st_shndx = i;
3526 if (info->relocatable || o == NULL)
3527 elfsym.st_value = 0;
3528 else
3529 elfsym.st_value = o->vma;
3530 if (! elf_link_output_sym (&finfo, (const char *) NULL,
3531 &elfsym, o))
3532 goto error_return;
3533 if (i == SHN_LORESERVE - 1)
3534 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3535 }
3536 }
3537
3538 /* Allocate some memory to hold information read in from the input
3539 files. */
3540 if (max_contents_size != 0)
3541 {
3542 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3543 if (finfo.contents == NULL)
3544 goto error_return;
3545 }
3546
3547 if (max_external_reloc_size != 0)
3548 {
3549 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
3550 if (finfo.external_relocs == NULL)
3551 goto error_return;
3552 }
3553
3554 if (max_internal_reloc_count != 0)
3555 {
3556 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
3557 amt *= sizeof (Elf_Internal_Rela);
3558 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
3559 if (finfo.internal_relocs == NULL)
3560 goto error_return;
3561 }
3562
3563 if (max_sym_count != 0)
3564 {
3565 amt = max_sym_count * sizeof (Elf_External_Sym);
3566 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
3567 if (finfo.external_syms == NULL)
3568 goto error_return;
3569
3570 amt = max_sym_count * sizeof (Elf_Internal_Sym);
3571 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
3572 if (finfo.internal_syms == NULL)
3573 goto error_return;
3574
3575 amt = max_sym_count * sizeof (long);
3576 finfo.indices = (long *) bfd_malloc (amt);
3577 if (finfo.indices == NULL)
3578 goto error_return;
3579
3580 amt = max_sym_count * sizeof (asection *);
3581 finfo.sections = (asection **) bfd_malloc (amt);
3582 if (finfo.sections == NULL)
3583 goto error_return;
3584 }
3585
3586 if (max_sym_shndx_count != 0)
3587 {
3588 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
3589 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
3590 if (finfo.locsym_shndx == NULL)
3591 goto error_return;
3592 }
3593
3594 if (finfo.first_tls_sec)
3595 {
3596 unsigned int align = 0;
3597 bfd_vma base = finfo.first_tls_sec->vma, end = 0;
3598 asection *sec;
3599
3600 for (sec = finfo.first_tls_sec;
3601 sec && (sec->flags & SEC_THREAD_LOCAL);
3602 sec = sec->next)
3603 {
3604 bfd_vma size = sec->_raw_size;
3605
3606 if (bfd_get_section_alignment (abfd, sec) > align)
3607 align = bfd_get_section_alignment (abfd, sec);
3608 if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3609 {
3610 struct bfd_link_order *o;
3611
3612 size = 0;
3613 for (o = sec->link_order_head; o != NULL; o = o->next)
3614 if (size < o->offset + o->size)
3615 size = o->offset + o->size;
3616 }
3617 end = sec->vma + size;
3618 }
3619 elf_hash_table (info)->tls_segment
3620 = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
3621 if (elf_hash_table (info)->tls_segment == NULL)
3622 goto error_return;
3623 elf_hash_table (info)->tls_segment->start = base;
3624 elf_hash_table (info)->tls_segment->size = end - base;
3625 elf_hash_table (info)->tls_segment->align = align;
3626 }
3627
3628 /* Since ELF permits relocations to be against local symbols, we
3629 must have the local symbols available when we do the relocations.
3630 Since we would rather only read the local symbols once, and we
3631 would rather not keep them in memory, we handle all the
3632 relocations for a single input file at the same time.
3633
3634 Unfortunately, there is no way to know the total number of local
3635 symbols until we have seen all of them, and the local symbol
3636 indices precede the global symbol indices. This means that when
3637 we are generating relocatable output, and we see a reloc against
3638 a global symbol, we can not know the symbol index until we have
3639 finished examining all the local symbols to see which ones we are
3640 going to output. To deal with this, we keep the relocations in
3641 memory, and don't output them until the end of the link. This is
3642 an unfortunate waste of memory, but I don't see a good way around
3643 it. Fortunately, it only happens when performing a relocatable
3644 link, which is not the common case. FIXME: If keep_memory is set
3645 we could write the relocs out and then read them again; I don't
3646 know how bad the memory loss will be. */
3647
3648 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3649 sub->output_has_begun = FALSE;
3650 for (o = abfd->sections; o != NULL; o = o->next)
3651 {
3652 for (p = o->link_order_head; p != NULL; p = p->next)
3653 {
3654 if (p->type == bfd_indirect_link_order
3655 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
3656 == bfd_target_elf_flavour)
3657 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
3658 {
3659 if (! sub->output_has_begun)
3660 {
3661 if (! elf_link_input_bfd (&finfo, sub))
3662 goto error_return;
3663 sub->output_has_begun = TRUE;
3664 }
3665 }
3666 else if (p->type == bfd_section_reloc_link_order
3667 || p->type == bfd_symbol_reloc_link_order)
3668 {
3669 if (! elf_reloc_link_order (abfd, info, o, p))
3670 goto error_return;
3671 }
3672 else
3673 {
3674 if (! _bfd_default_link_order (abfd, info, o, p))
3675 goto error_return;
3676 }
3677 }
3678 }
3679
3680 /* Output any global symbols that got converted to local in a
3681 version script or due to symbol visibility. We do this in a
3682 separate step since ELF requires all local symbols to appear
3683 prior to any global symbols. FIXME: We should only do this if
3684 some global symbols were, in fact, converted to become local.
3685 FIXME: Will this work correctly with the Irix 5 linker? */
3686 eoinfo.failed = FALSE;
3687 eoinfo.finfo = &finfo;
3688 eoinfo.localsyms = TRUE;
3689 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3690 (PTR) &eoinfo);
3691 if (eoinfo.failed)
3692 return FALSE;
3693
3694 /* That wrote out all the local symbols. Finish up the symbol table
3695 with the global symbols. Even if we want to strip everything we
3696 can, we still need to deal with those global symbols that got
3697 converted to local in a version script. */
3698
3699 /* The sh_info field records the index of the first non local symbol. */
3700 symtab_hdr->sh_info = bfd_get_symcount (abfd);
3701
3702 if (dynamic
3703 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
3704 {
3705 Elf_Internal_Sym sym;
3706 Elf_External_Sym *dynsym =
3707 (Elf_External_Sym *) finfo.dynsym_sec->contents;
3708 long last_local = 0;
3709
3710 /* Write out the section symbols for the output sections. */
3711 if (info->shared)
3712 {
3713 asection *s;
3714
3715 sym.st_size = 0;
3716 sym.st_name = 0;
3717 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3718 sym.st_other = 0;
3719
3720 for (s = abfd->sections; s != NULL; s = s->next)
3721 {
3722 int indx;
3723 Elf_External_Sym *dest;
3724
3725 indx = elf_section_data (s)->this_idx;
3726 BFD_ASSERT (indx > 0);
3727 sym.st_shndx = indx;
3728 sym.st_value = s->vma;
3729 dest = dynsym + elf_section_data (s)->dynindx;
3730 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
3731 }
3732
3733 last_local = bfd_count_sections (abfd);
3734 }
3735
3736 /* Write out the local dynsyms. */
3737 if (elf_hash_table (info)->dynlocal)
3738 {
3739 struct elf_link_local_dynamic_entry *e;
3740 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
3741 {
3742 asection *s;
3743 Elf_External_Sym *dest;
3744
3745 sym.st_size = e->isym.st_size;
3746 sym.st_other = e->isym.st_other;
3747
3748 /* Copy the internal symbol as is.
3749 Note that we saved a word of storage and overwrote
3750 the original st_name with the dynstr_index. */
3751 sym = e->isym;
3752
3753 if (e->isym.st_shndx != SHN_UNDEF
3754 && (e->isym.st_shndx < SHN_LORESERVE
3755 || e->isym.st_shndx > SHN_HIRESERVE))
3756 {
3757 s = bfd_section_from_elf_index (e->input_bfd,
3758 e->isym.st_shndx);
3759
3760 sym.st_shndx =
3761 elf_section_data (s->output_section)->this_idx;
3762 sym.st_value = (s->output_section->vma
3763 + s->output_offset
3764 + e->isym.st_value);
3765 }
3766
3767 if (last_local < e->dynindx)
3768 last_local = e->dynindx;
3769
3770 dest = dynsym + e->dynindx;
3771 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
3772 }
3773 }
3774
3775 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
3776 last_local + 1;
3777 }
3778
3779 /* We get the global symbols from the hash table. */
3780 eoinfo.failed = FALSE;
3781 eoinfo.localsyms = FALSE;
3782 eoinfo.finfo = &finfo;
3783 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3784 (PTR) &eoinfo);
3785 if (eoinfo.failed)
3786 return FALSE;
3787
3788 /* If backend needs to output some symbols not present in the hash
3789 table, do it now. */
3790 if (bed->elf_backend_output_arch_syms)
3791 {
3792 typedef bfd_boolean (*out_sym_func)
3793 PARAMS ((PTR, const char *, Elf_Internal_Sym *, asection *));
3794
3795 if (! ((*bed->elf_backend_output_arch_syms)
3796 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
3797 return FALSE;
3798 }
3799
3800 /* Flush all symbols to the file. */
3801 if (! elf_link_flush_output_syms (&finfo))
3802 return FALSE;
3803
3804 /* Now we know the size of the symtab section. */
3805 off += symtab_hdr->sh_size;
3806
3807 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3808 if (symtab_shndx_hdr->sh_name != 0)
3809 {
3810 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
3811 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
3812 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
3813 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
3814 symtab_shndx_hdr->sh_size = amt;
3815
3816 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
3817 off, TRUE);
3818
3819 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
3820 || (bfd_bwrite ((PTR) finfo.symshndxbuf, amt, abfd) != amt))
3821 return FALSE;
3822 }
3823
3824
3825 /* Finish up and write out the symbol string table (.strtab)
3826 section. */
3827 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3828 /* sh_name was set in prep_headers. */
3829 symstrtab_hdr->sh_type = SHT_STRTAB;
3830 symstrtab_hdr->sh_flags = 0;
3831 symstrtab_hdr->sh_addr = 0;
3832 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3833 symstrtab_hdr->sh_entsize = 0;
3834 symstrtab_hdr->sh_link = 0;
3835 symstrtab_hdr->sh_info = 0;
3836 /* sh_offset is set just below. */
3837 symstrtab_hdr->sh_addralign = 1;
3838
3839 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
3840 elf_tdata (abfd)->next_file_pos = off;
3841
3842 if (bfd_get_symcount (abfd) > 0)
3843 {
3844 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3845 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3846 return FALSE;
3847 }
3848
3849 /* Adjust the relocs to have the correct symbol indices. */
3850 for (o = abfd->sections; o != NULL; o = o->next)
3851 {
3852 if ((o->flags & SEC_RELOC) == 0)
3853 continue;
3854
3855 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
3856 elf_section_data (o)->rel_count,
3857 elf_section_data (o)->rel_hashes);
3858 if (elf_section_data (o)->rel_hdr2 != NULL)
3859 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
3860 elf_section_data (o)->rel_count2,
3861 (elf_section_data (o)->rel_hashes
3862 + elf_section_data (o)->rel_count));
3863
3864 /* Set the reloc_count field to 0 to prevent write_relocs from
3865 trying to swap the relocs out itself. */
3866 o->reloc_count = 0;
3867 }
3868
3869 if (dynamic && info->combreloc && dynobj != NULL)
3870 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
3871
3872 /* If we are linking against a dynamic object, or generating a
3873 shared library, finish up the dynamic linking information. */
3874 if (dynamic)
3875 {
3876 Elf_External_Dyn *dyncon, *dynconend;
3877
3878 /* Fix up .dynamic entries. */
3879 o = bfd_get_section_by_name (dynobj, ".dynamic");
3880 BFD_ASSERT (o != NULL);
3881
3882 dyncon = (Elf_External_Dyn *) o->contents;
3883 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3884 for (; dyncon < dynconend; dyncon++)
3885 {
3886 Elf_Internal_Dyn dyn;
3887 const char *name;
3888 unsigned int type;
3889
3890 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3891
3892 switch (dyn.d_tag)
3893 {
3894 default:
3895 break;
3896 case DT_NULL:
3897 if (relativecount > 0 && dyncon + 1 < dynconend)
3898 {
3899 switch (elf_section_data (reldyn)->this_hdr.sh_type)
3900 {
3901 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
3902 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
3903 default: break;
3904 }
3905 if (dyn.d_tag != DT_NULL)
3906 {
3907 dyn.d_un.d_val = relativecount;
3908 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3909 relativecount = 0;
3910 }
3911 }
3912 break;
3913 case DT_INIT:
3914 name = info->init_function;
3915 goto get_sym;
3916 case DT_FINI:
3917 name = info->fini_function;
3918 get_sym:
3919 {
3920 struct elf_link_hash_entry *h;
3921
3922 h = elf_link_hash_lookup (elf_hash_table (info), name,
3923 FALSE, FALSE, TRUE);
3924 if (h != NULL
3925 && (h->root.type == bfd_link_hash_defined
3926 || h->root.type == bfd_link_hash_defweak))
3927 {
3928 dyn.d_un.d_val = h->root.u.def.value;
3929 o = h->root.u.def.section;
3930 if (o->output_section != NULL)
3931 dyn.d_un.d_val += (o->output_section->vma
3932 + o->output_offset);
3933 else
3934 {
3935 /* The symbol is imported from another shared
3936 library and does not apply to this one. */
3937 dyn.d_un.d_val = 0;
3938 }
3939
3940 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3941 }
3942 }
3943 break;
3944
3945 case DT_PREINIT_ARRAYSZ:
3946 name = ".preinit_array";
3947 goto get_size;
3948 case DT_INIT_ARRAYSZ:
3949 name = ".init_array";
3950 goto get_size;
3951 case DT_FINI_ARRAYSZ:
3952 name = ".fini_array";
3953 get_size:
3954 o = bfd_get_section_by_name (abfd, name);
3955 if (o == NULL)
3956 {
3957 (*_bfd_error_handler)
3958 (_("%s: could not find output section %s"),
3959 bfd_get_filename (abfd), name);
3960 goto error_return;
3961 }
3962 if (o->_raw_size == 0)
3963 (*_bfd_error_handler)
3964 (_("warning: %s section has zero size"), name);
3965 dyn.d_un.d_val = o->_raw_size;
3966 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3967 break;
3968
3969 case DT_PREINIT_ARRAY:
3970 name = ".preinit_array";
3971 goto get_vma;
3972 case DT_INIT_ARRAY:
3973 name = ".init_array";
3974 goto get_vma;
3975 case DT_FINI_ARRAY:
3976 name = ".fini_array";
3977 goto get_vma;
3978
3979 case DT_HASH:
3980 name = ".hash";
3981 goto get_vma;
3982 case DT_STRTAB:
3983 name = ".dynstr";
3984 goto get_vma;
3985 case DT_SYMTAB:
3986 name = ".dynsym";
3987 goto get_vma;
3988 case DT_VERDEF:
3989 name = ".gnu.version_d";
3990 goto get_vma;
3991 case DT_VERNEED:
3992 name = ".gnu.version_r";
3993 goto get_vma;
3994 case DT_VERSYM:
3995 name = ".gnu.version";
3996 get_vma:
3997 o = bfd_get_section_by_name (abfd, name);
3998 if (o == NULL)
3999 {
4000 (*_bfd_error_handler)
4001 (_("%s: could not find output section %s"),
4002 bfd_get_filename (abfd), name);
4003 goto error_return;
4004 }
4005 dyn.d_un.d_ptr = o->vma;
4006 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4007 break;
4008
4009 case DT_REL:
4010 case DT_RELA:
4011 case DT_RELSZ:
4012 case DT_RELASZ:
4013 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
4014 type = SHT_REL;
4015 else
4016 type = SHT_RELA;
4017 dyn.d_un.d_val = 0;
4018 for (i = 1; i < elf_numsections (abfd); i++)
4019 {
4020 Elf_Internal_Shdr *hdr;
4021
4022 hdr = elf_elfsections (abfd)[i];
4023 if (hdr->sh_type == type
4024 && (hdr->sh_flags & SHF_ALLOC) != 0)
4025 {
4026 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
4027 dyn.d_un.d_val += hdr->sh_size;
4028 else
4029 {
4030 if (dyn.d_un.d_val == 0
4031 || hdr->sh_addr < dyn.d_un.d_val)
4032 dyn.d_un.d_val = hdr->sh_addr;
4033 }
4034 }
4035 }
4036 elf_swap_dyn_out (dynobj, &dyn, dyncon);
4037 break;
4038 }
4039 }
4040 }
4041
4042 /* If we have created any dynamic sections, then output them. */
4043 if (dynobj != NULL)
4044 {
4045 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
4046 goto error_return;
4047
4048 for (o = dynobj->sections; o != NULL; o = o->next)
4049 {
4050 if ((o->flags & SEC_HAS_CONTENTS) == 0
4051 || o->_raw_size == 0
4052 || o->output_section == bfd_abs_section_ptr)
4053 continue;
4054 if ((o->flags & SEC_LINKER_CREATED) == 0)
4055 {
4056 /* At this point, we are only interested in sections
4057 created by _bfd_elf_link_create_dynamic_sections. */
4058 continue;
4059 }
4060 if ((elf_section_data (o->output_section)->this_hdr.sh_type
4061 != SHT_STRTAB)
4062 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
4063 {
4064 if (! bfd_set_section_contents (abfd, o->output_section,
4065 o->contents,
4066 (file_ptr) o->output_offset,
4067 o->_raw_size))
4068 goto error_return;
4069 }
4070 else
4071 {
4072 /* The contents of the .dynstr section are actually in a
4073 stringtab. */
4074 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
4075 if (bfd_seek (abfd, off, SEEK_SET) != 0
4076 || ! _bfd_elf_strtab_emit (abfd,
4077 elf_hash_table (info)->dynstr))
4078 goto error_return;
4079 }
4080 }
4081 }
4082
4083 if (info->relocatable)
4084 {
4085 bfd_boolean failed = FALSE;
4086
4087 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4088 if (failed)
4089 goto error_return;
4090 }
4091
4092 /* If we have optimized stabs strings, output them. */
4093 if (elf_hash_table (info)->stab_info != NULL)
4094 {
4095 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
4096 goto error_return;
4097 }
4098
4099 if (info->eh_frame_hdr)
4100 {
4101 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
4102 goto error_return;
4103 }
4104
4105 if (finfo.symstrtab != NULL)
4106 _bfd_stringtab_free (finfo.symstrtab);
4107 if (finfo.contents != NULL)
4108 free (finfo.contents);
4109 if (finfo.external_relocs != NULL)
4110 free (finfo.external_relocs);
4111 if (finfo.internal_relocs != NULL)
4112 free (finfo.internal_relocs);
4113 if (finfo.external_syms != NULL)
4114 free (finfo.external_syms);
4115 if (finfo.locsym_shndx != NULL)
4116 free (finfo.locsym_shndx);
4117 if (finfo.internal_syms != NULL)
4118 free (finfo.internal_syms);
4119 if (finfo.indices != NULL)
4120 free (finfo.indices);
4121 if (finfo.sections != NULL)
4122 free (finfo.sections);
4123 if (finfo.symbuf != NULL)
4124 free (finfo.symbuf);
4125 if (finfo.symshndxbuf != NULL)
4126 free (finfo.symshndxbuf);
4127 for (o = abfd->sections; o != NULL; o = o->next)
4128 {
4129 if ((o->flags & SEC_RELOC) != 0
4130 && elf_section_data (o)->rel_hashes != NULL)
4131 free (elf_section_data (o)->rel_hashes);
4132 }
4133
4134 elf_tdata (abfd)->linker = TRUE;
4135
4136 return TRUE;
4137
4138 error_return:
4139 if (finfo.symstrtab != NULL)
4140 _bfd_stringtab_free (finfo.symstrtab);
4141 if (finfo.contents != NULL)
4142 free (finfo.contents);
4143 if (finfo.external_relocs != NULL)
4144 free (finfo.external_relocs);
4145 if (finfo.internal_relocs != NULL)
4146 free (finfo.internal_relocs);
4147 if (finfo.external_syms != NULL)
4148 free (finfo.external_syms);
4149 if (finfo.locsym_shndx != NULL)
4150 free (finfo.locsym_shndx);
4151 if (finfo.internal_syms != NULL)
4152 free (finfo.internal_syms);
4153 if (finfo.indices != NULL)
4154 free (finfo.indices);
4155 if (finfo.sections != NULL)
4156 free (finfo.sections);
4157 if (finfo.symbuf != NULL)
4158 free (finfo.symbuf);
4159 if (finfo.symshndxbuf != NULL)
4160 free (finfo.symshndxbuf);
4161 for (o = abfd->sections; o != NULL; o = o->next)
4162 {
4163 if ((o->flags & SEC_RELOC) != 0
4164 && elf_section_data (o)->rel_hashes != NULL)
4165 free (elf_section_data (o)->rel_hashes);
4166 }
4167
4168 return FALSE;
4169 }
4170
4171 /* Add a symbol to the output symbol table. */
4172
4173 static bfd_boolean
4174 elf_link_output_sym (finfo, name, elfsym, input_sec)
4175 struct elf_final_link_info *finfo;
4176 const char *name;
4177 Elf_Internal_Sym *elfsym;
4178 asection *input_sec;
4179 {
4180 Elf_External_Sym *dest;
4181 Elf_External_Sym_Shndx *destshndx;
4182 bfd_boolean (*output_symbol_hook)
4183 PARAMS ((bfd *, struct bfd_link_info *info, const char *,
4184 Elf_Internal_Sym *, asection *));
4185
4186 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
4187 elf_backend_link_output_symbol_hook;
4188 if (output_symbol_hook != NULL)
4189 {
4190 if (! ((*output_symbol_hook)
4191 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
4192 return FALSE;
4193 }
4194
4195 if (name == (const char *) NULL || *name == '\0')
4196 elfsym->st_name = 0;
4197 else if (input_sec->flags & SEC_EXCLUDE)
4198 elfsym->st_name = 0;
4199 else
4200 {
4201 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
4202 name, TRUE, FALSE);
4203 if (elfsym->st_name == (unsigned long) -1)
4204 return FALSE;
4205 }
4206
4207 if (finfo->symbuf_count >= finfo->symbuf_size)
4208 {
4209 if (! elf_link_flush_output_syms (finfo))
4210 return FALSE;
4211 }
4212
4213 dest = finfo->symbuf + finfo->symbuf_count;
4214 destshndx = finfo->symshndxbuf;
4215 if (destshndx != NULL)
4216 {
4217 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
4218 {
4219 bfd_size_type amt;
4220
4221 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
4222 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
4223 if (destshndx == NULL)
4224 return FALSE;
4225 memset ((char *) destshndx + amt, 0, amt);
4226 finfo->shndxbuf_size *= 2;
4227 }
4228 destshndx += bfd_get_symcount (finfo->output_bfd);
4229 }
4230
4231 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
4232 finfo->symbuf_count += 1;
4233 bfd_get_symcount (finfo->output_bfd) += 1;
4234
4235 return TRUE;
4236 }
4237
4238 /* Flush the output symbols to the file. */
4239
4240 static bfd_boolean
4241 elf_link_flush_output_syms (finfo)
4242 struct elf_final_link_info *finfo;
4243 {
4244 if (finfo->symbuf_count > 0)
4245 {
4246 Elf_Internal_Shdr *hdr;
4247 file_ptr pos;
4248 bfd_size_type amt;
4249
4250 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
4251 pos = hdr->sh_offset + hdr->sh_size;
4252 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
4253 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
4254 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
4255 return FALSE;
4256
4257 hdr->sh_size += amt;
4258 finfo->symbuf_count = 0;
4259 }
4260
4261 return TRUE;
4262 }
4263
4264 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
4265 allowing an unsatisfied unversioned symbol in the DSO to match a
4266 versioned symbol that would normally require an explicit version.
4267 We also handle the case that a DSO references a hidden symbol
4268 which may be satisfied by a versioned symbol in another DSO. */
4269
4270 static bfd_boolean
4271 elf_link_check_versioned_symbol (info, h)
4272 struct bfd_link_info *info;
4273 struct elf_link_hash_entry *h;
4274 {
4275 bfd *abfd;
4276 struct elf_link_loaded_list *loaded;
4277
4278 if (info->hash->creator->flavour != bfd_target_elf_flavour)
4279 return FALSE;
4280
4281 switch (h->root.type)
4282 {
4283 default:
4284 abfd = NULL;
4285 break;
4286
4287 case bfd_link_hash_undefined:
4288 case bfd_link_hash_undefweak:
4289 abfd = h->root.u.undef.abfd;
4290 if ((abfd->flags & DYNAMIC) == 0 || elf_dt_soname (abfd) == NULL)
4291 return FALSE;
4292 break;
4293
4294 case bfd_link_hash_defined:
4295 case bfd_link_hash_defweak:
4296 abfd = h->root.u.def.section->owner;
4297 break;
4298
4299 case bfd_link_hash_common:
4300 abfd = h->root.u.c.p->section->owner;
4301 break;
4302 }
4303 BFD_ASSERT (abfd != NULL);
4304
4305 for (loaded = elf_hash_table (info)->loaded;
4306 loaded != NULL;
4307 loaded = loaded->next)
4308 {
4309 bfd *input;
4310 Elf_Internal_Shdr *hdr;
4311 bfd_size_type symcount;
4312 bfd_size_type extsymcount;
4313 bfd_size_type extsymoff;
4314 Elf_Internal_Shdr *versymhdr;
4315 Elf_Internal_Sym *isym;
4316 Elf_Internal_Sym *isymend;
4317 Elf_Internal_Sym *isymbuf;
4318 Elf_External_Versym *ever;
4319 Elf_External_Versym *extversym;
4320
4321 input = loaded->abfd;
4322
4323 /* We check each DSO for a possible hidden versioned definition. */
4324 if (input == abfd
4325 || (input->flags & DYNAMIC) == 0
4326 || elf_dynversym (input) == 0)
4327 continue;
4328
4329 hdr = &elf_tdata (input)->dynsymtab_hdr;
4330
4331 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4332 if (elf_bad_symtab (input))
4333 {
4334 extsymcount = symcount;
4335 extsymoff = 0;
4336 }
4337 else
4338 {
4339 extsymcount = symcount - hdr->sh_info;
4340 extsymoff = hdr->sh_info;
4341 }
4342
4343 if (extsymcount == 0)
4344 continue;
4345
4346 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
4347 NULL, NULL, NULL);
4348 if (isymbuf == NULL)
4349 return FALSE;
4350
4351 /* Read in any version definitions. */
4352 versymhdr = &elf_tdata (input)->dynversym_hdr;
4353 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4354 if (extversym == NULL)
4355 goto error_ret;
4356
4357 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
4358 || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
4359 != versymhdr->sh_size))
4360 {
4361 free (extversym);
4362 error_ret:
4363 free (isymbuf);
4364 return FALSE;
4365 }
4366
4367 ever = extversym + extsymoff;
4368 isymend = isymbuf + extsymcount;
4369 for (isym = isymbuf; isym < isymend; isym++, ever++)
4370 {
4371 const char *name;
4372 Elf_Internal_Versym iver;
4373 unsigned short version_index;
4374
4375 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
4376 || isym->st_shndx == SHN_UNDEF)
4377 continue;
4378
4379 name = bfd_elf_string_from_elf_section (input,
4380 hdr->sh_link,
4381 isym->st_name);
4382 if (strcmp (name, h->root.root.string) != 0)
4383 continue;
4384
4385 _bfd_elf_swap_versym_in (input, ever, &iver);
4386
4387 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
4388 {
4389 /* If we have a non-hidden versioned sym, then it should
4390 have provided a definition for the undefined sym. */
4391 abort ();
4392 }
4393
4394 version_index = iver.vs_vers & VERSYM_VERSION;
4395 if (version_index == 1 || version_index == 2)
4396 {
4397 /* This is the base or first version. We can use it. */
4398 free (extversym);
4399 free (isymbuf);
4400 return TRUE;
4401 }
4402 }
4403
4404 free (extversym);
4405 free (isymbuf);
4406 }
4407
4408 return FALSE;
4409 }
4410
4411 /* Add an external symbol to the symbol table. This is called from
4412 the hash table traversal routine. When generating a shared object,
4413 we go through the symbol table twice. The first time we output
4414 anything that might have been forced to local scope in a version
4415 script. The second time we output the symbols that are still
4416 global symbols. */
4417
4418 static bfd_boolean
4419 elf_link_output_extsym (h, data)
4420 struct elf_link_hash_entry *h;
4421 PTR data;
4422 {
4423 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
4424 struct elf_final_link_info *finfo = eoinfo->finfo;
4425 bfd_boolean strip;
4426 Elf_Internal_Sym sym;
4427 asection *input_sec;
4428
4429 if (h->root.type == bfd_link_hash_warning)
4430 {
4431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4432 if (h->root.type == bfd_link_hash_new)
4433 return TRUE;
4434 }
4435
4436 /* Decide whether to output this symbol in this pass. */
4437 if (eoinfo->localsyms)
4438 {
4439 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4440 return TRUE;
4441 }
4442 else
4443 {
4444 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4445 return TRUE;
4446 }
4447
4448 /* If we are not creating a shared library, and this symbol is
4449 referenced by a shared library but is not defined anywhere, then
4450 warn that it is undefined. If we do not do this, the runtime
4451 linker will complain that the symbol is undefined when the
4452 program is run. We don't have to worry about symbols that are
4453 referenced by regular files, because we will already have issued
4454 warnings for them. */
4455 if (! finfo->info->relocatable
4456 && (finfo->info->executable
4457 || ! finfo->info->allow_shlib_undefined)
4458 && h->root.type == bfd_link_hash_undefined
4459 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4460 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
4461 && ! elf_link_check_versioned_symbol (finfo->info, h))
4462 {
4463 if (! ((*finfo->info->callbacks->undefined_symbol)
4464 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
4465 (asection *) NULL, (bfd_vma) 0, TRUE)))
4466 {
4467 eoinfo->failed = TRUE;
4468 return FALSE;
4469 }
4470 }
4471
4472 /* We should also warn if a forced local symbol is referenced from
4473 shared libraries. */
4474 if (! finfo->info->relocatable
4475 && (! finfo->info->shared || ! finfo->info->allow_shlib_undefined)
4476 && (h->elf_link_hash_flags
4477 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC
4478 | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
4479 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
4480 && ! elf_link_check_versioned_symbol (finfo->info, h))
4481 {
4482 (*_bfd_error_handler)
4483 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
4484 bfd_get_filename (finfo->output_bfd),
4485 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
4486 ? "internal"
4487 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
4488 ? "hidden" : "local",
4489 h->root.root.string,
4490 bfd_archive_filename (h->root.u.def.section->owner));
4491 eoinfo->failed = TRUE;
4492 return FALSE;
4493 }
4494
4495 /* We don't want to output symbols that have never been mentioned by
4496 a regular file, or that we have been told to strip. However, if
4497 h->indx is set to -2, the symbol is used by a reloc and we must
4498 output it. */
4499 if (h->indx == -2)
4500 strip = FALSE;
4501 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4502 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4503 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4504 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4505 strip = TRUE;
4506 else if (finfo->info->strip == strip_all)
4507 strip = TRUE;
4508 else if (finfo->info->strip == strip_some
4509 && bfd_hash_lookup (finfo->info->keep_hash,
4510 h->root.root.string, FALSE, FALSE) == NULL)
4511 strip = TRUE;
4512 else if (finfo->info->strip_discarded
4513 && (h->root.type == bfd_link_hash_defined
4514 || h->root.type == bfd_link_hash_defweak)
4515 && elf_discarded_section (h->root.u.def.section))
4516 strip = TRUE;
4517 else
4518 strip = FALSE;
4519
4520 /* If we're stripping it, and it's not a dynamic symbol, there's
4521 nothing else to do unless it is a forced local symbol. */
4522 if (strip
4523 && h->dynindx == -1
4524 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4525 return TRUE;
4526
4527 sym.st_value = 0;
4528 sym.st_size = h->size;
4529 sym.st_other = h->other;
4530 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4531 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
4532 else if (h->root.type == bfd_link_hash_undefweak
4533 || h->root.type == bfd_link_hash_defweak)
4534 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
4535 else
4536 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
4537
4538 switch (h->root.type)
4539 {
4540 default:
4541 case bfd_link_hash_new:
4542 case bfd_link_hash_warning:
4543 abort ();
4544 return FALSE;
4545
4546 case bfd_link_hash_undefined:
4547 case bfd_link_hash_undefweak:
4548 input_sec = bfd_und_section_ptr;
4549 sym.st_shndx = SHN_UNDEF;
4550 break;
4551
4552 case bfd_link_hash_defined:
4553 case bfd_link_hash_defweak:
4554 {
4555 input_sec = h->root.u.def.section;
4556 if (input_sec->output_section != NULL)
4557 {
4558 sym.st_shndx =
4559 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
4560 input_sec->output_section);
4561 if (sym.st_shndx == SHN_BAD)
4562 {
4563 (*_bfd_error_handler)
4564 (_("%s: could not find output section %s for input section %s"),
4565 bfd_get_filename (finfo->output_bfd),
4566 input_sec->output_section->name,
4567 input_sec->name);
4568 eoinfo->failed = TRUE;
4569 return FALSE;
4570 }
4571
4572 /* ELF symbols in relocatable files are section relative,
4573 but in nonrelocatable files they are virtual
4574 addresses. */
4575 sym.st_value = h->root.u.def.value + input_sec->output_offset;
4576 if (! finfo->info->relocatable)
4577 {
4578 sym.st_value += input_sec->output_section->vma;
4579 if (h->type == STT_TLS)
4580 {
4581 /* STT_TLS symbols are relative to PT_TLS segment
4582 base. */
4583 BFD_ASSERT (finfo->first_tls_sec != NULL);
4584 sym.st_value -= finfo->first_tls_sec->vma;
4585 }
4586 }
4587 }
4588 else
4589 {
4590 BFD_ASSERT (input_sec->owner == NULL
4591 || (input_sec->owner->flags & DYNAMIC) != 0);
4592 sym.st_shndx = SHN_UNDEF;
4593 input_sec = bfd_und_section_ptr;
4594 }
4595 }
4596 break;
4597
4598 case bfd_link_hash_common:
4599 input_sec = h->root.u.c.p->section;
4600 sym.st_shndx = SHN_COMMON;
4601 sym.st_value = 1 << h->root.u.c.p->alignment_power;
4602 break;
4603
4604 case bfd_link_hash_indirect:
4605 /* These symbols are created by symbol versioning. They point
4606 to the decorated version of the name. For example, if the
4607 symbol foo@@GNU_1.2 is the default, which should be used when
4608 foo is used with no version, then we add an indirect symbol
4609 foo which points to foo@@GNU_1.2. We ignore these symbols,
4610 since the indirected symbol is already in the hash table. */
4611 return TRUE;
4612 }
4613
4614 /* Give the processor backend a chance to tweak the symbol value,
4615 and also to finish up anything that needs to be done for this
4616 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
4617 forced local syms when non-shared is due to a historical quirk. */
4618 if ((h->dynindx != -1
4619 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4620 && ((finfo->info->shared
4621 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4622 || h->root.type != bfd_link_hash_undefweak))
4623 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4624 && elf_hash_table (finfo->info)->dynamic_sections_created)
4625 {
4626 struct elf_backend_data *bed;
4627
4628 bed = get_elf_backend_data (finfo->output_bfd);
4629 if (! ((*bed->elf_backend_finish_dynamic_symbol)
4630 (finfo->output_bfd, finfo->info, h, &sym)))
4631 {
4632 eoinfo->failed = TRUE;
4633 return FALSE;
4634 }
4635 }
4636
4637 /* If we are marking the symbol as undefined, and there are no
4638 non-weak references to this symbol from a regular object, then
4639 mark the symbol as weak undefined; if there are non-weak
4640 references, mark the symbol as strong. We can't do this earlier,
4641 because it might not be marked as undefined until the
4642 finish_dynamic_symbol routine gets through with it. */
4643 if (sym.st_shndx == SHN_UNDEF
4644 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
4645 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
4646 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
4647 {
4648 int bindtype;
4649
4650 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
4651 bindtype = STB_GLOBAL;
4652 else
4653 bindtype = STB_WEAK;
4654 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
4655 }
4656
4657 /* If a non-weak symbol with non-default visibility is not defined
4658 locally, it is a fatal error. */
4659 if (! finfo->info->relocatable
4660 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
4661 && ELF_ST_BIND (sym.st_info) != STB_WEAK
4662 && h->root.type == bfd_link_hash_undefined
4663 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4664 {
4665 (*_bfd_error_handler)
4666 (_("%s: %s symbol `%s' isn't defined"),
4667 bfd_get_filename (finfo->output_bfd),
4668 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
4669 ? "protected"
4670 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
4671 ? "internal" : "hidden",
4672 h->root.root.string);
4673 eoinfo->failed = TRUE;
4674 return FALSE;
4675 }
4676
4677 /* If this symbol should be put in the .dynsym section, then put it
4678 there now. We already know the symbol index. We also fill in
4679 the entry in the .hash section. */
4680 if (h->dynindx != -1
4681 && elf_hash_table (finfo->info)->dynamic_sections_created)
4682 {
4683 size_t bucketcount;
4684 size_t bucket;
4685 size_t hash_entry_size;
4686 bfd_byte *bucketpos;
4687 bfd_vma chain;
4688 Elf_External_Sym *esym;
4689
4690 sym.st_name = h->dynstr_index;
4691 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
4692 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
4693
4694 bucketcount = elf_hash_table (finfo->info)->bucketcount;
4695 bucket = h->elf_hash_value % bucketcount;
4696 hash_entry_size
4697 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
4698 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
4699 + (bucket + 2) * hash_entry_size);
4700 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
4701 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
4702 bucketpos);
4703 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
4704 ((bfd_byte *) finfo->hash_sec->contents
4705 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
4706
4707 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
4708 {
4709 Elf_Internal_Versym iversym;
4710 Elf_External_Versym *eversym;
4711
4712 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4713 {
4714 if (h->verinfo.verdef == NULL)
4715 iversym.vs_vers = 0;
4716 else
4717 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
4718 }
4719 else
4720 {
4721 if (h->verinfo.vertree == NULL)
4722 iversym.vs_vers = 1;
4723 else
4724 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
4725 }
4726
4727 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
4728 iversym.vs_vers |= VERSYM_HIDDEN;
4729
4730 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
4731 eversym += h->dynindx;
4732 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
4733 }
4734 }
4735
4736 /* If we're stripping it, then it was just a dynamic symbol, and
4737 there's nothing else to do. */
4738 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
4739 return TRUE;
4740
4741 h->indx = bfd_get_symcount (finfo->output_bfd);
4742
4743 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
4744 {
4745 eoinfo->failed = TRUE;
4746 return FALSE;
4747 }
4748
4749 return TRUE;
4750 }
4751
4752 /* Link an input file into the linker output file. This function
4753 handles all the sections and relocations of the input file at once.
4754 This is so that we only have to read the local symbols once, and
4755 don't have to keep them in memory. */
4756
4757 static bfd_boolean
4758 elf_link_input_bfd (finfo, input_bfd)
4759 struct elf_final_link_info *finfo;
4760 bfd *input_bfd;
4761 {
4762 bfd_boolean (*relocate_section)
4763 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
4764 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
4765 bfd *output_bfd;
4766 Elf_Internal_Shdr *symtab_hdr;
4767 size_t locsymcount;
4768 size_t extsymoff;
4769 Elf_Internal_Sym *isymbuf;
4770 Elf_Internal_Sym *isym;
4771 Elf_Internal_Sym *isymend;
4772 long *pindex;
4773 asection **ppsection;
4774 asection *o;
4775 struct elf_backend_data *bed;
4776 bfd_boolean emit_relocs;
4777 struct elf_link_hash_entry **sym_hashes;
4778
4779 output_bfd = finfo->output_bfd;
4780 bed = get_elf_backend_data (output_bfd);
4781 relocate_section = bed->elf_backend_relocate_section;
4782
4783 /* If this is a dynamic object, we don't want to do anything here:
4784 we don't want the local symbols, and we don't want the section
4785 contents. */
4786 if ((input_bfd->flags & DYNAMIC) != 0)
4787 return TRUE;
4788
4789 emit_relocs = (finfo->info->relocatable
4790 || finfo->info->emitrelocations
4791 || bed->elf_backend_emit_relocs);
4792
4793 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4794 if (elf_bad_symtab (input_bfd))
4795 {
4796 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
4797 extsymoff = 0;
4798 }
4799 else
4800 {
4801 locsymcount = symtab_hdr->sh_info;
4802 extsymoff = symtab_hdr->sh_info;
4803 }
4804
4805 /* Read the local symbols. */
4806 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4807 if (isymbuf == NULL && locsymcount != 0)
4808 {
4809 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
4810 finfo->internal_syms,
4811 finfo->external_syms,
4812 finfo->locsym_shndx);
4813 if (isymbuf == NULL)
4814 return FALSE;
4815 }
4816
4817 /* Find local symbol sections and adjust values of symbols in
4818 SEC_MERGE sections. Write out those local symbols we know are
4819 going into the output file. */
4820 isymend = isymbuf + locsymcount;
4821 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
4822 isym < isymend;
4823 isym++, pindex++, ppsection++)
4824 {
4825 asection *isec;
4826 const char *name;
4827 Elf_Internal_Sym osym;
4828
4829 *pindex = -1;
4830
4831 if (elf_bad_symtab (input_bfd))
4832 {
4833 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
4834 {
4835 *ppsection = NULL;
4836 continue;
4837 }
4838 }
4839
4840 if (isym->st_shndx == SHN_UNDEF)
4841 isec = bfd_und_section_ptr;
4842 else if (isym->st_shndx < SHN_LORESERVE
4843 || isym->st_shndx > SHN_HIRESERVE)
4844 {
4845 isec = section_from_elf_index (input_bfd, isym->st_shndx);
4846 if (isec
4847 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
4848 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
4849 isym->st_value =
4850 _bfd_merged_section_offset (output_bfd, &isec,
4851 elf_section_data (isec)->sec_info,
4852 isym->st_value, (bfd_vma) 0);
4853 }
4854 else if (isym->st_shndx == SHN_ABS)
4855 isec = bfd_abs_section_ptr;
4856 else if (isym->st_shndx == SHN_COMMON)
4857 isec = bfd_com_section_ptr;
4858 else
4859 {
4860 /* Who knows? */
4861 isec = NULL;
4862 }
4863
4864 *ppsection = isec;
4865
4866 /* Don't output the first, undefined, symbol. */
4867 if (ppsection == finfo->sections)
4868 continue;
4869
4870 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4871 {
4872 /* We never output section symbols. Instead, we use the
4873 section symbol of the corresponding section in the output
4874 file. */
4875 continue;
4876 }
4877
4878 /* If we are stripping all symbols, we don't want to output this
4879 one. */
4880 if (finfo->info->strip == strip_all)
4881 continue;
4882
4883 /* If we are discarding all local symbols, we don't want to
4884 output this one. If we are generating a relocatable output
4885 file, then some of the local symbols may be required by
4886 relocs; we output them below as we discover that they are
4887 needed. */
4888 if (finfo->info->discard == discard_all)
4889 continue;
4890
4891 /* If this symbol is defined in a section which we are
4892 discarding, we don't need to keep it, but note that
4893 linker_mark is only reliable for sections that have contents.
4894 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
4895 as well as linker_mark. */
4896 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
4897 && isec != NULL
4898 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
4899 || (! finfo->info->relocatable
4900 && (isec->flags & SEC_EXCLUDE) != 0)))
4901 continue;
4902
4903 /* Get the name of the symbol. */
4904 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
4905 isym->st_name);
4906 if (name == NULL)
4907 return FALSE;
4908
4909 /* See if we are discarding symbols with this name. */
4910 if ((finfo->info->strip == strip_some
4911 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4912 == NULL))
4913 || (((finfo->info->discard == discard_sec_merge
4914 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
4915 || finfo->info->discard == discard_l)
4916 && bfd_is_local_label_name (input_bfd, name)))
4917 continue;
4918
4919 /* If we get here, we are going to output this symbol. */
4920
4921 osym = *isym;
4922
4923 /* Adjust the section index for the output file. */
4924 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
4925 isec->output_section);
4926 if (osym.st_shndx == SHN_BAD)
4927 return FALSE;
4928
4929 *pindex = bfd_get_symcount (output_bfd);
4930
4931 /* ELF symbols in relocatable files are section relative, but
4932 in executable files they are virtual addresses. Note that
4933 this code assumes that all ELF sections have an associated
4934 BFD section with a reasonable value for output_offset; below
4935 we assume that they also have a reasonable value for
4936 output_section. Any special sections must be set up to meet
4937 these requirements. */
4938 osym.st_value += isec->output_offset;
4939 if (! finfo->info->relocatable)
4940 {
4941 osym.st_value += isec->output_section->vma;
4942 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
4943 {
4944 /* STT_TLS symbols are relative to PT_TLS segment base. */
4945 BFD_ASSERT (finfo->first_tls_sec != NULL);
4946 osym.st_value -= finfo->first_tls_sec->vma;
4947 }
4948 }
4949
4950 if (! elf_link_output_sym (finfo, name, &osym, isec))
4951 return FALSE;
4952 }
4953
4954 /* Relocate the contents of each section. */
4955 sym_hashes = elf_sym_hashes (input_bfd);
4956 for (o = input_bfd->sections; o != NULL; o = o->next)
4957 {
4958 bfd_byte *contents;
4959
4960 if (! o->linker_mark)
4961 {
4962 /* This section was omitted from the link. */
4963 continue;
4964 }
4965
4966 if ((o->flags & SEC_HAS_CONTENTS) == 0
4967 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
4968 continue;
4969
4970 if ((o->flags & SEC_LINKER_CREATED) != 0)
4971 {
4972 /* Section was created by _bfd_elf_link_create_dynamic_sections
4973 or somesuch. */
4974 continue;
4975 }
4976
4977 /* Get the contents of the section. They have been cached by a
4978 relaxation routine. Note that o is a section in an input
4979 file, so the contents field will not have been set by any of
4980 the routines which work on output files. */
4981 if (elf_section_data (o)->this_hdr.contents != NULL)
4982 contents = elf_section_data (o)->this_hdr.contents;
4983 else
4984 {
4985 contents = finfo->contents;
4986 if (! bfd_get_section_contents (input_bfd, o, contents,
4987 (file_ptr) 0, o->_raw_size))
4988 return FALSE;
4989 }
4990
4991 if ((o->flags & SEC_RELOC) != 0)
4992 {
4993 Elf_Internal_Rela *internal_relocs;
4994
4995 /* Get the swapped relocs. */
4996 internal_relocs
4997 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
4998 finfo->internal_relocs, FALSE);
4999 if (internal_relocs == NULL
5000 && o->reloc_count > 0)
5001 return FALSE;
5002
5003 /* Run through the relocs looking for any against symbols
5004 from discarded sections and section symbols from
5005 removed link-once sections. Complain about relocs
5006 against discarded sections. Zero relocs against removed
5007 link-once sections. Preserve debug information as much
5008 as we can. */
5009 if (!elf_section_ignore_discarded_relocs (o))
5010 {
5011 Elf_Internal_Rela *rel, *relend;
5012
5013 rel = internal_relocs;
5014 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
5015 for ( ; rel < relend; rel++)
5016 {
5017 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
5018 asection *sec;
5019
5020 if (r_symndx >= locsymcount
5021 || (elf_bad_symtab (input_bfd)
5022 && finfo->sections[r_symndx] == NULL))
5023 {
5024 struct elf_link_hash_entry *h;
5025
5026 h = sym_hashes[r_symndx - extsymoff];
5027 while (h->root.type == bfd_link_hash_indirect
5028 || h->root.type == bfd_link_hash_warning)
5029 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5030
5031 /* Complain if the definition comes from a
5032 discarded section. */
5033 sec = h->root.u.def.section;
5034 if ((h->root.type == bfd_link_hash_defined
5035 || h->root.type == bfd_link_hash_defweak)
5036 && elf_discarded_section (sec))
5037 {
5038 if ((o->flags & SEC_DEBUGGING) != 0)
5039 {
5040 BFD_ASSERT (r_symndx != 0);
5041 /* Try to preserve debug information. */
5042 if ((o->flags & SEC_DEBUGGING) != 0
5043 && sec->kept_section != NULL
5044 && sec->_raw_size == sec->kept_section->_raw_size)
5045 h->root.u.def.section
5046 = sec->kept_section;
5047 else
5048 memset (rel, 0, sizeof (*rel));
5049 }
5050 else
5051 finfo->info->callbacks->error_handler
5052 (LD_DEFINITION_IN_DISCARDED_SECTION,
5053 _("%T: discarded in section `%s' from %s\n"),
5054 h->root.root.string,
5055 h->root.root.string,
5056 h->root.u.def.section->name,
5057 bfd_archive_filename (h->root.u.def.section->owner));
5058 }
5059 }
5060 else
5061 {
5062 sec = finfo->sections[r_symndx];
5063
5064 if (sec != NULL && elf_discarded_section (sec))
5065 {
5066 if ((o->flags & SEC_DEBUGGING) != 0
5067 || (sec->flags & SEC_LINK_ONCE) != 0)
5068 {
5069 BFD_ASSERT (r_symndx != 0);
5070 /* Try to preserve debug information. */
5071 if ((o->flags & SEC_DEBUGGING) != 0
5072 && sec->kept_section != NULL
5073 && sec->_raw_size == sec->kept_section->_raw_size)
5074 finfo->sections[r_symndx]
5075 = sec->kept_section;
5076 else
5077 {
5078 rel->r_info
5079 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
5080 rel->r_addend = 0;
5081 }
5082 }
5083 else
5084 {
5085 static int count;
5086 int ok;
5087 char *buf;
5088
5089 ok = asprintf (&buf, "local symbol %d",
5090 count++);
5091 if (ok <= 0)
5092 buf = (char *) "local symbol";
5093 finfo->info->callbacks->error_handler
5094 (LD_DEFINITION_IN_DISCARDED_SECTION,
5095 _("%T: discarded in section `%s' from %s\n"),
5096 buf, buf, sec->name,
5097 bfd_archive_filename (input_bfd));
5098 if (ok != -1)
5099 free (buf);
5100 }
5101 }
5102 }
5103 }
5104 }
5105
5106 /* Relocate the section by invoking a back end routine.
5107
5108 The back end routine is responsible for adjusting the
5109 section contents as necessary, and (if using Rela relocs
5110 and generating a relocatable output file) adjusting the
5111 reloc addend as necessary.
5112
5113 The back end routine does not have to worry about setting
5114 the reloc address or the reloc symbol index.
5115
5116 The back end routine is given a pointer to the swapped in
5117 internal symbols, and can access the hash table entries
5118 for the external symbols via elf_sym_hashes (input_bfd).
5119
5120 When generating relocatable output, the back end routine
5121 must handle STB_LOCAL/STT_SECTION symbols specially. The
5122 output symbol is going to be a section symbol
5123 corresponding to the output section, which will require
5124 the addend to be adjusted. */
5125
5126 if (! (*relocate_section) (output_bfd, finfo->info,
5127 input_bfd, o, contents,
5128 internal_relocs,
5129 isymbuf,
5130 finfo->sections))
5131 return FALSE;
5132
5133 if (emit_relocs)
5134 {
5135 Elf_Internal_Rela *irela;
5136 Elf_Internal_Rela *irelaend;
5137 bfd_vma last_offset;
5138 struct elf_link_hash_entry **rel_hash;
5139 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
5140 unsigned int next_erel;
5141 bfd_boolean (*reloc_emitter)
5142 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *,
5143 Elf_Internal_Rela *));
5144 bfd_boolean rela_normal;
5145
5146 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5147 rela_normal = (bed->rela_normal
5148 && (input_rel_hdr->sh_entsize
5149 == sizeof (Elf_External_Rela)));
5150
5151 /* Adjust the reloc addresses and symbol indices. */
5152
5153 irela = internal_relocs;
5154 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
5155 rel_hash = (elf_section_data (o->output_section)->rel_hashes
5156 + elf_section_data (o->output_section)->rel_count
5157 + elf_section_data (o->output_section)->rel_count2);
5158 last_offset = o->output_offset;
5159 if (!finfo->info->relocatable)
5160 last_offset += o->output_section->vma;
5161 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
5162 {
5163 unsigned long r_symndx;
5164 asection *sec;
5165 Elf_Internal_Sym sym;
5166
5167 if (next_erel == bed->s->int_rels_per_ext_rel)
5168 {
5169 rel_hash++;
5170 next_erel = 0;
5171 }
5172
5173 irela->r_offset = _bfd_elf_section_offset (output_bfd,
5174 finfo->info, o,
5175 irela->r_offset);
5176 if (irela->r_offset >= (bfd_vma) -2)
5177 {
5178 /* This is a reloc for a deleted entry or somesuch.
5179 Turn it into an R_*_NONE reloc, at the same
5180 offset as the last reloc. elf_eh_frame.c and
5181 elf_bfd_discard_info rely on reloc offsets
5182 being ordered. */
5183 irela->r_offset = last_offset;
5184 irela->r_info = 0;
5185 irela->r_addend = 0;
5186 continue;
5187 }
5188
5189 irela->r_offset += o->output_offset;
5190
5191 /* Relocs in an executable have to be virtual addresses. */
5192 if (!finfo->info->relocatable)
5193 irela->r_offset += o->output_section->vma;
5194
5195 last_offset = irela->r_offset;
5196
5197 r_symndx = ELF_R_SYM (irela->r_info);
5198 if (r_symndx == STN_UNDEF)
5199 continue;
5200
5201 if (r_symndx >= locsymcount
5202 || (elf_bad_symtab (input_bfd)
5203 && finfo->sections[r_symndx] == NULL))
5204 {
5205 struct elf_link_hash_entry *rh;
5206 unsigned long indx;
5207
5208 /* This is a reloc against a global symbol. We
5209 have not yet output all the local symbols, so
5210 we do not know the symbol index of any global
5211 symbol. We set the rel_hash entry for this
5212 reloc to point to the global hash table entry
5213 for this symbol. The symbol index is then
5214 set at the end of elf_bfd_final_link. */
5215 indx = r_symndx - extsymoff;
5216 rh = elf_sym_hashes (input_bfd)[indx];
5217 while (rh->root.type == bfd_link_hash_indirect
5218 || rh->root.type == bfd_link_hash_warning)
5219 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
5220
5221 /* Setting the index to -2 tells
5222 elf_link_output_extsym that this symbol is
5223 used by a reloc. */
5224 BFD_ASSERT (rh->indx < 0);
5225 rh->indx = -2;
5226
5227 *rel_hash = rh;
5228
5229 continue;
5230 }
5231
5232 /* This is a reloc against a local symbol. */
5233
5234 *rel_hash = NULL;
5235 sym = isymbuf[r_symndx];
5236 sec = finfo->sections[r_symndx];
5237 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
5238 {
5239 /* I suppose the backend ought to fill in the
5240 section of any STT_SECTION symbol against a
5241 processor specific section. If we have
5242 discarded a section, the output_section will
5243 be the absolute section. */
5244 if (bfd_is_abs_section (sec)
5245 || (sec != NULL
5246 && bfd_is_abs_section (sec->output_section)))
5247 r_symndx = 0;
5248 else if (sec == NULL || sec->owner == NULL)
5249 {
5250 bfd_set_error (bfd_error_bad_value);
5251 return FALSE;
5252 }
5253 else
5254 {
5255 r_symndx = sec->output_section->target_index;
5256 BFD_ASSERT (r_symndx != 0);
5257 }
5258
5259 /* Adjust the addend according to where the
5260 section winds up in the output section. */
5261 if (rela_normal)
5262 irela->r_addend += sec->output_offset;
5263 }
5264 else
5265 {
5266 if (finfo->indices[r_symndx] == -1)
5267 {
5268 unsigned long shlink;
5269 const char *name;
5270 asection *osec;
5271
5272 if (finfo->info->strip == strip_all)
5273 {
5274 /* You can't do ld -r -s. */
5275 bfd_set_error (bfd_error_invalid_operation);
5276 return FALSE;
5277 }
5278
5279 /* This symbol was skipped earlier, but
5280 since it is needed by a reloc, we
5281 must output it now. */
5282 shlink = symtab_hdr->sh_link;
5283 name = (bfd_elf_string_from_elf_section
5284 (input_bfd, shlink, sym.st_name));
5285 if (name == NULL)
5286 return FALSE;
5287
5288 osec = sec->output_section;
5289 sym.st_shndx =
5290 _bfd_elf_section_from_bfd_section (output_bfd,
5291 osec);
5292 if (sym.st_shndx == SHN_BAD)
5293 return FALSE;
5294
5295 sym.st_value += sec->output_offset;
5296 if (! finfo->info->relocatable)
5297 {
5298 sym.st_value += osec->vma;
5299 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
5300 {
5301 /* STT_TLS symbols are relative to PT_TLS
5302 segment base. */
5303 BFD_ASSERT (finfo->first_tls_sec != NULL);
5304 sym.st_value -= finfo->first_tls_sec->vma;
5305 }
5306 }
5307
5308 finfo->indices[r_symndx]
5309 = bfd_get_symcount (output_bfd);
5310
5311 if (! elf_link_output_sym (finfo, name, &sym, sec))
5312 return FALSE;
5313 }
5314
5315 r_symndx = finfo->indices[r_symndx];
5316 }
5317
5318 irela->r_info = ELF_R_INFO (r_symndx,
5319 ELF_R_TYPE (irela->r_info));
5320 }
5321
5322 /* Swap out the relocs. */
5323 if (bed->elf_backend_emit_relocs
5324 && !(finfo->info->relocatable
5325 || finfo->info->emitrelocations))
5326 reloc_emitter = bed->elf_backend_emit_relocs;
5327 else
5328 reloc_emitter = _bfd_elf_link_output_relocs;
5329
5330 if (input_rel_hdr->sh_size != 0
5331 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
5332 internal_relocs))
5333 return FALSE;
5334
5335 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
5336 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
5337 {
5338 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
5339 * bed->s->int_rels_per_ext_rel);
5340 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
5341 internal_relocs))
5342 return FALSE;
5343 }
5344 }
5345 }
5346
5347 /* Write out the modified section contents. */
5348 if (bed->elf_backend_write_section
5349 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
5350 {
5351 /* Section written out. */
5352 }
5353 else switch (o->sec_info_type)
5354 {
5355 case ELF_INFO_TYPE_STABS:
5356 if (! (_bfd_write_section_stabs
5357 (output_bfd,
5358 &elf_hash_table (finfo->info)->stab_info,
5359 o, &elf_section_data (o)->sec_info, contents)))
5360 return FALSE;
5361 break;
5362 case ELF_INFO_TYPE_MERGE:
5363 if (! _bfd_write_merged_section (output_bfd, o,
5364 elf_section_data (o)->sec_info))
5365 return FALSE;
5366 break;
5367 case ELF_INFO_TYPE_EH_FRAME:
5368 {
5369 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
5370 o, contents))
5371 return FALSE;
5372 }
5373 break;
5374 default:
5375 {
5376 bfd_size_type sec_size;
5377
5378 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
5379 if (! (o->flags & SEC_EXCLUDE)
5380 && ! bfd_set_section_contents (output_bfd, o->output_section,
5381 contents,
5382 (file_ptr) o->output_offset,
5383 sec_size))
5384 return FALSE;
5385 }
5386 break;
5387 }
5388 }
5389
5390 return TRUE;
5391 }
5392
5393 /* Generate a reloc when linking an ELF file. This is a reloc
5394 requested by the linker, and does come from any input file. This
5395 is used to build constructor and destructor tables when linking
5396 with -Ur. */
5397
5398 static bfd_boolean
5399 elf_reloc_link_order (output_bfd, info, output_section, link_order)
5400 bfd *output_bfd;
5401 struct bfd_link_info *info;
5402 asection *output_section;
5403 struct bfd_link_order *link_order;
5404 {
5405 reloc_howto_type *howto;
5406 long indx;
5407 bfd_vma offset;
5408 bfd_vma addend;
5409 struct elf_link_hash_entry **rel_hash_ptr;
5410 Elf_Internal_Shdr *rel_hdr;
5411 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
5412 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
5413 bfd_byte *erel;
5414 unsigned int i;
5415
5416 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5417 if (howto == NULL)
5418 {
5419 bfd_set_error (bfd_error_bad_value);
5420 return FALSE;
5421 }
5422
5423 addend = link_order->u.reloc.p->addend;
5424
5425 /* Figure out the symbol index. */
5426 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
5427 + elf_section_data (output_section)->rel_count
5428 + elf_section_data (output_section)->rel_count2);
5429 if (link_order->type == bfd_section_reloc_link_order)
5430 {
5431 indx = link_order->u.reloc.p->u.section->target_index;
5432 BFD_ASSERT (indx != 0);
5433 *rel_hash_ptr = NULL;
5434 }
5435 else
5436 {
5437 struct elf_link_hash_entry *h;
5438
5439 /* Treat a reloc against a defined symbol as though it were
5440 actually against the section. */
5441 h = ((struct elf_link_hash_entry *)
5442 bfd_wrapped_link_hash_lookup (output_bfd, info,
5443 link_order->u.reloc.p->u.name,
5444 FALSE, FALSE, TRUE));
5445 if (h != NULL
5446 && (h->root.type == bfd_link_hash_defined
5447 || h->root.type == bfd_link_hash_defweak))
5448 {
5449 asection *section;
5450
5451 section = h->root.u.def.section;
5452 indx = section->output_section->target_index;
5453 *rel_hash_ptr = NULL;
5454 /* It seems that we ought to add the symbol value to the
5455 addend here, but in practice it has already been added
5456 because it was passed to constructor_callback. */
5457 addend += section->output_section->vma + section->output_offset;
5458 }
5459 else if (h != NULL)
5460 {
5461 /* Setting the index to -2 tells elf_link_output_extsym that
5462 this symbol is used by a reloc. */
5463 h->indx = -2;
5464 *rel_hash_ptr = h;
5465 indx = 0;
5466 }
5467 else
5468 {
5469 if (! ((*info->callbacks->unattached_reloc)
5470 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5471 (asection *) NULL, (bfd_vma) 0)))
5472 return FALSE;
5473 indx = 0;
5474 }
5475 }
5476
5477 /* If this is an inplace reloc, we must write the addend into the
5478 object file. */
5479 if (howto->partial_inplace && addend != 0)
5480 {
5481 bfd_size_type size;
5482 bfd_reloc_status_type rstat;
5483 bfd_byte *buf;
5484 bfd_boolean ok;
5485 const char *sym_name;
5486
5487 size = bfd_get_reloc_size (howto);
5488 buf = (bfd_byte *) bfd_zmalloc (size);
5489 if (buf == (bfd_byte *) NULL)
5490 return FALSE;
5491 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
5492 switch (rstat)
5493 {
5494 case bfd_reloc_ok:
5495 break;
5496
5497 default:
5498 case bfd_reloc_outofrange:
5499 abort ();
5500
5501 case bfd_reloc_overflow:
5502 if (link_order->type == bfd_section_reloc_link_order)
5503 sym_name = bfd_section_name (output_bfd,
5504 link_order->u.reloc.p->u.section);
5505 else
5506 sym_name = link_order->u.reloc.p->u.name;
5507 if (! ((*info->callbacks->reloc_overflow)
5508 (info, sym_name, howto->name, addend,
5509 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
5510 {
5511 free (buf);
5512 return FALSE;
5513 }
5514 break;
5515 }
5516 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5517 (file_ptr) link_order->offset, size);
5518 free (buf);
5519 if (! ok)
5520 return FALSE;
5521 }
5522
5523 /* The address of a reloc is relative to the section in a
5524 relocatable file, and is a virtual address in an executable
5525 file. */
5526 offset = link_order->offset;
5527 if (! info->relocatable)
5528 offset += output_section->vma;
5529
5530 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
5531 {
5532 irel[i].r_offset = offset;
5533 irel[i].r_info = 0;
5534 irel[i].r_addend = 0;
5535 }
5536 irel[0].r_info = ELF_R_INFO (indx, howto->type);
5537
5538 rel_hdr = &elf_section_data (output_section)->rel_hdr;
5539 erel = rel_hdr->contents;
5540 if (rel_hdr->sh_type == SHT_REL)
5541 {
5542 erel += (elf_section_data (output_section)->rel_count
5543 * sizeof (Elf_External_Rel));
5544 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
5545 }
5546 else
5547 {
5548 irel[0].r_addend = addend;
5549 erel += (elf_section_data (output_section)->rel_count
5550 * sizeof (Elf_External_Rela));
5551 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
5552 }
5553
5554 ++elf_section_data (output_section)->rel_count;
5555
5556 return TRUE;
5557 }
5558 \f
5559 /* Garbage collect unused sections. */
5560
5561 static bfd_boolean elf_gc_mark
5562 PARAMS ((struct bfd_link_info *, asection *,
5563 asection * (*) (asection *, struct bfd_link_info *,
5564 Elf_Internal_Rela *, struct elf_link_hash_entry *,
5565 Elf_Internal_Sym *)));
5566
5567 static bfd_boolean elf_gc_sweep
5568 PARAMS ((struct bfd_link_info *,
5569 bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *,
5570 const Elf_Internal_Rela *)));
5571
5572 static bfd_boolean elf_gc_sweep_symbol
5573 PARAMS ((struct elf_link_hash_entry *, PTR));
5574
5575 static bfd_boolean elf_gc_allocate_got_offsets
5576 PARAMS ((struct elf_link_hash_entry *, PTR));
5577
5578 static bfd_boolean elf_gc_propagate_vtable_entries_used
5579 PARAMS ((struct elf_link_hash_entry *, PTR));
5580
5581 static bfd_boolean elf_gc_smash_unused_vtentry_relocs
5582 PARAMS ((struct elf_link_hash_entry *, PTR));
5583
5584 /* The mark phase of garbage collection. For a given section, mark
5585 it and any sections in this section's group, and all the sections
5586 which define symbols to which it refers. */
5587
5588 typedef asection * (*gc_mark_hook_fn)
5589 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5590 struct elf_link_hash_entry *, Elf_Internal_Sym *));
5591
5592 static bfd_boolean
5593 elf_gc_mark (info, sec, gc_mark_hook)
5594 struct bfd_link_info *info;
5595 asection *sec;
5596 gc_mark_hook_fn gc_mark_hook;
5597 {
5598 bfd_boolean ret;
5599 asection *group_sec;
5600
5601 sec->gc_mark = 1;
5602
5603 /* Mark all the sections in the group. */
5604 group_sec = elf_section_data (sec)->next_in_group;
5605 if (group_sec && !group_sec->gc_mark)
5606 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
5607 return FALSE;
5608
5609 /* Look through the section relocs. */
5610 ret = TRUE;
5611 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
5612 {
5613 Elf_Internal_Rela *relstart, *rel, *relend;
5614 Elf_Internal_Shdr *symtab_hdr;
5615 struct elf_link_hash_entry **sym_hashes;
5616 size_t nlocsyms;
5617 size_t extsymoff;
5618 bfd *input_bfd = sec->owner;
5619 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
5620 Elf_Internal_Sym *isym = NULL;
5621
5622 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5623 sym_hashes = elf_sym_hashes (input_bfd);
5624
5625 /* Read the local symbols. */
5626 if (elf_bad_symtab (input_bfd))
5627 {
5628 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
5629 extsymoff = 0;
5630 }
5631 else
5632 extsymoff = nlocsyms = symtab_hdr->sh_info;
5633
5634 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
5635 if (isym == NULL && nlocsyms != 0)
5636 {
5637 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
5638 NULL, NULL, NULL);
5639 if (isym == NULL)
5640 return FALSE;
5641 }
5642
5643 /* Read the relocations. */
5644 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL,
5645 (Elf_Internal_Rela *) NULL,
5646 info->keep_memory);
5647 if (relstart == NULL)
5648 {
5649 ret = FALSE;
5650 goto out1;
5651 }
5652 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5653
5654 for (rel = relstart; rel < relend; rel++)
5655 {
5656 unsigned long r_symndx;
5657 asection *rsec;
5658 struct elf_link_hash_entry *h;
5659
5660 r_symndx = ELF_R_SYM (rel->r_info);
5661 if (r_symndx == 0)
5662 continue;
5663
5664 if (r_symndx >= nlocsyms
5665 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
5666 {
5667 h = sym_hashes[r_symndx - extsymoff];
5668 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
5669 }
5670 else
5671 {
5672 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
5673 }
5674
5675 if (rsec && !rsec->gc_mark)
5676 {
5677 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
5678 rsec->gc_mark = 1;
5679 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
5680 {
5681 ret = FALSE;
5682 goto out2;
5683 }
5684 }
5685 }
5686
5687 out2:
5688 if (elf_section_data (sec)->relocs != relstart)
5689 free (relstart);
5690 out1:
5691 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
5692 {
5693 if (! info->keep_memory)
5694 free (isym);
5695 else
5696 symtab_hdr->contents = (unsigned char *) isym;
5697 }
5698 }
5699
5700 return ret;
5701 }
5702
5703 /* The sweep phase of garbage collection. Remove all garbage sections. */
5704
5705 typedef bfd_boolean (*gc_sweep_hook_fn)
5706 PARAMS ((bfd *, struct bfd_link_info *, asection *,
5707 const Elf_Internal_Rela *));
5708
5709 static bfd_boolean
5710 elf_gc_sweep (info, gc_sweep_hook)
5711 struct bfd_link_info *info;
5712 gc_sweep_hook_fn gc_sweep_hook;
5713 {
5714 bfd *sub;
5715
5716 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5717 {
5718 asection *o;
5719
5720 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5721 continue;
5722
5723 for (o = sub->sections; o != NULL; o = o->next)
5724 {
5725 /* Keep special sections. Keep .debug sections. */
5726 if ((o->flags & SEC_LINKER_CREATED)
5727 || (o->flags & SEC_DEBUGGING))
5728 o->gc_mark = 1;
5729
5730 if (o->gc_mark)
5731 continue;
5732
5733 /* Skip sweeping sections already excluded. */
5734 if (o->flags & SEC_EXCLUDE)
5735 continue;
5736
5737 /* Since this is early in the link process, it is simple
5738 to remove a section from the output. */
5739 o->flags |= SEC_EXCLUDE;
5740
5741 /* But we also have to update some of the relocation
5742 info we collected before. */
5743 if (gc_sweep_hook
5744 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
5745 {
5746 Elf_Internal_Rela *internal_relocs;
5747 bfd_boolean r;
5748
5749 internal_relocs
5750 = _bfd_elf_link_read_relocs (o->owner, o, NULL,
5751 (Elf_Internal_Rela *) NULL,
5752 info->keep_memory);
5753 if (internal_relocs == NULL)
5754 return FALSE;
5755
5756 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
5757
5758 if (elf_section_data (o)->relocs != internal_relocs)
5759 free (internal_relocs);
5760
5761 if (!r)
5762 return FALSE;
5763 }
5764 }
5765 }
5766
5767 /* Remove the symbols that were in the swept sections from the dynamic
5768 symbol table. GCFIXME: Anyone know how to get them out of the
5769 static symbol table as well? */
5770 {
5771 int i = 0;
5772
5773 elf_link_hash_traverse (elf_hash_table (info),
5774 elf_gc_sweep_symbol,
5775 (PTR) &i);
5776
5777 elf_hash_table (info)->dynsymcount = i;
5778 }
5779
5780 return TRUE;
5781 }
5782
5783 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5784
5785 static bfd_boolean
5786 elf_gc_sweep_symbol (h, idxptr)
5787 struct elf_link_hash_entry *h;
5788 PTR idxptr;
5789 {
5790 int *idx = (int *) idxptr;
5791
5792 if (h->root.type == bfd_link_hash_warning)
5793 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5794
5795 if (h->dynindx != -1
5796 && ((h->root.type != bfd_link_hash_defined
5797 && h->root.type != bfd_link_hash_defweak)
5798 || h->root.u.def.section->gc_mark))
5799 h->dynindx = (*idx)++;
5800
5801 return TRUE;
5802 }
5803
5804 /* Propogate collected vtable information. This is called through
5805 elf_link_hash_traverse. */
5806
5807 static bfd_boolean
5808 elf_gc_propagate_vtable_entries_used (h, okp)
5809 struct elf_link_hash_entry *h;
5810 PTR okp;
5811 {
5812 if (h->root.type == bfd_link_hash_warning)
5813 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5814
5815 /* Those that are not vtables. */
5816 if (h->vtable_parent == NULL)
5817 return TRUE;
5818
5819 /* Those vtables that do not have parents, we cannot merge. */
5820 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
5821 return TRUE;
5822
5823 /* If we've already been done, exit. */
5824 if (h->vtable_entries_used && h->vtable_entries_used[-1])
5825 return TRUE;
5826
5827 /* Make sure the parent's table is up to date. */
5828 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
5829
5830 if (h->vtable_entries_used == NULL)
5831 {
5832 /* None of this table's entries were referenced. Re-use the
5833 parent's table. */
5834 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
5835 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
5836 }
5837 else
5838 {
5839 size_t n;
5840 bfd_boolean *cu, *pu;
5841
5842 /* Or the parent's entries into ours. */
5843 cu = h->vtable_entries_used;
5844 cu[-1] = TRUE;
5845 pu = h->vtable_parent->vtable_entries_used;
5846 if (pu != NULL)
5847 {
5848 asection *sec = h->root.u.def.section;
5849 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
5850 unsigned int log_file_align = bed->s->log_file_align;
5851
5852 n = h->vtable_parent->vtable_entries_size >> log_file_align;
5853 while (n--)
5854 {
5855 if (*pu)
5856 *cu = TRUE;
5857 pu++;
5858 cu++;
5859 }
5860 }
5861 }
5862
5863 return TRUE;
5864 }
5865
5866 static bfd_boolean
5867 elf_gc_smash_unused_vtentry_relocs (h, okp)
5868 struct elf_link_hash_entry *h;
5869 PTR okp;
5870 {
5871 asection *sec;
5872 bfd_vma hstart, hend;
5873 Elf_Internal_Rela *relstart, *relend, *rel;
5874 struct elf_backend_data *bed;
5875 unsigned int log_file_align;
5876
5877 if (h->root.type == bfd_link_hash_warning)
5878 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5879
5880 /* Take care of both those symbols that do not describe vtables as
5881 well as those that are not loaded. */
5882 if (h->vtable_parent == NULL)
5883 return TRUE;
5884
5885 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5886 || h->root.type == bfd_link_hash_defweak);
5887
5888 sec = h->root.u.def.section;
5889 hstart = h->root.u.def.value;
5890 hend = hstart + h->size;
5891
5892 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL,
5893 (Elf_Internal_Rela *) NULL, TRUE);
5894 if (!relstart)
5895 return *(bfd_boolean *) okp = FALSE;
5896 bed = get_elf_backend_data (sec->owner);
5897 log_file_align = bed->s->log_file_align;
5898
5899 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5900
5901 for (rel = relstart; rel < relend; ++rel)
5902 if (rel->r_offset >= hstart && rel->r_offset < hend)
5903 {
5904 /* If the entry is in use, do nothing. */
5905 if (h->vtable_entries_used
5906 && (rel->r_offset - hstart) < h->vtable_entries_size)
5907 {
5908 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
5909 if (h->vtable_entries_used[entry])
5910 continue;
5911 }
5912 /* Otherwise, kill it. */
5913 rel->r_offset = rel->r_info = rel->r_addend = 0;
5914 }
5915
5916 return TRUE;
5917 }
5918
5919 /* Do mark and sweep of unused sections. */
5920
5921 bfd_boolean
5922 elf_gc_sections (abfd, info)
5923 bfd *abfd;
5924 struct bfd_link_info *info;
5925 {
5926 bfd_boolean ok = TRUE;
5927 bfd *sub;
5928 asection * (*gc_mark_hook)
5929 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
5930 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
5931
5932 if (!get_elf_backend_data (abfd)->can_gc_sections
5933 || info->relocatable || info->emitrelocations
5934 || elf_hash_table (info)->dynamic_sections_created)
5935 return TRUE;
5936
5937 /* Apply transitive closure to the vtable entry usage info. */
5938 elf_link_hash_traverse (elf_hash_table (info),
5939 elf_gc_propagate_vtable_entries_used,
5940 (PTR) &ok);
5941 if (!ok)
5942 return FALSE;
5943
5944 /* Kill the vtable relocations that were not used. */
5945 elf_link_hash_traverse (elf_hash_table (info),
5946 elf_gc_smash_unused_vtentry_relocs,
5947 (PTR) &ok);
5948 if (!ok)
5949 return FALSE;
5950
5951 /* Grovel through relocs to find out who stays ... */
5952
5953 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
5954 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5955 {
5956 asection *o;
5957
5958 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
5959 continue;
5960
5961 for (o = sub->sections; o != NULL; o = o->next)
5962 {
5963 if (o->flags & SEC_KEEP)
5964 if (!elf_gc_mark (info, o, gc_mark_hook))
5965 return FALSE;
5966 }
5967 }
5968
5969 /* ... and mark SEC_EXCLUDE for those that go. */
5970 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
5971 return FALSE;
5972
5973 return TRUE;
5974 }
5975 \f
5976 /* Called from check_relocs to record the existance of a VTINHERIT reloc. */
5977
5978 bfd_boolean
5979 elf_gc_record_vtinherit (abfd, sec, h, offset)
5980 bfd *abfd;
5981 asection *sec;
5982 struct elf_link_hash_entry *h;
5983 bfd_vma offset;
5984 {
5985 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
5986 struct elf_link_hash_entry **search, *child;
5987 bfd_size_type extsymcount;
5988
5989 /* The sh_info field of the symtab header tells us where the
5990 external symbols start. We don't care about the local symbols at
5991 this point. */
5992 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
5993 if (!elf_bad_symtab (abfd))
5994 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
5995
5996 sym_hashes = elf_sym_hashes (abfd);
5997 sym_hashes_end = sym_hashes + extsymcount;
5998
5999 /* Hunt down the child symbol, which is in this section at the same
6000 offset as the relocation. */
6001 for (search = sym_hashes; search != sym_hashes_end; ++search)
6002 {
6003 if ((child = *search) != NULL
6004 && (child->root.type == bfd_link_hash_defined
6005 || child->root.type == bfd_link_hash_defweak)
6006 && child->root.u.def.section == sec
6007 && child->root.u.def.value == offset)
6008 goto win;
6009 }
6010
6011 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
6012 bfd_archive_filename (abfd), sec->name,
6013 (unsigned long) offset);
6014 bfd_set_error (bfd_error_invalid_operation);
6015 return FALSE;
6016
6017 win:
6018 if (!h)
6019 {
6020 /* This *should* only be the absolute section. It could potentially
6021 be that someone has defined a non-global vtable though, which
6022 would be bad. It isn't worth paging in the local symbols to be
6023 sure though; that case should simply be handled by the assembler. */
6024
6025 child->vtable_parent = (struct elf_link_hash_entry *) -1;
6026 }
6027 else
6028 child->vtable_parent = h;
6029
6030 return TRUE;
6031 }
6032
6033 /* Called from check_relocs to record the existance of a VTENTRY reloc. */
6034
6035 bfd_boolean
6036 elf_gc_record_vtentry (abfd, sec, h, addend)
6037 bfd *abfd ATTRIBUTE_UNUSED;
6038 asection *sec ATTRIBUTE_UNUSED;
6039 struct elf_link_hash_entry *h;
6040 bfd_vma addend;
6041 {
6042 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6043 unsigned int log_file_align = bed->s->log_file_align;
6044
6045 if (addend >= h->vtable_entries_size)
6046 {
6047 size_t size, bytes, file_align;
6048 bfd_boolean *ptr = h->vtable_entries_used;
6049
6050 /* While the symbol is undefined, we have to be prepared to handle
6051 a zero size. */
6052 file_align = 1 << log_file_align;
6053 if (h->root.type == bfd_link_hash_undefined)
6054 size = addend + file_align;
6055 else
6056 {
6057 size = h->size;
6058 if (addend >= size)
6059 {
6060 /* Oops! We've got a reference past the defined end of
6061 the table. This is probably a bug -- shall we warn? */
6062 size = addend + file_align;
6063 }
6064 }
6065 size = (size + file_align - 1) & -file_align;
6066
6067 /* Allocate one extra entry for use as a "done" flag for the
6068 consolidation pass. */
6069 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
6070
6071 if (ptr)
6072 {
6073 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
6074
6075 if (ptr != NULL)
6076 {
6077 size_t oldbytes;
6078
6079 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
6080 * sizeof (bfd_boolean));
6081 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
6082 }
6083 }
6084 else
6085 ptr = bfd_zmalloc ((bfd_size_type) bytes);
6086
6087 if (ptr == NULL)
6088 return FALSE;
6089
6090 /* And arrange for that done flag to be at index -1. */
6091 h->vtable_entries_used = ptr + 1;
6092 h->vtable_entries_size = size;
6093 }
6094
6095 h->vtable_entries_used[addend >> log_file_align] = TRUE;
6096
6097 return TRUE;
6098 }
6099
6100 /* And an accompanying bit to work out final got entry offsets once
6101 we're done. Should be called from final_link. */
6102
6103 bfd_boolean
6104 elf_gc_common_finalize_got_offsets (abfd, info)
6105 bfd *abfd;
6106 struct bfd_link_info *info;
6107 {
6108 bfd *i;
6109 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6110 bfd_vma gotoff;
6111
6112 /* The GOT offset is relative to the .got section, but the GOT header is
6113 put into the .got.plt section, if the backend uses it. */
6114 if (bed->want_got_plt)
6115 gotoff = 0;
6116 else
6117 gotoff = bed->got_header_size;
6118
6119 /* Do the local .got entries first. */
6120 for (i = info->input_bfds; i; i = i->link_next)
6121 {
6122 bfd_signed_vma *local_got;
6123 bfd_size_type j, locsymcount;
6124 Elf_Internal_Shdr *symtab_hdr;
6125
6126 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
6127 continue;
6128
6129 local_got = elf_local_got_refcounts (i);
6130 if (!local_got)
6131 continue;
6132
6133 symtab_hdr = &elf_tdata (i)->symtab_hdr;
6134 if (elf_bad_symtab (i))
6135 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6136 else
6137 locsymcount = symtab_hdr->sh_info;
6138
6139 for (j = 0; j < locsymcount; ++j)
6140 {
6141 if (local_got[j] > 0)
6142 {
6143 local_got[j] = gotoff;
6144 gotoff += ARCH_SIZE / 8;
6145 }
6146 else
6147 local_got[j] = (bfd_vma) -1;
6148 }
6149 }
6150
6151 /* Then the global .got entries. .plt refcounts are handled by
6152 adjust_dynamic_symbol */
6153 elf_link_hash_traverse (elf_hash_table (info),
6154 elf_gc_allocate_got_offsets,
6155 (PTR) &gotoff);
6156 return TRUE;
6157 }
6158
6159 /* We need a special top-level link routine to convert got reference counts
6160 to real got offsets. */
6161
6162 static bfd_boolean
6163 elf_gc_allocate_got_offsets (h, offarg)
6164 struct elf_link_hash_entry *h;
6165 PTR offarg;
6166 {
6167 bfd_vma *off = (bfd_vma *) offarg;
6168
6169 if (h->root.type == bfd_link_hash_warning)
6170 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6171
6172 if (h->got.refcount > 0)
6173 {
6174 h->got.offset = off[0];
6175 off[0] += ARCH_SIZE / 8;
6176 }
6177 else
6178 h->got.offset = (bfd_vma) -1;
6179
6180 return TRUE;
6181 }
6182
6183 /* Many folk need no more in the way of final link than this, once
6184 got entry reference counting is enabled. */
6185
6186 bfd_boolean
6187 elf_gc_common_final_link (abfd, info)
6188 bfd *abfd;
6189 struct bfd_link_info *info;
6190 {
6191 if (!elf_gc_common_finalize_got_offsets (abfd, info))
6192 return FALSE;
6193
6194 /* Invoke the regular ELF backend linker to do all the work. */
6195 return elf_bfd_final_link (abfd, info);
6196 }
6197
6198 /* This function will be called though elf_link_hash_traverse to store
6199 all hash value of the exported symbols in an array. */
6200
6201 static bfd_boolean
6202 elf_collect_hash_codes (h, data)
6203 struct elf_link_hash_entry *h;
6204 PTR data;
6205 {
6206 unsigned long **valuep = (unsigned long **) data;
6207 const char *name;
6208 char *p;
6209 unsigned long ha;
6210 char *alc = NULL;
6211
6212 if (h->root.type == bfd_link_hash_warning)
6213 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6214
6215 /* Ignore indirect symbols. These are added by the versioning code. */
6216 if (h->dynindx == -1)
6217 return TRUE;
6218
6219 name = h->root.root.string;
6220 p = strchr (name, ELF_VER_CHR);
6221 if (p != NULL)
6222 {
6223 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
6224 memcpy (alc, name, (size_t) (p - name));
6225 alc[p - name] = '\0';
6226 name = alc;
6227 }
6228
6229 /* Compute the hash value. */
6230 ha = bfd_elf_hash (name);
6231
6232 /* Store the found hash value in the array given as the argument. */
6233 *(*valuep)++ = ha;
6234
6235 /* And store it in the struct so that we can put it in the hash table
6236 later. */
6237 h->elf_hash_value = ha;
6238
6239 if (alc != NULL)
6240 free (alc);
6241
6242 return TRUE;
6243 }
6244
6245 bfd_boolean
6246 elf_reloc_symbol_deleted_p (offset, cookie)
6247 bfd_vma offset;
6248 PTR cookie;
6249 {
6250 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
6251
6252 if (rcookie->bad_symtab)
6253 rcookie->rel = rcookie->rels;
6254
6255 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
6256 {
6257 unsigned long r_symndx;
6258
6259 if (! rcookie->bad_symtab)
6260 if (rcookie->rel->r_offset > offset)
6261 return FALSE;
6262 if (rcookie->rel->r_offset != offset)
6263 continue;
6264
6265 r_symndx = ELF_R_SYM (rcookie->rel->r_info);
6266 if (r_symndx == SHN_UNDEF)
6267 return TRUE;
6268
6269 if (r_symndx >= rcookie->locsymcount
6270 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
6271 {
6272 struct elf_link_hash_entry *h;
6273
6274 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
6275
6276 while (h->root.type == bfd_link_hash_indirect
6277 || h->root.type == bfd_link_hash_warning)
6278 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6279
6280 if ((h->root.type == bfd_link_hash_defined
6281 || h->root.type == bfd_link_hash_defweak)
6282 && elf_discarded_section (h->root.u.def.section))
6283 return TRUE;
6284 else
6285 return FALSE;
6286 }
6287 else
6288 {
6289 /* It's not a relocation against a global symbol,
6290 but it could be a relocation against a local
6291 symbol for a discarded section. */
6292 asection *isec;
6293 Elf_Internal_Sym *isym;
6294
6295 /* Need to: get the symbol; get the section. */
6296 isym = &rcookie->locsyms[r_symndx];
6297 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6298 {
6299 isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
6300 if (isec != NULL && elf_discarded_section (isec))
6301 return TRUE;
6302 }
6303 }
6304 return FALSE;
6305 }
6306 return FALSE;
6307 }
6308
6309 /* Discard unneeded references to discarded sections.
6310 Returns TRUE if any section's size was changed. */
6311 /* This function assumes that the relocations are in sorted order,
6312 which is true for all known assemblers. */
6313
6314 bfd_boolean
6315 elf_bfd_discard_info (output_bfd, info)
6316 bfd *output_bfd;
6317 struct bfd_link_info *info;
6318 {
6319 struct elf_reloc_cookie cookie;
6320 asection *stab, *eh;
6321 Elf_Internal_Shdr *symtab_hdr;
6322 struct elf_backend_data *bed;
6323 bfd *abfd;
6324 unsigned int count;
6325 bfd_boolean ret = FALSE;
6326
6327 if (info->traditional_format
6328 || info->hash->creator->flavour != bfd_target_elf_flavour
6329 || ! is_elf_hash_table (info))
6330 return FALSE;
6331
6332 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
6333 {
6334 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6335 continue;
6336
6337 bed = get_elf_backend_data (abfd);
6338
6339 if ((abfd->flags & DYNAMIC) != 0)
6340 continue;
6341
6342 eh = bfd_get_section_by_name (abfd, ".eh_frame");
6343 if (info->relocatable
6344 || (eh != NULL
6345 && (eh->_raw_size == 0
6346 || bfd_is_abs_section (eh->output_section))))
6347 eh = NULL;
6348
6349 stab = bfd_get_section_by_name (abfd, ".stab");
6350 if (stab != NULL
6351 && (stab->_raw_size == 0
6352 || bfd_is_abs_section (stab->output_section)
6353 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
6354 stab = NULL;
6355
6356 if (stab == NULL
6357 && eh == NULL
6358 && bed->elf_backend_discard_info == NULL)
6359 continue;
6360
6361 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6362 cookie.abfd = abfd;
6363 cookie.sym_hashes = elf_sym_hashes (abfd);
6364 cookie.bad_symtab = elf_bad_symtab (abfd);
6365 if (cookie.bad_symtab)
6366 {
6367 cookie.locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6368 cookie.extsymoff = 0;
6369 }
6370 else
6371 {
6372 cookie.locsymcount = symtab_hdr->sh_info;
6373 cookie.extsymoff = symtab_hdr->sh_info;
6374 }
6375
6376 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6377 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
6378 {
6379 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6380 cookie.locsymcount, 0,
6381 NULL, NULL, NULL);
6382 if (cookie.locsyms == NULL)
6383 return FALSE;
6384 }
6385
6386 if (stab != NULL)
6387 {
6388 cookie.rels = NULL;
6389 count = stab->reloc_count;
6390 if (count != 0)
6391 cookie.rels
6392 = _bfd_elf_link_read_relocs (abfd, stab, (PTR) NULL,
6393 (Elf_Internal_Rela *) NULL,
6394 info->keep_memory);
6395 if (cookie.rels != NULL)
6396 {
6397 cookie.rel = cookie.rels;
6398 cookie.relend = cookie.rels;
6399 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6400 if (_bfd_discard_section_stabs (abfd, stab,
6401 elf_section_data (stab)->sec_info,
6402 elf_reloc_symbol_deleted_p,
6403 &cookie))
6404 ret = TRUE;
6405 if (elf_section_data (stab)->relocs != cookie.rels)
6406 free (cookie.rels);
6407 }
6408 }
6409
6410 if (eh != NULL)
6411 {
6412 cookie.rels = NULL;
6413 count = eh->reloc_count;
6414 if (count != 0)
6415 cookie.rels
6416 = _bfd_elf_link_read_relocs (abfd, eh, (PTR) NULL,
6417 (Elf_Internal_Rela *) NULL,
6418 info->keep_memory);
6419 cookie.rel = cookie.rels;
6420 cookie.relend = cookie.rels;
6421 if (cookie.rels != NULL)
6422 cookie.relend += count * bed->s->int_rels_per_ext_rel;
6423
6424 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
6425 elf_reloc_symbol_deleted_p,
6426 &cookie))
6427 ret = TRUE;
6428
6429 if (cookie.rels != NULL
6430 && elf_section_data (eh)->relocs != cookie.rels)
6431 free (cookie.rels);
6432 }
6433
6434 if (bed->elf_backend_discard_info != NULL
6435 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
6436 ret = TRUE;
6437
6438 if (cookie.locsyms != NULL
6439 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
6440 {
6441 if (! info->keep_memory)
6442 free (cookie.locsyms);
6443 else
6444 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
6445 }
6446 }
6447
6448 if (info->eh_frame_hdr
6449 && !info->relocatable
6450 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
6451 ret = TRUE;
6452
6453 return ret;
6454 }
6455
6456 static bfd_boolean
6457 elf_section_ignore_discarded_relocs (sec)
6458 asection *sec;
6459 {
6460 struct elf_backend_data *bed;
6461
6462 switch (sec->sec_info_type)
6463 {
6464 case ELF_INFO_TYPE_STABS:
6465 case ELF_INFO_TYPE_EH_FRAME:
6466 return TRUE;
6467 default:
6468 break;
6469 }
6470
6471 bed = get_elf_backend_data (sec->owner);
6472 if (bed->elf_backend_ignore_discarded_relocs != NULL
6473 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6474 return TRUE;
6475
6476 return FALSE;
6477 }
This page took 0.169474 seconds and 5 git commands to generate.