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