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