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