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