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