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