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