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