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