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