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