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