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