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