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