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