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