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