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