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