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