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