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