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