From Eric Youngdale <eric@andante.jic.com>:
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
8afe83be 1/* ELF linker support.
56f3b62c 2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
8afe83be
KR
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
943fbd5b 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
452a5efb 19
ede4eed4
KR
20/* ELF linker code. */
21
22static boolean elf_link_add_object_symbols
23 PARAMS ((bfd *, struct bfd_link_info *));
24static boolean elf_link_add_archive_symbols
25 PARAMS ((bfd *, struct bfd_link_info *));
ede4eed4
KR
26static boolean elf_export_symbol
27 PARAMS ((struct elf_link_hash_entry *, PTR));
28static boolean elf_adjust_dynamic_symbol
29 PARAMS ((struct elf_link_hash_entry *, PTR));
d044b40a
ILT
30static boolean elf_link_find_version_dependencies
31 PARAMS ((struct elf_link_hash_entry *, PTR));
32static boolean elf_link_find_version_dependencies
33 PARAMS ((struct elf_link_hash_entry *, PTR));
34static boolean elf_link_assign_sym_version
35 PARAMS ((struct elf_link_hash_entry *, PTR));
36static boolean elf_link_renumber_dynsyms
37 PARAMS ((struct elf_link_hash_entry *, PTR));
ede4eed4
KR
38
39/* This struct is used to pass information to routines called via
40 elf_link_hash_traverse which must return failure. */
41
42struct elf_info_failed
43{
44 boolean failed;
45 struct bfd_link_info *info;
ff12f303 46};
ede4eed4
KR
47
48/* Given an ELF BFD, add symbols to the global hash table as
49 appropriate. */
50
51boolean
52elf_bfd_link_add_symbols (abfd, info)
53 bfd *abfd;
54 struct bfd_link_info *info;
55{
ede4eed4
KR
56 switch (bfd_get_format (abfd))
57 {
58 case bfd_object:
59 return elf_link_add_object_symbols (abfd, info);
60 case bfd_archive:
ede4eed4
KR
61 return elf_link_add_archive_symbols (abfd, info);
62 default:
63 bfd_set_error (bfd_error_wrong_format);
64 return false;
65 }
66}
3b3753b8 67\f
ede4eed4
KR
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
94static boolean
95elf_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;
58142f10
ILT
120 defined = (boolean *) bfd_malloc (c * sizeof (boolean));
121 included = (boolean *) bfd_malloc (c * sizeof (boolean));
ede4eed4 122 if (defined == (boolean *) NULL || included == (boolean *) NULL)
58142f10 123 goto error_return;
ede4eed4
KR
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);
d044b40a
ILT
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)
ede4eed4 186 continue;
d044b40a 187
ede4eed4
KR
188 if (h->root.type != bfd_link_hash_undefined)
189 {
68807a39
ILT
190 if (h->root.type != bfd_link_hash_undefweak)
191 defined[i] = true;
ede4eed4
KR
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
266static boolean
267elf_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;
d044b40a
ILT
285 bfd_byte *dynver = NULL;
286 Elf_External_Versym *extversym = NULL;
287 Elf_External_Versym *ever;
ede4eed4
KR
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
d044b40a
ILT
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
0cb70568
ILT
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
54626f1a
ILT
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. */
d044b40a 341 if (dynamic && abfd->xvec == info->hash->creator)
54626f1a
ILT
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
0cb70568
ILT
361 sz = bfd_section_size (abfd, s);
362 msg = (char *) bfd_alloc (abfd, sz);
363 if (msg == NULL)
a9713b91 364 goto error_return;
0cb70568
ILT
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
54626f1a
ILT
370 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
371 false, collect, (struct bfd_link_hash_entry **) NULL)))
0cb70568
ILT
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
d044b40a
ILT
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)
ede4eed4 395 {
d044b40a
ILT
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, hdr->sh_size, abfd)
420 != hdr->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 }
ede4eed4
KR
465 }
466
ede4eed4
KR
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
58142f10
ILT
483 buf = ((Elf_External_Sym *)
484 bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
ede4eed4 485 if (buf == NULL && extsymcount != 0)
58142f10 486 goto error_return;
ede4eed4
KR
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)
a9713b91 494 goto error_return;
ede4eed4
KR
495 elf_sym_hashes (abfd) = sym_hash;
496
d044b40a 497 if (! dynamic)
ede4eed4 498 {
ede4eed4
KR
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
ede4eed4
KR
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
60a49e7f
ILT
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. */
ede4eed4
KR
527 add_needed = true;
528 name = bfd_get_filename (abfd);
60a49e7f 529 if (elf_dt_name (abfd) != NULL)
ede4eed4 530 {
60a49e7f 531 name = elf_dt_name (abfd);
ede4eed4
KR
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
58142f10 543 dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
ede4eed4 544 if (dynbuf == NULL)
58142f10 545 goto error_return;
ede4eed4
KR
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);
60a49e7f 563 if (dyn.d_tag == DT_SONAME)
ede4eed4
KR
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 {
54406786 572 struct bfd_link_needed_list *n, **pn;
ede4eed4
KR
573 char *fnm, *anm;
574
54406786
ILT
575 n = ((struct bfd_link_needed_list *)
576 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
ede4eed4
KR
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;
010d9f2d 608 abfd->section_count = 0;
ede4eed4
KR
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);
d044b40a
ILT
655 if (extversym != NULL)
656 free (extversym);
ede4eed4
KR
657 return true;
658 }
659 }
660 }
661
662 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
663 goto error_return;
664 }
60a49e7f
ILT
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;
ede4eed4
KR
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
d044b40a 682 ever = extversym != NULL ? extversym + hdr->sh_info : NULL;
ede4eed4 683 esymend = buf + extsymcount;
d044b40a
ILT
684 for (esym = buf;
685 esym < esymend;
686 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
ede4eed4
KR
687 {
688 Elf_Internal_Sym sym;
689 int bind;
690 bfd_vma value;
691 asection *sec;
692 flagword flags;
693 const char *name;
0cb70568 694 struct elf_link_hash_entry *h;
ede4eed4 695 boolean definition;
ee9f09cd 696 boolean size_change_ok, type_change_ok;
452a5efb 697 boolean new_weakdef;
ede4eed4
KR
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
ee9f09cd 783 size_change_ok = false;
5b3b9ff6 784 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
ede4eed4
KR
785 if (info->hash->creator->flavour == bfd_target_elf_flavour)
786 {
d044b40a
ILT
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
ede4eed4
KR
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. */
8881b321
ILT
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));
ede4eed4
KR
843 if (h == NULL)
844 goto error_return;
845 *sym_hash = h;
846
869b7d80
ILT
847 if (h->root.type == bfd_link_hash_new)
848 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
849
0cb70568
ILT
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
ee9f09cd
ILT
854 /* It's OK to change the type if it used to be a weak
855 definition. */
5b3b9ff6
ILT
856 if (h->root.type == bfd_link_hash_defweak
857 || h->root.type == bfd_link_hash_undefweak)
858 type_change_ok = true;
ee9f09cd
ILT
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
5b3b9ff6
ILT
862 be overriding an old definition. */
863 if (type_change_ok
864 || h->root.type == bfd_link_hash_undefined)
865 size_change_ok = true;
8235c112 866
d044b40a
ILT
867 override = false;
868
ede4eed4
KR
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
4596421b
ILT
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. */
ede4eed4
KR
880 if (dynamic && definition)
881 {
882 if (h->root.type == bfd_link_hash_defined
3d7c42c9
ILT
883 || h->root.type == bfd_link_hash_defweak
884 || (h->root.type == bfd_link_hash_common
4596421b
ILT
885 && (bind == STB_WEAK
886 || ELF_ST_TYPE (sym.st_info) == STT_FUNC)))
3d7c42c9 887 {
d044b40a 888 override = true;
3d7c42c9
ILT
889 sec = bfd_und_section_ptr;
890 definition = false;
ee9f09cd 891 size_change_ok = true;
4596421b
ILT
892 if (h->root.type == bfd_link_hash_common)
893 type_change_ok = true;
3d7c42c9 894 }
ede4eed4
KR
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
4596421b
ILT
904 && (definition
905 || (bfd_is_com_section (sec)
906 && (h->root.type == bfd_link_hash_defweak
907 || h->type == STT_FUNC)))
ede4eed4
KR
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
d044b40a 911 && (h->root.u.def.section->owner->flags & DYNAMIC) != 0)
ede4eed4 912 {
d044b40a 913 override = true;
ede4eed4
KR
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;
ee9f09cd 919 size_change_ok = true;
4596421b
ILT
920 if (bfd_is_com_section (sec))
921 type_change_ok = true;
d044b40a
ILT
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;
ede4eed4 928 }
d044b40a
ILT
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];
ede4eed4
KR
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
0cb70568
ILT
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
452a5efb 948 new_weakdef = false;
ede4eed4
KR
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
0cb70568 954 && h->weakdef == NULL)
ede4eed4
KR
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
0cb70568
ILT
968 h->weakdef = weaks;
969 weaks = h;
452a5efb 970 new_weakdef = true;
ede4eed4
KR
971 }
972
973 /* Get the alignment of a common symbol. */
974 if (sym.st_shndx == SHN_COMMON
0cb70568
ILT
975 && h->root.type == bfd_link_hash_common)
976 h->root.u.c.p->alignment_power = bfd_log2 (sym.st_value);
ede4eed4
KR
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. */
3d7c42c9
ILT
985 if (sym.st_size != 0
986 && (definition || h->size == 0))
ede4eed4 987 {
ee9f09cd 988 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
3d7c42c9
ILT
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
ede4eed4
KR
994 h->size = sym.st_size;
995 }
3d7c42c9
ILT
996 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
997 && (definition || h->type == STT_NOTYPE))
ede4eed4 998 {
3d7c42c9 999 if (h->type != STT_NOTYPE
8235c112 1000 && h->type != ELF_ST_TYPE (sym.st_info)
ee9f09cd 1001 && ! type_change_ok)
3d7c42c9
ILT
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
ede4eed4
KR
1007 h->type = ELF_ST_TYPE (sym.st_info);
1008 }
1009
6c02f1a0
ILT
1010 if (sym.st_other != 0
1011 && (definition || h->other == 0))
1012 h->other = sym.st_other;
1013
ede4eed4
KR
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
440f3914 1018 object and a shared object. */
ede4eed4
KR
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;
0db6249c 1038 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
1c4794f5
ILT
1039 | ELF_LINK_HASH_REF_REGULAR)) != 0
1040 || (h->weakdef != NULL
440f3914
ILT
1041 && ! new_weakdef
1042 && h->weakdef->dynindx != -1))
ede4eed4
KR
1043 dynsym = true;
1044 }
1045
1046 h->elf_link_hash_flags |= new_flag;
d044b40a
ILT
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 *hi;
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 hi = NULL;
1071 if (! (_bfd_generic_link_add_one_symbol
1072 (info, abfd, shortname, BSF_INDIRECT,
1073 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1074 collect, (struct bfd_link_hash_entry **) &hi)))
1075 goto error_return;
1076
1077 if (hi->root.type == bfd_link_hash_indirect)
1078 hi->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
1079 }
1080 }
1081
ede4eed4
KR
1082 if (dynsym && h->dynindx == -1)
1083 {
1084 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1085 goto error_return;
452a5efb
ILT
1086 if (h->weakdef != NULL
1087 && ! new_weakdef
1088 && h->weakdef->dynindx == -1)
1089 {
1090 if (! _bfd_elf_link_record_dynamic_symbol (info,
1091 h->weakdef))
1092 goto error_return;
1093 }
ede4eed4
KR
1094 }
1095 }
1096 }
1097
1098 /* Now set the weakdefs field correctly for all the weak defined
1099 symbols we found. The only way to do this is to search all the
1100 symbols. Since we only need the information for non functions in
1101 dynamic objects, that's the only time we actually put anything on
1102 the list WEAKS. We need this information so that if a regular
1103 object refers to a symbol defined weakly in a dynamic object, the
1104 real symbol in the dynamic object is also put in the dynamic
1105 symbols; we also must arrange for both symbols to point to the
1106 same memory location. We could handle the general case of symbol
1107 aliasing, but a general symbol alias can only be generated in
1108 assembler code, handling it correctly would be very time
1109 consuming, and other ELF linkers don't handle general aliasing
1110 either. */
1111 while (weaks != NULL)
1112 {
1113 struct elf_link_hash_entry *hlook;
1114 asection *slook;
1115 bfd_vma vlook;
1116 struct elf_link_hash_entry **hpp;
1117 struct elf_link_hash_entry **hppend;
1118
1119 hlook = weaks;
1120 weaks = hlook->weakdef;
1121 hlook->weakdef = NULL;
1122
1123 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
1124 || hlook->root.type == bfd_link_hash_defweak
1125 || hlook->root.type == bfd_link_hash_common
1126 || hlook->root.type == bfd_link_hash_indirect);
1127 slook = hlook->root.u.def.section;
1128 vlook = hlook->root.u.def.value;
1129
1130 hpp = elf_sym_hashes (abfd);
1131 hppend = hpp + extsymcount;
1132 for (; hpp < hppend; hpp++)
1133 {
1134 struct elf_link_hash_entry *h;
1135
1136 h = *hpp;
1137 if (h != NULL && h != hlook
d2bb6c79 1138 && h->root.type == bfd_link_hash_defined
ede4eed4
KR
1139 && h->root.u.def.section == slook
1140 && h->root.u.def.value == vlook)
1141 {
1142 hlook->weakdef = h;
1143
1144 /* If the weak definition is in the list of dynamic
1145 symbols, make sure the real definition is put there
1146 as well. */
1147 if (hlook->dynindx != -1
1148 && h->dynindx == -1)
1149 {
1150 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1151 goto error_return;
1152 }
1153
440f3914
ILT
1154 /* If the real definition is in the list of dynamic
1155 symbols, make sure the weak definition is put there
1156 as well. If we don't do this, then the dynamic
1157 loader might not merge the entries for the real
1158 definition and the weak definition. */
1159 if (h->dynindx != -1
1160 && hlook->dynindx == -1)
1161 {
1162 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
1163 goto error_return;
1164 }
1165
ede4eed4
KR
1166 break;
1167 }
1168 }
1169 }
1170
1171 if (buf != NULL)
1172 {
1173 free (buf);
1174 buf = NULL;
1175 }
1176
d044b40a
ILT
1177 if (extversym != NULL)
1178 {
1179 free (extversym);
1180 extversym = NULL;
1181 }
1182
ede4eed4
KR
1183 /* If this object is the same format as the output object, and it is
1184 not a shared library, then let the backend look through the
1185 relocs.
1186
1187 This is required to build global offset table entries and to
1188 arrange for dynamic relocs. It is not required for the
1189 particular common case of linking non PIC code, even when linking
1190 against shared libraries, but unfortunately there is no way of
1191 knowing whether an object file has been compiled PIC or not.
1192 Looking through the relocs is not particularly time consuming.
1193 The problem is that we must either (1) keep the relocs in memory,
1194 which causes the linker to require additional runtime memory or
1195 (2) read the relocs twice from the input file, which wastes time.
1196 This would be a good case for using mmap.
1197
1198 I have no idea how to handle linking PIC code into a file of a
1199 different format. It probably can't be done. */
1200 check_relocs = get_elf_backend_data (abfd)->check_relocs;
1201 if (! dynamic
1202 && abfd->xvec == info->hash->creator
1203 && check_relocs != NULL)
1204 {
1205 asection *o;
1206
1207 for (o = abfd->sections; o != NULL; o = o->next)
1208 {
1209 Elf_Internal_Rela *internal_relocs;
1210 boolean ok;
1211
1212 if ((o->flags & SEC_RELOC) == 0
1213 || o->reloc_count == 0)
ede4eed4
KR
1214 continue;
1215
c86158e5
ILT
1216 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
1217 (abfd, o, (PTR) NULL,
1218 (Elf_Internal_Rela *) NULL,
1219 info->keep_memory));
ede4eed4
KR
1220 if (internal_relocs == NULL)
1221 goto error_return;
1222
1223 ok = (*check_relocs) (abfd, info, o, internal_relocs);
1224
1225 if (! info->keep_memory)
1226 free (internal_relocs);
1227
1228 if (! ok)
1229 goto error_return;
1230 }
1231 }
1232
1726b8f0
ILT
1233 /* If this is a non-traditional, non-relocateable link, try to
1234 optimize the handling of the .stab/.stabstr sections. */
1235 if (! dynamic
1236 && ! info->relocateable
1237 && ! info->traditional_format
1238 && info->hash->creator->flavour == bfd_target_elf_flavour
1239 && (info->strip != strip_all && info->strip != strip_debugger))
1240 {
1241 asection *stab, *stabstr;
1242
1243 stab = bfd_get_section_by_name (abfd, ".stab");
1244 if (stab != NULL)
1245 {
1246 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
1247
1248 if (stabstr != NULL)
1249 {
1250 struct bfd_elf_section_data *secdata;
1251
1252 secdata = elf_section_data (stab);
1253 if (! _bfd_link_section_stabs (abfd,
1254 &elf_hash_table (info)->stab_info,
1255 stab, stabstr,
1256 &secdata->stab_info))
1257 goto error_return;
1258 }
1259 }
1260 }
1261
ede4eed4
KR
1262 return true;
1263
1264 error_return:
1265 if (buf != NULL)
1266 free (buf);
1267 if (dynbuf != NULL)
1268 free (dynbuf);
d044b40a
ILT
1269 if (dynver != NULL)
1270 free (dynver);
1271 if (extversym != NULL)
1272 free (extversym);
ede4eed4
KR
1273 return false;
1274}
1275
1276/* Create some sections which will be filled in with dynamic linking
1277 information. ABFD is an input file which requires dynamic sections
1278 to be created. The dynamic sections take up virtual memory space
1279 when the final executable is run, so we need to create them before
1280 addresses are assigned to the output sections. We work out the
1281 actual contents and size of these sections later. */
1282
1283boolean
1284elf_link_create_dynamic_sections (abfd, info)
1285 bfd *abfd;
1286 struct bfd_link_info *info;
1287{
1288 flagword flags;
1289 register asection *s;
1290 struct elf_link_hash_entry *h;
1291 struct elf_backend_data *bed;
1292
1293 if (elf_hash_table (info)->dynamic_sections_created)
1294 return true;
1295
1296 /* Make sure that all dynamic sections use the same input BFD. */
1297 if (elf_hash_table (info)->dynobj == NULL)
1298 elf_hash_table (info)->dynobj = abfd;
1299 else
1300 abfd = elf_hash_table (info)->dynobj;
1301
1302 /* Note that we set the SEC_IN_MEMORY flag for all of these
1303 sections. */
ff12f303
ILT
1304 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1305 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
ede4eed4
KR
1306
1307 /* A dynamically linked executable has a .interp section, but a
1308 shared library does not. */
1309 if (! info->shared)
1310 {
1311 s = bfd_make_section (abfd, ".interp");
1312 if (s == NULL
1313 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1314 return false;
1315 }
1316
d044b40a
ILT
1317 /* Create sections to hold version informations. These are removed
1318 if they are not needed. */
1319 s = bfd_make_section (abfd, ".gnu.version_d");
1320 if (s == NULL
1321 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1322 || ! bfd_set_section_alignment (abfd, s, 2))
1323 return false;
1324
1325 s = bfd_make_section (abfd, ".gnu.version");
1326 if (s == NULL
1327 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1328 || ! bfd_set_section_alignment (abfd, s, 1))
1329 return false;
1330
1331 s = bfd_make_section (abfd, ".gnu.version_r");
1332 if (s == NULL
1333 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1334 || ! bfd_set_section_alignment (abfd, s, 2))
1335 return false;
1336
ede4eed4
KR
1337 s = bfd_make_section (abfd, ".dynsym");
1338 if (s == NULL
1339 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1340 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1341 return false;
1342
1343 s = bfd_make_section (abfd, ".dynstr");
1344 if (s == NULL
1345 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1346 return false;
1347
1348 /* Create a strtab to hold the dynamic symbol names. */
1349 if (elf_hash_table (info)->dynstr == NULL)
1350 {
1351 elf_hash_table (info)->dynstr = elf_stringtab_init ();
1352 if (elf_hash_table (info)->dynstr == NULL)
1353 return false;
1354 }
1355
1356 s = bfd_make_section (abfd, ".dynamic");
1357 if (s == NULL
1358 || ! bfd_set_section_flags (abfd, s, flags)
1359 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1360 return false;
1361
1362 /* The special symbol _DYNAMIC is always set to the start of the
1363 .dynamic section. This call occurs before we have processed the
1364 symbols for any dynamic object, so we don't have to worry about
1365 overriding a dynamic definition. We could set _DYNAMIC in a
1366 linker script, but we only want to define it if we are, in fact,
1367 creating a .dynamic section. We don't want to define it if there
1368 is no .dynamic section, since on some ELF platforms the start up
1369 code examines it to decide how to initialize the process. */
1370 h = NULL;
1371 if (! (_bfd_generic_link_add_one_symbol
1372 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
1373 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
1374 (struct bfd_link_hash_entry **) &h)))
1375 return false;
1376 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1377 h->type = STT_OBJECT;
1378
1379 if (info->shared
1380 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1381 return false;
1382
1383 s = bfd_make_section (abfd, ".hash");
1384 if (s == NULL
1385 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1386 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1387 return false;
1388
1389 /* Let the backend create the rest of the sections. This lets the
1390 backend set the right flags. The backend will normally create
1391 the .got and .plt sections. */
1392 bed = get_elf_backend_data (abfd);
1393 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
1394 return false;
1395
1396 elf_hash_table (info)->dynamic_sections_created = true;
1397
1398 return true;
1399}
1400
1401/* Add an entry to the .dynamic table. */
1402
1403boolean
1404elf_add_dynamic_entry (info, tag, val)
1405 struct bfd_link_info *info;
1406 bfd_vma tag;
1407 bfd_vma val;
1408{
1409 Elf_Internal_Dyn dyn;
1410 bfd *dynobj;
1411 asection *s;
1412 size_t newsize;
1413 bfd_byte *newcontents;
1414
1415 dynobj = elf_hash_table (info)->dynobj;
1416
1417 s = bfd_get_section_by_name (dynobj, ".dynamic");
1418 BFD_ASSERT (s != NULL);
1419
1420 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
58142f10 1421 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
ede4eed4 1422 if (newcontents == NULL)
58142f10 1423 return false;
ede4eed4
KR
1424
1425 dyn.d_tag = tag;
1426 dyn.d_un.d_val = val;
1427 elf_swap_dyn_out (dynobj, &dyn,
1428 (Elf_External_Dyn *) (newcontents + s->_raw_size));
1429
1430 s->_raw_size = newsize;
1431 s->contents = newcontents;
1432
1433 return true;
1434}
3b3753b8 1435\f
ede4eed4
KR
1436
1437/* Read and swap the relocs for a section. They may have been cached.
1438 If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1439 they are used as buffers to read into. They are known to be large
1440 enough. If the INTERNAL_RELOCS relocs argument is NULL, the return
1441 value is allocated using either malloc or bfd_alloc, according to
1442 the KEEP_MEMORY argument. */
1443
c86158e5
ILT
1444Elf_Internal_Rela *
1445NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
1446 keep_memory)
ede4eed4
KR
1447 bfd *abfd;
1448 asection *o;
1449 PTR external_relocs;
1450 Elf_Internal_Rela *internal_relocs;
1451 boolean keep_memory;
1452{
1453 Elf_Internal_Shdr *rel_hdr;
1454 PTR alloc1 = NULL;
1455 Elf_Internal_Rela *alloc2 = NULL;
1456
1457 if (elf_section_data (o)->relocs != NULL)
1458 return elf_section_data (o)->relocs;
1459
1460 if (o->reloc_count == 0)
1461 return NULL;
1462
1463 rel_hdr = &elf_section_data (o)->rel_hdr;
1464
1465 if (internal_relocs == NULL)
1466 {
1467 size_t size;
1468
1469 size = o->reloc_count * sizeof (Elf_Internal_Rela);
1470 if (keep_memory)
1471 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
1472 else
58142f10 1473 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
ede4eed4 1474 if (internal_relocs == NULL)
58142f10 1475 goto error_return;
ede4eed4
KR
1476 }
1477
1478 if (external_relocs == NULL)
1479 {
58142f10 1480 alloc1 = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
ede4eed4 1481 if (alloc1 == NULL)
58142f10 1482 goto error_return;
ede4eed4
KR
1483 external_relocs = alloc1;
1484 }
1485
1486 if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1487 || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd)
1488 != rel_hdr->sh_size))
1489 goto error_return;
1490
1491 /* Swap in the relocs. For convenience, we always produce an
1492 Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1493 to 0. */
1494 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
1495 {
1496 Elf_External_Rel *erel;
1497 Elf_External_Rel *erelend;
1498 Elf_Internal_Rela *irela;
1499
1500 erel = (Elf_External_Rel *) external_relocs;
1501 erelend = erel + o->reloc_count;
1502 irela = internal_relocs;
1503 for (; erel < erelend; erel++, irela++)
1504 {
1505 Elf_Internal_Rel irel;
1506
1507 elf_swap_reloc_in (abfd, erel, &irel);
1508 irela->r_offset = irel.r_offset;
1509 irela->r_info = irel.r_info;
1510 irela->r_addend = 0;
1511 }
1512 }
1513 else
1514 {
1515 Elf_External_Rela *erela;
1516 Elf_External_Rela *erelaend;
1517 Elf_Internal_Rela *irela;
1518
1519 BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
1520
1521 erela = (Elf_External_Rela *) external_relocs;
1522 erelaend = erela + o->reloc_count;
1523 irela = internal_relocs;
1524 for (; erela < erelaend; erela++, irela++)
1525 elf_swap_reloca_in (abfd, erela, irela);
1526 }
1527
1528 /* Cache the results for next time, if we can. */
1529 if (keep_memory)
1530 elf_section_data (o)->relocs = internal_relocs;
ff12f303 1531
ede4eed4
KR
1532 if (alloc1 != NULL)
1533 free (alloc1);
1534
1535 /* Don't free alloc2, since if it was allocated we are passing it
1536 back (under the name of internal_relocs). */
1537
1538 return internal_relocs;
1539
1540 error_return:
1541 if (alloc1 != NULL)
1542 free (alloc1);
1543 if (alloc2 != NULL)
1544 free (alloc2);
1545 return NULL;
1546}
3b3753b8 1547\f
ede4eed4
KR
1548
1549/* Record an assignment to a symbol made by a linker script. We need
1550 this in case some dynamic object refers to this symbol. */
1551
1552/*ARGSUSED*/
1553boolean
1554NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
1555 bfd *output_bfd;
1556 struct bfd_link_info *info;
1557 const char *name;
1558 boolean provide;
1559{
1560 struct elf_link_hash_entry *h;
1561
1562 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1563 return true;
1564
1565 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
1566 if (h == NULL)
1567 return false;
1568
869b7d80
ILT
1569 if (h->root.type == bfd_link_hash_new)
1570 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
1571
ede4eed4
KR
1572 /* If this symbol is being provided by the linker script, and it is
1573 currently defined by a dynamic object, but not by a regular
1574 object, then mark it as undefined so that the generic linker will
1575 force the correct value. */
1576 if (provide
1577 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1578 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1579 h->root.type = bfd_link_hash_undefined;
1580
1581 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1582 h->type = STT_OBJECT;
1583
1584 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1585 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
1586 || info->shared)
1587 && h->dynindx == -1)
1588 {
1589 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1590 return false;
1591
1592 /* If this is a weak defined symbol, and we know a corresponding
1593 real symbol from the same dynamic object, make sure the real
1594 symbol is also made into a dynamic symbol. */
1595 if (h->weakdef != NULL
1596 && h->weakdef->dynindx == -1)
1597 {
1598 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1599 return false;
1600 }
1601 }
1602
1603 return true;
1604}
3b3753b8 1605\f
d044b40a
ILT
1606/* This structure is used to pass information to
1607 elf_link_assign_sym_version. */
1608
1609struct elf_assign_sym_version_info
1610{
1611 /* Output BFD. */
1612 bfd *output_bfd;
1613 /* General link information. */
1614 struct bfd_link_info *info;
1615 /* Version tree. */
1616 struct bfd_elf_version_tree *verdefs;
1617 /* Whether we are exporting all dynamic symbols. */
1618 boolean export_dynamic;
1619 /* Whether we removed any symbols from the dynamic symbol table. */
1620 boolean removed_dynamic;
1621 /* Whether we had a failure. */
1622 boolean failed;
1623};
1624
1625/* This structure is used to pass information to
1626 elf_link_find_version_dependencies. */
1627
1628struct elf_find_verdep_info
1629{
1630 /* Output BFD. */
1631 bfd *output_bfd;
1632 /* General link information. */
1633 struct bfd_link_info *info;
1634 /* The number of dependencies. */
1635 unsigned int vers;
1636 /* Whether we had a failure. */
1637 boolean failed;
1638};
ede4eed4
KR
1639
1640/* Array used to determine the number of hash table buckets to use
1641 based on the number of symbols there are. If there are fewer than
1642 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1643 fewer than 37 we use 17 buckets, and so forth. We never use more
6b8ec6f3 1644 than 32771 buckets. */
ede4eed4
KR
1645
1646static const size_t elf_buckets[] =
1647{
6b8ec6f3
ILT
1648 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
1649 16411, 32771, 0
ede4eed4
KR
1650};
1651
1652/* Set up the sizes and contents of the ELF dynamic sections. This is
1653 called by the ELF linker emulation before_allocation routine. We
1654 must set the sizes of the sections before the linker sets the
1655 addresses of the various sections. */
1656
1657boolean
1658NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
148437ec 1659 export_dynamic, filter_shlib,
d044b40a
ILT
1660 auxiliary_filters, info, sinterpptr,
1661 verdefs)
ede4eed4
KR
1662 bfd *output_bfd;
1663 const char *soname;
1664 const char *rpath;
1665 boolean export_dynamic;
148437ec 1666 const char *filter_shlib;
db109ca2 1667 const char * const *auxiliary_filters;
ede4eed4
KR
1668 struct bfd_link_info *info;
1669 asection **sinterpptr;
d044b40a 1670 struct bfd_elf_version_tree *verdefs;
ede4eed4 1671{
d044b40a 1672 bfd_size_type soname_indx;
ede4eed4
KR
1673 bfd *dynobj;
1674 struct elf_backend_data *bed;
1675
1676 *sinterpptr = NULL;
1677
d044b40a
ILT
1678 soname_indx = -1;
1679
ede4eed4
KR
1680 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1681 return true;
1682
ff12f303
ILT
1683 /* The backend may have to create some sections regardless of whether
1684 we're dynamic or not. */
1685 bed = get_elf_backend_data (output_bfd);
1686 if (bed->elf_backend_always_size_sections
1687 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
1688 return false;
1689
ede4eed4
KR
1690 dynobj = elf_hash_table (info)->dynobj;
1691
1692 /* If there were no dynamic objects in the link, there is nothing to
1693 do here. */
1694 if (dynobj == NULL)
1695 return true;
1696
1697 /* If we are supposed to export all symbols into the dynamic symbol
1698 table (this is not the normal case), then do so. */
1699 if (export_dynamic)
1700 {
1701 struct elf_info_failed eif;
1702
1703 eif.failed = false;
1704 eif.info = info;
1705 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
1706 (PTR) &eif);
1707 if (eif.failed)
1708 return false;
1709 }
1710
1711 if (elf_hash_table (info)->dynamic_sections_created)
1712 {
1713 struct elf_info_failed eif;
73a68447 1714 struct elf_link_hash_entry *h;
ede4eed4
KR
1715 bfd_size_type strsize;
1716
1717 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1718 BFD_ASSERT (*sinterpptr != NULL || info->shared);
1719
1720 if (soname != NULL)
1721 {
d044b40a
ILT
1722 soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
1723 soname, true, true);
1724 if (soname_indx == (bfd_size_type) -1
1725 || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
ede4eed4 1726 return false;
ff12f303 1727 }
ede4eed4 1728
951fe66d
ILT
1729 if (info->symbolic)
1730 {
1731 if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1732 return false;
1733 }
1734
ede4eed4
KR
1735 if (rpath != NULL)
1736 {
1737 bfd_size_type indx;
1738
1739 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
1740 true, true);
1741 if (indx == (bfd_size_type) -1
1742 || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
1743 return false;
1744 }
1745
148437ec
ILT
1746 if (filter_shlib != NULL)
1747 {
1748 bfd_size_type indx;
1749
1750 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
1751 filter_shlib, true, true);
1752 if (indx == (bfd_size_type) -1
1753 || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
1754 return false;
1755 }
1756
db109ca2 1757 if (auxiliary_filters != NULL)
148437ec 1758 {
db109ca2 1759 const char * const *p;
148437ec 1760
db109ca2
ILT
1761 for (p = auxiliary_filters; *p != NULL; p++)
1762 {
1763 bfd_size_type indx;
1764
1765 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
1766 *p, true, true);
1767 if (indx == (bfd_size_type) -1
1768 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
1769 return false;
1770 }
148437ec
ILT
1771 }
1772
ede4eed4
KR
1773 /* Find all symbols which were defined in a dynamic object and make
1774 the backend pick a reasonable value for them. */
1775 eif.failed = false;
1776 eif.info = info;
1777 elf_link_hash_traverse (elf_hash_table (info),
1778 elf_adjust_dynamic_symbol,
1779 (PTR) &eif);
1780 if (eif.failed)
1781 return false;
1782
1783 /* Add some entries to the .dynamic section. We fill in some of the
1784 values later, in elf_bfd_final_link, but we must add the entries
1785 now so that we know the final size of the .dynamic section. */
73a68447
ILT
1786 h = elf_link_hash_lookup (elf_hash_table (info), "_init", false,
1787 false, false);
1788 if (h != NULL
1789 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1790 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
ede4eed4
KR
1791 {
1792 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
1793 return false;
1794 }
73a68447
ILT
1795 h = elf_link_hash_lookup (elf_hash_table (info), "_fini", false,
1796 false, false);
1797 if (h != NULL
1798 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1799 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
ede4eed4
KR
1800 {
1801 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
1802 return false;
1803 }
1804 strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1805 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1806 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1807 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1808 || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1809 || ! elf_add_dynamic_entry (info, DT_SYMENT,
1810 sizeof (Elf_External_Sym)))
1811 return false;
1812 }
1813
1814 /* The backend must work out the sizes of all the other dynamic
1815 sections. */
ede4eed4
KR
1816 if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1817 return false;
1818
1819 if (elf_hash_table (info)->dynamic_sections_created)
1820 {
1821 size_t dynsymcount;
1822 asection *s;
1823 size_t i;
1824 size_t bucketcount = 0;
1825 Elf_Internal_Sym isym;
1826
d044b40a
ILT
1827 /* Set up the version definition section. */
1828 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
1829 BFD_ASSERT (s != NULL);
1830 if (verdefs == NULL)
1831 {
1832 struct elf_assign_sym_version_info sinfo;
1833 asection **spp;
1834
1835 /* No version script was used. In this case, we just check
1836 that there were no version overrides for any symbols. */
1837 sinfo.output_bfd = output_bfd;
1838 sinfo.info = info;
1839 sinfo.verdefs = verdefs;
1840 sinfo.removed_dynamic = false;
1841 sinfo.export_dynamic = export_dynamic;
1842 sinfo.failed = false;
1843
1844 elf_link_hash_traverse (elf_hash_table (info),
1845 elf_link_assign_sym_version,
1846 (PTR) &sinfo);
1847 if (sinfo.failed)
1848 return false;
1849
1850 /* Don't include this section in the output file. */
1851 for (spp = &output_bfd->sections;
1852 *spp != s->output_section;
1853 spp = &(*spp)->next)
1854 ;
1855 *spp = s->output_section->next;
1856 --output_bfd->section_count;
1857 }
1858 else
1859 {
1860 struct elf_assign_sym_version_info sinfo;
1861 unsigned int cdefs;
1862 bfd_size_type size;
1863 struct bfd_elf_version_tree *t;
1864 bfd_byte *p;
1865 Elf_Internal_Verdef def;
1866 Elf_Internal_Verdaux defaux;
1867
1868 /* Attach all of the symbols to their version information.
1869 This may cause some symbols to be unexported. */
1870 sinfo.output_bfd = output_bfd;
1871 sinfo.info = info;
1872 sinfo.verdefs = verdefs;
1873 sinfo.export_dynamic = export_dynamic;
1874 sinfo.removed_dynamic = false;
1875 sinfo.failed = false;
1876 elf_link_hash_traverse (elf_hash_table (info),
1877 elf_link_assign_sym_version,
1878 (PTR) &sinfo);
1879 if (sinfo.failed)
1880 return false;
1881
1882 if (sinfo.removed_dynamic)
1883 {
1884 /* Some dynamic symbols were changed to be local
1885 symbols. In this case, we renumber all of the
1886 dynamic symbols, so that we don't have a hole.
1887 FIXME: The names of the removed symbols will still be
1888 in the dynamic string table, wasting space. */
1889 elf_hash_table (info)->dynsymcount = 1;
1890 elf_link_hash_traverse (elf_hash_table (info),
1891 elf_link_renumber_dynsyms,
1892 (PTR) info);
1893 }
1894
1895 cdefs = 0;
1896 size = 0;
1897
1898 /* Make space for the base version. */
1899 size += sizeof (Elf_External_Verdef);
1900 size += sizeof (Elf_External_Verdaux);
1901 ++cdefs;
1902
1903 for (t = verdefs; t != NULL; t = t->next)
1904 {
1905 struct bfd_elf_version_deps *n;
1906
1907 size += sizeof (Elf_External_Verdef);
1908 size += sizeof (Elf_External_Verdaux);
1909 ++cdefs;
1910
1911 for (n = t->deps; n != NULL; n = n->next)
1912 size += sizeof (Elf_External_Verdaux);
1913 }
1914
1915 s->_raw_size = size;
1916 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1917 if (s->contents == NULL && s->_raw_size != 0)
1918 return false;
1919
1920 /* Fill in the version definition section. */
1921
1922 p = s->contents;
1923
1924 def.vd_version = VER_DEF_CURRENT;
1925 def.vd_flags = VER_FLG_BASE;
1926 def.vd_ndx = 1;
1927 def.vd_cnt = 1;
1928 def.vd_aux = sizeof (Elf_External_Verdef);
1929 def.vd_next = (sizeof (Elf_External_Verdef)
1930 + sizeof (Elf_External_Verdaux));
1931
1932 if (soname_indx != -1)
1933 {
1934 def.vd_hash = bfd_elf_hash ((const unsigned char *) soname);
1935 defaux.vda_name = soname_indx;
1936 }
1937 else
1938 {
1939 const char *name;
1940 bfd_size_type indx;
1941
1942 name = output_bfd->filename;
1943 def.vd_hash = bfd_elf_hash ((const unsigned char *) name);
1944 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
1945 name, true, false);
1946 if (indx == (bfd_size_type) -1)
1947 return false;
1948 defaux.vda_name = indx;
1949 }
1950 defaux.vda_next = 0;
1951
1952 _bfd_elf_swap_verdef_out (output_bfd, &def,
1953 (Elf_External_Verdef *)p);
1954 p += sizeof (Elf_External_Verdef);
1955 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
1956 (Elf_External_Verdaux *) p);
1957 p += sizeof (Elf_External_Verdaux);
1958
1959 for (t = verdefs; t != NULL; t = t->next)
1960 {
1961 unsigned int cdeps;
1962 struct bfd_elf_version_deps *n;
1963 struct elf_link_hash_entry *h;
1964
1965 cdeps = 0;
1966 for (n = t->deps; n != NULL; n = n->next)
1967 ++cdeps;
1968
1969 /* Add a symbol representing this version. */
1970 h = NULL;
1971 if (! (_bfd_generic_link_add_one_symbol
1972 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
1973 (bfd_vma) 0, (const char *) NULL, false,
1974 get_elf_backend_data (dynobj)->collect,
1975 (struct bfd_link_hash_entry **) &h)))
1976 return false;
1977 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
1978 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1979 h->type = STT_OBJECT;
1980 h->verinfo.vertree = t;
1981
1982 if (info->shared)
1983 {
1984 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1985 return false;
1986 }
1987
1988 def.vd_version = VER_DEF_CURRENT;
1989 def.vd_flags = 0;
1990 if (t->globals == NULL && t->locals == NULL && ! t->used)
1991 def.vd_flags |= VER_FLG_WEAK;
1992 def.vd_ndx = t->vernum + 1;
1993 def.vd_cnt = cdeps + 1;
1994 def.vd_hash = bfd_elf_hash ((const unsigned char *) t->name);
1995 def.vd_aux = sizeof (Elf_External_Verdef);
1996 if (t->next != NULL)
1997 def.vd_next = (sizeof (Elf_External_Verdef)
1998 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
1999 else
2000 def.vd_next = 0;
2001
2002 _bfd_elf_swap_verdef_out (output_bfd, &def,
2003 (Elf_External_Verdef *) p);
2004 p += sizeof (Elf_External_Verdef);
2005
2006 defaux.vda_name = h->dynstr_index;
2007 if (t->deps == NULL)
2008 defaux.vda_next = 0;
2009 else
2010 defaux.vda_next = sizeof (Elf_External_Verdaux);
2011 t->name_indx = defaux.vda_name;
2012
2013 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2014 (Elf_External_Verdaux *) p);
2015 p += sizeof (Elf_External_Verdaux);
2016
2017 for (n = t->deps; n != NULL; n = n->next)
2018 {
2019 defaux.vda_name = n->version_needed->name_indx;
2020 if (n->next == NULL)
2021 defaux.vda_next = 0;
2022 else
2023 defaux.vda_next = sizeof (Elf_External_Verdaux);
2024
2025 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
2026 (Elf_External_Verdaux *) p);
2027 p += sizeof (Elf_External_Verdaux);
2028 }
2029 }
2030
2031 if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
2032 || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
2033 return false;
2034
2035 elf_tdata (output_bfd)->cverdefs = cdefs;
2036 }
2037
2038 /* Work out the size of the version reference section. */
2039
2040 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2041 BFD_ASSERT (s != NULL);
2042 {
2043 struct elf_find_verdep_info sinfo;
2044
2045 sinfo.output_bfd = output_bfd;
2046 sinfo.info = info;
2047 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
2048 if (sinfo.vers == 0)
2049 sinfo.vers = 1;
2050 sinfo.failed = false;
2051
2052 elf_link_hash_traverse (elf_hash_table (info),
2053 elf_link_find_version_dependencies,
2054 (PTR) &sinfo);
2055
2056 if (elf_tdata (output_bfd)->verref == NULL)
2057 {
2058 asection **spp;
2059
2060 /* We don't have any version definitions, so we can just
2061 remove the section. */
2062
2063 for (spp = &output_bfd->sections;
2064 *spp != s->output_section;
2065 spp = &(*spp)->next)
2066 ;
2067 *spp = s->output_section->next;
2068 --output_bfd->section_count;
2069 }
2070 else
2071 {
2072 Elf_Internal_Verneed *t;
2073 unsigned int size;
2074 unsigned int crefs;
2075 bfd_byte *p;
2076
2077 /* Build the version definition section. */
2078 for (t = elf_tdata (output_bfd)->verref;
2079 t != NULL;
2080 t = t->vn_nextref)
2081 {
2082 Elf_Internal_Vernaux *a;
2083
2084 size += sizeof (Elf_External_Verneed);
2085 ++crefs;
2086 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2087 size += sizeof (Elf_External_Vernaux);
2088 }
2089
2090 s->_raw_size = size;
2091 s->contents = (bfd_byte *) bfd_alloc (output_bfd, size);
2092 if (s->contents == NULL)
2093 return false;
2094
2095 p = s->contents;
2096 for (t = elf_tdata (output_bfd)->verref;
2097 t != NULL;
2098 t = t->vn_nextref)
2099 {
2100 unsigned int caux;
2101 Elf_Internal_Vernaux *a;
2102 bfd_size_type indx;
2103
2104 caux = 0;
2105 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2106 ++caux;
2107
2108 t->vn_version = VER_NEED_CURRENT;
2109 t->vn_cnt = caux;
2110 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2111 t->vn_bfd->filename, true, false);
2112 if (indx == (bfd_size_type) -1)
2113 return false;
2114 t->vn_file = indx;
2115 t->vn_aux = sizeof (Elf_External_Verneed);
2116 if (t->vn_nextref == NULL)
2117 t->vn_next = 0;
2118 else
2119 t->vn_next = (sizeof (Elf_External_Verneed)
2120 + caux * sizeof (Elf_External_Vernaux));
2121
2122 _bfd_elf_swap_verneed_out (output_bfd, t,
2123 (Elf_External_Verneed *) p);
2124 p += sizeof (Elf_External_Verneed);
2125
2126 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2127 {
2128 a->vna_hash = bfd_elf_hash ((const unsigned char *)
2129 a->vna_nodename);
2130 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
2131 a->vna_nodename, true, false);
2132 if (indx == (bfd_size_type) -1)
2133 return false;
2134 a->vna_name = indx;
2135 if (a->vna_nextptr == NULL)
2136 a->vna_next = 0;
2137 else
2138 a->vna_next = sizeof (Elf_External_Vernaux);
2139
2140 _bfd_elf_swap_vernaux_out (output_bfd, a,
2141 (Elf_External_Vernaux *) p);
2142 p += sizeof (Elf_External_Vernaux);
2143 }
2144 }
2145
2146 if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
2147 || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
2148 return false;
2149
2150 elf_tdata (output_bfd)->cverrefs = crefs;
2151 }
2152 }
2153
2154 dynsymcount = elf_hash_table (info)->dynsymcount;
2155
2156 /* Work out the size of the symbol version section. */
2157 s = bfd_get_section_by_name (dynobj, ".gnu.version");
2158 BFD_ASSERT (s != NULL);
2159 if (dynsymcount == 0
2160 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
2161 {
2162 asection **spp;
2163
2164 /* We don't need any symbol versions; just discard the
2165 section. */
2166 for (spp = &output_bfd->sections;
2167 *spp != s->output_section;
2168 spp = &(*spp)->next)
2169 ;
2170 *spp = s->output_section->next;
2171 --output_bfd->section_count;
2172 }
2173 else
2174 {
2175 Elf_Internal_Versym intversym;
2176
2177 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
2178 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2179 if (s->contents == NULL)
2180 return false;
2181
2182 intversym.vs_vers = 0;
2183 _bfd_elf_swap_versym_out (output_bfd, &intversym,
2184 (Elf_External_Versym *) s->contents);
2185
2186 if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
2187 return false;
2188 }
2189
ede4eed4
KR
2190 /* Set the size of the .dynsym and .hash sections. We counted
2191 the number of dynamic symbols in elf_link_add_object_symbols.
2192 We will build the contents of .dynsym and .hash when we build
2193 the final symbol table, because until then we do not know the
2194 correct value to give the symbols. We built the .dynstr
2195 section as we went along in elf_link_add_object_symbols. */
ede4eed4
KR
2196 s = bfd_get_section_by_name (dynobj, ".dynsym");
2197 BFD_ASSERT (s != NULL);
2198 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
2199 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2200 if (s->contents == NULL && s->_raw_size != 0)
a9713b91 2201 return false;
ede4eed4
KR
2202
2203 /* The first entry in .dynsym is a dummy symbol. */
2204 isym.st_value = 0;
2205 isym.st_size = 0;
2206 isym.st_name = 0;
2207 isym.st_info = 0;
2208 isym.st_other = 0;
2209 isym.st_shndx = 0;
2210 elf_swap_symbol_out (output_bfd, &isym,
cf9fb9f2 2211 (PTR) (Elf_External_Sym *) s->contents);
ede4eed4
KR
2212
2213 for (i = 0; elf_buckets[i] != 0; i++)
2214 {
2215 bucketcount = elf_buckets[i];
2216 if (dynsymcount < elf_buckets[i + 1])
2217 break;
2218 }
2219
2220 s = bfd_get_section_by_name (dynobj, ".hash");
2221 BFD_ASSERT (s != NULL);
2222 s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
2223 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
2224 if (s->contents == NULL)
a9713b91 2225 return false;
3fe22b98 2226 memset (s->contents, 0, (size_t) s->_raw_size);
ede4eed4
KR
2227
2228 put_word (output_bfd, bucketcount, s->contents);
2229 put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
2230
2231 elf_hash_table (info)->bucketcount = bucketcount;
2232
2233 s = bfd_get_section_by_name (dynobj, ".dynstr");
2234 BFD_ASSERT (s != NULL);
2235 s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
2236
2237 if (! elf_add_dynamic_entry (info, DT_NULL, 0))
2238 return false;
2239 }
2240
2241 return true;
2242}
3b3753b8 2243\f
ede4eed4
KR
2244/* Make the backend pick a good value for a dynamic symbol. This is
2245 called via elf_link_hash_traverse, and also calls itself
2246 recursively. */
2247
2248static boolean
2249elf_adjust_dynamic_symbol (h, data)
2250 struct elf_link_hash_entry *h;
2251 PTR data;
2252{
2253 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2254 bfd *dynobj;
2255 struct elf_backend_data *bed;
2256
d044b40a
ILT
2257 /* Ignore indirect symbols. There are added by the versioning code. */
2258 if (h->root.type == bfd_link_hash_indirect)
2259 return true;
2260
869b7d80
ILT
2261 /* If this symbol was mentioned in a non-ELF file, try to set
2262 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2263 permit a non-ELF file to correctly refer to a symbol defined in
2264 an ELF dynamic object. */
2265 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2266 {
2267 if (h->root.type != bfd_link_hash_defined
2268 && h->root.type != bfd_link_hash_defweak)
2269 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2270 else
2271 {
e303e2e3
ILT
2272 if (h->root.u.def.section->owner != NULL
2273 && (bfd_get_flavour (h->root.u.def.section->owner)
2274 == bfd_target_elf_flavour))
869b7d80
ILT
2275 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2276 else
2277 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2278 }
2279
2280 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2281 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2282 {
2283 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2284 {
2285 eif->failed = true;
2286 return false;
2287 }
2288 }
2289 }
2290
ce6a7731
ILT
2291 /* If this is a final link, and the symbol was defined as a common
2292 symbol in a regular object file, and there was no definition in
2293 any dynamic object, then the linker will have allocated space for
2294 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2295 flag will not have been set. */
2296 if (h->root.type == bfd_link_hash_defined
2297 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2298 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2299 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2300 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2301 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2302
951fe66d
ILT
2303 /* If -Bsymbolic was used (which means to bind references to global
2304 symbols to the definition within the shared object), and this
2305 symbol was defined in a regular object, then it actually doesn't
2306 need a PLT entry. */
2307 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2308 && eif->info->shared
2309 && eif->info->symbolic
2310 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2311 h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
2312
ede4eed4
KR
2313 /* If this symbol does not require a PLT entry, and it is not
2314 defined by a dynamic object, or is not referenced by a regular
452a5efb
ILT
2315 object, ignore it. We do have to handle a weak defined symbol,
2316 even if no regular object refers to it, if we decided to add it
2317 to the dynamic symbol table. FIXME: Do we normally need to worry
2318 about symbols which are defined by one dynamic object and
2319 referenced by another one? */
ede4eed4
KR
2320 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2321 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2322 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
452a5efb
ILT
2323 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2324 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
ede4eed4
KR
2325 return true;
2326
2327 /* If we've already adjusted this symbol, don't do it again. This
2328 can happen via a recursive call. */
2329 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2330 return true;
2331
2332 /* Don't look at this symbol again. Note that we must set this
2333 after checking the above conditions, because we may look at a
2334 symbol once, decide not to do anything, and then get called
2335 recursively later after REF_REGULAR is set below. */
2336 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2337
2338 /* If this is a weak definition, and we know a real definition, and
2339 the real symbol is not itself defined by a regular object file,
2340 then get a good value for the real definition. We handle the
2341 real symbol first, for the convenience of the backend routine.
2342
2343 Note that there is a confusing case here. If the real definition
2344 is defined by a regular object file, we don't get the real symbol
2345 from the dynamic object, but we do get the weak symbol. If the
2346 processor backend uses a COPY reloc, then if some routine in the
2347 dynamic object changes the real symbol, we will not see that
2348 change in the corresponding weak symbol. This is the way other
2349 ELF linkers work as well, and seems to be a result of the shared
2350 library model.
2351
2352 I will clarify this issue. Most SVR4 shared libraries define the
2353 variable _timezone and define timezone as a weak synonym. The
2354 tzset call changes _timezone. If you write
2355 extern int timezone;
2356 int _timezone = 5;
2357 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2358 you might expect that, since timezone is a synonym for _timezone,
2359 the same number will print both times. However, if the processor
2360 backend uses a COPY reloc, then actually timezone will be copied
2361 into your process image, and, since you define _timezone
2362 yourself, _timezone will not. Thus timezone and _timezone will
2363 wind up at different memory locations. The tzset call will set
2364 _timezone, leaving timezone unchanged. */
2365
2366 if (h->weakdef != NULL)
2367 {
2368 struct elf_link_hash_entry *weakdef;
2369
2370 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2371 || h->root.type == bfd_link_hash_defweak);
2372 weakdef = h->weakdef;
2373 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2374 || weakdef->root.type == bfd_link_hash_defweak);
2375 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2376 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2377 {
2378 /* This symbol is defined by a regular object file, so we
2379 will not do anything special. Clear weakdef for the
2380 convenience of the processor backend. */
2381 h->weakdef = NULL;
2382 }
2383 else
2384 {
2385 /* There is an implicit reference by a regular object file
2386 via the weak symbol. */
2387 weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2388 if (! elf_adjust_dynamic_symbol (weakdef, (PTR) eif))
2389 return false;
2390 }
2391 }
2392
2393 dynobj = elf_hash_table (eif->info)->dynobj;
2394 bed = get_elf_backend_data (dynobj);
2395 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2396 {
2397 eif->failed = true;
2398 return false;
2399 }
2400
2401 return true;
2402}
2403\f
d044b40a
ILT
2404/* This routine is used to export all defined symbols into the dynamic
2405 symbol table. It is called via elf_link_hash_traverse. */
2406
2407static boolean
2408elf_export_symbol (h, data)
2409 struct elf_link_hash_entry *h;
2410 PTR data;
2411{
2412 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2413
2414 if (h->dynindx == -1
2415 && (h->elf_link_hash_flags
2416 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 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 return true;
2426}
2427\f
2428/* Look through the symbols which are defined in other shared
2429 libraries and referenced here. Update the list of version
2430 dependencies. This will be put into the .gnu.version_r section.
2431 This function is called via elf_link_hash_traverse. */
2432
2433static boolean
2434elf_link_find_version_dependencies (h, data)
2435 struct elf_link_hash_entry *h;
2436 PTR data;
2437{
2438 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2439 Elf_Internal_Verneed *t;
2440 Elf_Internal_Vernaux *a;
2441
2442 /* We only care about symbols defined in shared objects with version
2443 information. */
2444 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2445 || h->dynindx == -1
2446 || h->verinfo.verdef == NULL)
2447 return true;
2448
2449 /* See if we already know about this version. */
2450 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
2451 {
2452 if (t->vn_bfd == h->verinfo.verdef->vd_bfd)
2453 continue;
2454
2455 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2456 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2457 return true;
2458
2459 break;
2460 }
2461
2462 /* This is a new version. Add it to tree we are building. */
2463
2464 if (t == NULL)
2465 {
2466 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t);
2467 if (t == NULL)
2468 {
2469 rinfo->failed = true;
2470 return false;
2471 }
2472
2473 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2474 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
2475 elf_tdata (rinfo->output_bfd)->verref = t;
2476 }
2477
2478 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a);
2479
2480 /* Note that we are copying a string pointer here, and testing it
2481 above. If bfd_elf_string_from_elf_section is ever changed to
2482 discard the string data when low in memory, this will have to be
2483 fixed. */
2484 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2485
2486 a->vna_flags = h->verinfo.verdef->vd_flags;
2487 a->vna_nextptr = t->vn_auxptr;
2488
2489 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2490 ++rinfo->vers;
2491
2492 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2493
2494 t->vn_auxptr = a;
2495
2496 return true;
2497}
2498
2499/* Figure out appropriate versions for all the symbols. We may not
2500 have the version number script until we have read all of the input
2501 files, so until that point we don't know which symbols should be
2502 local. This function is called via elf_link_hash_traverse. */
2503
2504static boolean
2505elf_link_assign_sym_version (h, data)
2506 struct elf_link_hash_entry *h;
2507 PTR data;
2508{
2509 struct elf_assign_sym_version_info *sinfo =
2510 (struct elf_assign_sym_version_info *) data;
2511 struct bfd_link_info *info = sinfo->info;
2512 char *p;
2513
2514 /* We only need version numbers for symbols defined in regular
2515 objects. */
2516 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2517 return true;
2518
2519 p = strchr (h->root.root.string, ELF_VER_CHR);
2520 if (p != NULL && h->verinfo.vertree == NULL)
2521 {
2522 struct bfd_elf_version_tree *t;
2523 boolean hidden;
2524
2525 hidden = true;
2526
2527 /* There are two consecutive ELF_VER_CHR characters if this is
2528 not a hidden symbol. */
2529 ++p;
2530 if (*p == ELF_VER_CHR)
2531 {
2532 hidden = false;
2533 ++p;
2534 }
2535
2536 /* If there is no version string, we can just return out. */
2537 if (*p == '\0')
2538 {
2539 if (hidden)
2540 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
2541 return true;
2542 }
2543
2544 /* Look for the version. If we find it, it is no longer weak. */
2545 for (t = sinfo->verdefs; t != NULL; t = t->next)
2546 {
2547 if (strcmp (t->name, p) == 0)
2548 {
2549 h->verinfo.vertree = t;
2550 t->used = true;
2551 break;
2552 }
2553 }
2554
2555 if (t == NULL)
2556 {
2557 /* We could not find the version. Return an error.
2558 FIXME: Why? */
2559 (*_bfd_error_handler)
2560 ("%s: invalid version %s", bfd_get_filename (sinfo->output_bfd),
2561 h->root.root.string);
2562 bfd_set_error (bfd_error_bad_value);
2563 sinfo->failed = true;
2564 return false;
2565 }
2566
2567 if (hidden)
2568 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
2569 }
2570
2571 /* If we don't have a version for this symbol, see if we can find
2572 something. */
2573 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2574 {
2575 struct bfd_elf_version_tree *t;
2576 struct bfd_elf_version_tree *deflt;
2577 struct bfd_elf_version_expr *d;
2578
2579 /* See if can find what version this symbol is in. If the
2580 symbol is supposed to eb local, then don't actually register
2581 it. */
2582 deflt = NULL;
2583 for (t = sinfo->verdefs; t != NULL; t = t->next)
2584 {
2585 if (t->globals != NULL)
2586 {
2587 for (d = t->globals; d != NULL; d = d->next)
2588 {
2589 if (fnmatch (d->match, h->root.root.string, 0) == 0)
2590 {
2591 h->verinfo.vertree = t;
2592 break;
2593 }
2594 }
2595
2596 if (d != NULL)
2597 break;
2598 }
2599
2600 if (t->locals != NULL)
2601 {
2602 for (d = t->locals; d != NULL; d = d->next)
2603 {
2604 if (d->match[0] == '*' && d->match[1] == '\0')
2605 deflt = t;
2606 else if (fnmatch (d->match, h->root.root.string, 0) == 0)
2607 {
2608 h->verinfo.vertree = t;
2609 if (h->dynindx != -1
2610 && info->shared
2611 && ! sinfo->export_dynamic
2612 && (h->elf_link_hash_flags
2613 & ELF_LINK_HASH_NEEDS_PLT) == 0)
2614 {
2615 sinfo->removed_dynamic = true;
2616 h->dynindx = -1;
2617 /* FIXME: The name of the symbol has already
2618 been recorded in the dynamic string table
2619 section. */
2620 }
2621 break;
2622 }
2623 }
2624
2625 if (d != NULL)
2626 break;
2627 }
2628 }
2629
2630 if (deflt != NULL && h->verinfo.vertree == NULL)
2631 {
2632 h->verinfo.vertree = deflt;
2633 if (h->dynindx != -1
2634 && info->shared
2635 && ! sinfo->export_dynamic
2636 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2637 {
2638 sinfo->removed_dynamic = true;
2639 h->dynindx = -1;
2640 /* FIXME: The name of the symbol has already been
2641 recorded in the dynamic string table section. */
2642 }
2643 }
2644 }
2645
2646 return true;
2647}
2648
2649/* This function is used to renumber the dynamic symbols, if some of
2650 them are removed because they are marked as local. This is called
2651 via elf_link_hash_traverse. */
2652
2653static boolean
2654elf_link_renumber_dynsyms (h, data)
2655 struct elf_link_hash_entry *h;
2656 PTR data;
2657{
2658 struct bfd_link_info *info = (struct bfd_link_info *) data;
2659
2660 if (h->dynindx != -1)
2661 {
2662 h->dynindx = elf_hash_table (info)->dynsymcount;
2663 ++elf_hash_table (info)->dynsymcount;
2664 }
2665
2666 return true;
2667}
2668\f
ede4eed4
KR
2669/* Final phase of ELF linker. */
2670
2671/* A structure we use to avoid passing large numbers of arguments. */
2672
2673struct elf_final_link_info
2674{
2675 /* General link information. */
2676 struct bfd_link_info *info;
2677 /* Output BFD. */
2678 bfd *output_bfd;
2679 /* Symbol string table. */
2680 struct bfd_strtab_hash *symstrtab;
2681 /* .dynsym section. */
2682 asection *dynsym_sec;
2683 /* .hash section. */
2684 asection *hash_sec;
d044b40a
ILT
2685 /* symbol version section (.gnu.version). */
2686 asection *symver_sec;
ede4eed4
KR
2687 /* Buffer large enough to hold contents of any section. */
2688 bfd_byte *contents;
2689 /* Buffer large enough to hold external relocs of any section. */
2690 PTR external_relocs;
2691 /* Buffer large enough to hold internal relocs of any section. */
2692 Elf_Internal_Rela *internal_relocs;
2693 /* Buffer large enough to hold external local symbols of any input
2694 BFD. */
2695 Elf_External_Sym *external_syms;
2696 /* Buffer large enough to hold internal local symbols of any input
2697 BFD. */
2698 Elf_Internal_Sym *internal_syms;
2699 /* Array large enough to hold a symbol index for each local symbol
2700 of any input BFD. */
2701 long *indices;
2702 /* Array large enough to hold a section pointer for each local
2703 symbol of any input BFD. */
2704 asection **sections;
2705 /* Buffer to hold swapped out symbols. */
2706 Elf_External_Sym *symbuf;
2707 /* Number of swapped out symbols in buffer. */
2708 size_t symbuf_count;
2709 /* Number of symbols which fit in symbuf. */
2710 size_t symbuf_size;
2711};
2712
2713static boolean elf_link_output_sym
2714 PARAMS ((struct elf_final_link_info *, const char *,
2715 Elf_Internal_Sym *, asection *));
2716static boolean elf_link_flush_output_syms
2717 PARAMS ((struct elf_final_link_info *));
2718static boolean elf_link_output_extsym
2719 PARAMS ((struct elf_link_hash_entry *, PTR));
2720static boolean elf_link_input_bfd
2721 PARAMS ((struct elf_final_link_info *, bfd *));
2722static boolean elf_reloc_link_order
2723 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2724 struct bfd_link_order *));
2725
2726/* This struct is used to pass information to routines called via
2727 elf_link_hash_traverse which must return failure. */
2728
2729struct elf_finfo_failed
2730{
2731 boolean failed;
2732 struct elf_final_link_info *finfo;
ff12f303 2733};
ede4eed4
KR
2734
2735/* Do the final step of an ELF link. */
2736
2737boolean
2738elf_bfd_final_link (abfd, info)
2739 bfd *abfd;
2740 struct bfd_link_info *info;
2741{
2742 boolean dynamic;
2743 bfd *dynobj;
2744 struct elf_final_link_info finfo;
2745 register asection *o;
2746 register struct bfd_link_order *p;
2747 register bfd *sub;
2748 size_t max_contents_size;
2749 size_t max_external_reloc_size;
2750 size_t max_internal_reloc_count;
2751 size_t max_sym_count;
2752 file_ptr off;
2753 Elf_Internal_Sym elfsym;
2754 unsigned int i;
2755 Elf_Internal_Shdr *symtab_hdr;
2756 Elf_Internal_Shdr *symstrtab_hdr;
2757 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2758 struct elf_finfo_failed eif;
2759
2760 if (info->shared)
2761 abfd->flags |= DYNAMIC;
2762
2763 dynamic = elf_hash_table (info)->dynamic_sections_created;
2764 dynobj = elf_hash_table (info)->dynobj;
2765
2766 finfo.info = info;
2767 finfo.output_bfd = abfd;
2768 finfo.symstrtab = elf_stringtab_init ();
2769 if (finfo.symstrtab == NULL)
2770 return false;
d044b40a 2771
ede4eed4
KR
2772 if (! dynamic)
2773 {
2774 finfo.dynsym_sec = NULL;
2775 finfo.hash_sec = NULL;
d044b40a 2776 finfo.symver_sec = NULL;
ede4eed4
KR
2777 }
2778 else
2779 {
2780 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
2781 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
2782 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
d044b40a
ILT
2783 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
2784 /* Note that it is OK if symver_sec is NULL. */
ede4eed4 2785 }
d044b40a 2786
ede4eed4
KR
2787 finfo.contents = NULL;
2788 finfo.external_relocs = NULL;
2789 finfo.internal_relocs = NULL;
2790 finfo.external_syms = NULL;
2791 finfo.internal_syms = NULL;
2792 finfo.indices = NULL;
2793 finfo.sections = NULL;
2794 finfo.symbuf = NULL;
2795 finfo.symbuf_count = 0;
2796
2797 /* Count up the number of relocations we will output for each output
2798 section, so that we know the sizes of the reloc sections. We
2799 also figure out some maximum sizes. */
2800 max_contents_size = 0;
2801 max_external_reloc_size = 0;
2802 max_internal_reloc_count = 0;
2803 max_sym_count = 0;
2804 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2805 {
2806 o->reloc_count = 0;
2807
2808 for (p = o->link_order_head; p != NULL; p = p->next)
2809 {
2810 if (p->type == bfd_section_reloc_link_order
2811 || p->type == bfd_symbol_reloc_link_order)
2812 ++o->reloc_count;
2813 else if (p->type == bfd_indirect_link_order)
2814 {
2815 asection *sec;
2816
2817 sec = p->u.indirect.section;
2818
7ec49f91
ILT
2819 /* Mark all sections which are to be included in the
2820 link. This will normally be every section. We need
2821 to do this so that we can identify any sections which
2822 the linker has decided to not include. */
ff0e4a93 2823 sec->linker_mark = true;
7ec49f91 2824
ede4eed4
KR
2825 if (info->relocateable)
2826 o->reloc_count += sec->reloc_count;
2827
2828 if (sec->_raw_size > max_contents_size)
2829 max_contents_size = sec->_raw_size;
2830 if (sec->_cooked_size > max_contents_size)
2831 max_contents_size = sec->_cooked_size;
2832
2833 /* We are interested in just local symbols, not all
2834 symbols. */
d044b40a
ILT
2835 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
2836 && (sec->owner->flags & DYNAMIC) == 0)
ede4eed4
KR
2837 {
2838 size_t sym_count;
2839
2840 if (elf_bad_symtab (sec->owner))
2841 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
2842 / sizeof (Elf_External_Sym));
2843 else
2844 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
2845
2846 if (sym_count > max_sym_count)
2847 max_sym_count = sym_count;
2848
2849 if ((sec->flags & SEC_RELOC) != 0)
2850 {
2851 size_t ext_size;
2852
2853 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
2854 if (ext_size > max_external_reloc_size)
2855 max_external_reloc_size = ext_size;
2856 if (sec->reloc_count > max_internal_reloc_count)
2857 max_internal_reloc_count = sec->reloc_count;
2858 }
2859 }
2860 }
2861 }
2862
2863 if (o->reloc_count > 0)
2864 o->flags |= SEC_RELOC;
2865 else
2866 {
2867 /* Explicitly clear the SEC_RELOC flag. The linker tends to
2868 set it (this is probably a bug) and if it is set
2869 assign_section_numbers will create a reloc section. */
2870 o->flags &=~ SEC_RELOC;
2871 }
2872
2873 /* If the SEC_ALLOC flag is not set, force the section VMA to
2874 zero. This is done in elf_fake_sections as well, but forcing
2875 the VMA to 0 here will ensure that relocs against these
2876 sections are handled correctly. */
2e0567eb
ILT
2877 if ((o->flags & SEC_ALLOC) == 0
2878 && ! o->user_set_vma)
ede4eed4
KR
2879 o->vma = 0;
2880 }
2881
2882 /* Figure out the file positions for everything but the symbol table
2883 and the relocs. We set symcount to force assign_section_numbers
2884 to create a symbol table. */
2885 abfd->symcount = info->strip == strip_all ? 0 : 1;
2886 BFD_ASSERT (! abfd->output_has_begun);
2887 if (! _bfd_elf_compute_section_file_positions (abfd, info))
2888 goto error_return;
2889
2890 /* That created the reloc sections. Set their sizes, and assign
2891 them file positions, and allocate some buffers. */
2892 for (o = abfd->sections; o != NULL; o = o->next)
2893 {
2894 if ((o->flags & SEC_RELOC) != 0)
2895 {
2896 Elf_Internal_Shdr *rel_hdr;
2897 register struct elf_link_hash_entry **p, **pend;
2898
2899 rel_hdr = &elf_section_data (o)->rel_hdr;
2900
2901 rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
2902
2903 /* The contents field must last into write_object_contents,
2904 so we allocate it with bfd_alloc rather than malloc. */
2905 rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
2906 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
a9713b91 2907 goto error_return;
ede4eed4
KR
2908
2909 p = ((struct elf_link_hash_entry **)
58142f10
ILT
2910 bfd_malloc (o->reloc_count
2911 * sizeof (struct elf_link_hash_entry *)));
ede4eed4 2912 if (p == NULL && o->reloc_count != 0)
58142f10 2913 goto error_return;
ede4eed4
KR
2914 elf_section_data (o)->rel_hashes = p;
2915 pend = p + o->reloc_count;
2916 for (; p < pend; p++)
2917 *p = NULL;
2918
2919 /* Use the reloc_count field as an index when outputting the
2920 relocs. */
2921 o->reloc_count = 0;
2922 }
2923 }
2924
2925 _bfd_elf_assign_file_positions_for_relocs (abfd);
2926
2927 /* We have now assigned file positions for all the sections except
2928 .symtab and .strtab. We start the .symtab section at the current
2929 file position, and write directly to it. We build the .strtab
ab276dfa 2930 section in memory. */
ede4eed4
KR
2931 abfd->symcount = 0;
2932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2933 /* sh_name is set in prep_headers. */
2934 symtab_hdr->sh_type = SHT_SYMTAB;
2935 symtab_hdr->sh_flags = 0;
2936 symtab_hdr->sh_addr = 0;
2937 symtab_hdr->sh_size = 0;
2938 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2939 /* sh_link is set in assign_section_numbers. */
2940 /* sh_info is set below. */
2941 /* sh_offset is set just below. */
2942 symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */
2943
2944 off = elf_tdata (abfd)->next_file_pos;
2945 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
2946
2947 /* Note that at this point elf_tdata (abfd)->next_file_pos is
2948 incorrect. We do not yet know the size of the .symtab section.
2949 We correct next_file_pos below, after we do know the size. */
2950
2951 /* Allocate a buffer to hold swapped out symbols. This is to avoid
2952 continuously seeking to the right position in the file. */
2953 if (! info->keep_memory || max_sym_count < 20)
2954 finfo.symbuf_size = 20;
2955 else
2956 finfo.symbuf_size = max_sym_count;
2957 finfo.symbuf = ((Elf_External_Sym *)
58142f10 2958 bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
ede4eed4 2959 if (finfo.symbuf == NULL)
58142f10 2960 goto error_return;
ede4eed4
KR
2961
2962 /* Start writing out the symbol table. The first symbol is always a
2963 dummy symbol. */
28c16b55
ILT
2964 if (info->strip != strip_all || info->relocateable)
2965 {
2966 elfsym.st_value = 0;
2967 elfsym.st_size = 0;
2968 elfsym.st_info = 0;
2969 elfsym.st_other = 0;
2970 elfsym.st_shndx = SHN_UNDEF;
2971 if (! elf_link_output_sym (&finfo, (const char *) NULL,
2972 &elfsym, bfd_und_section_ptr))
2973 goto error_return;
2974 }
ede4eed4
KR
2975
2976#if 0
2977 /* Some standard ELF linkers do this, but we don't because it causes
2978 bootstrap comparison failures. */
2979 /* Output a file symbol for the output file as the second symbol.
2980 We output this even if we are discarding local symbols, although
2981 I'm not sure if this is correct. */
2982 elfsym.st_value = 0;
2983 elfsym.st_size = 0;
2984 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2985 elfsym.st_other = 0;
2986 elfsym.st_shndx = SHN_ABS;
2987 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
2988 &elfsym, bfd_abs_section_ptr))
2989 goto error_return;
2990#endif
2991
2992 /* Output a symbol for each section. We output these even if we are
2993 discarding local symbols, since they are used for relocs. These
2994 symbols have no names. We store the index of each one in the
2995 index field of the section, so that we can find it again when
2996 outputting relocs. */
28c16b55 2997 if (info->strip != strip_all || info->relocateable)
ede4eed4 2998 {
28c16b55
ILT
2999 elfsym.st_value = 0;
3000 elfsym.st_size = 0;
3001 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3002 elfsym.st_other = 0;
3003 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
3004 {
3005 o = section_from_elf_index (abfd, i);
3006 if (o != NULL)
3007 o->target_index = abfd->symcount;
3008 elfsym.st_shndx = i;
3009 if (! elf_link_output_sym (&finfo, (const char *) NULL,
3010 &elfsym, o))
3011 goto error_return;
3012 }
ede4eed4
KR
3013 }
3014
3015 /* Allocate some memory to hold information read in from the input
3016 files. */
58142f10
ILT
3017 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3018 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
ede4eed4 3019 finfo.internal_relocs = ((Elf_Internal_Rela *)
58142f10
ILT
3020 bfd_malloc (max_internal_reloc_count
3021 * sizeof (Elf_Internal_Rela)));
ede4eed4 3022 finfo.external_syms = ((Elf_External_Sym *)
58142f10
ILT
3023 bfd_malloc (max_sym_count
3024 * sizeof (Elf_External_Sym)));
ede4eed4 3025 finfo.internal_syms = ((Elf_Internal_Sym *)
58142f10
ILT
3026 bfd_malloc (max_sym_count
3027 * sizeof (Elf_Internal_Sym)));
3028 finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
3029 finfo.sections = ((asection **)
3030 bfd_malloc (max_sym_count * sizeof (asection *)));
ede4eed4
KR
3031 if ((finfo.contents == NULL && max_contents_size != 0)
3032 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
3033 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
3034 || (finfo.external_syms == NULL && max_sym_count != 0)
3035 || (finfo.internal_syms == NULL && max_sym_count != 0)
3036 || (finfo.indices == NULL && max_sym_count != 0)
3037 || (finfo.sections == NULL && max_sym_count != 0))
58142f10 3038 goto error_return;
ede4eed4
KR
3039
3040 /* Since ELF permits relocations to be against local symbols, we
3041 must have the local symbols available when we do the relocations.
3042 Since we would rather only read the local symbols once, and we
3043 would rather not keep them in memory, we handle all the
3044 relocations for a single input file at the same time.
3045
3046 Unfortunately, there is no way to know the total number of local
3047 symbols until we have seen all of them, and the local symbol
3048 indices precede the global symbol indices. This means that when
3049 we are generating relocateable output, and we see a reloc against
3050 a global symbol, we can not know the symbol index until we have
3051 finished examining all the local symbols to see which ones we are
3052 going to output. To deal with this, we keep the relocations in
3053 memory, and don't output them until the end of the link. This is
3054 an unfortunate waste of memory, but I don't see a good way around
3055 it. Fortunately, it only happens when performing a relocateable
3056 link, which is not the common case. FIXME: If keep_memory is set
3057 we could write the relocs out and then read them again; I don't
3058 know how bad the memory loss will be. */
3059
3060 for (sub = info->input_bfds; sub != NULL; sub = sub->next)
3061 sub->output_has_begun = false;
3062 for (o = abfd->sections; o != NULL; o = o->next)
3063 {
3064 for (p = o->link_order_head; p != NULL; p = p->next)
3065 {
3066 if (p->type == bfd_indirect_link_order
3067 && (bfd_get_flavour (p->u.indirect.section->owner)
3068 == bfd_target_elf_flavour))
3069 {
3070 sub = p->u.indirect.section->owner;
3071 if (! sub->output_has_begun)
3072 {
3073 if (! elf_link_input_bfd (&finfo, sub))
3074 goto error_return;
3075 sub->output_has_begun = true;
3076 }
3077 }
3078 else if (p->type == bfd_section_reloc_link_order
3079 || p->type == bfd_symbol_reloc_link_order)
3080 {
3081 if (! elf_reloc_link_order (abfd, info, o, p))
3082 goto error_return;
3083 }
3084 else
3085 {
3086 if (! _bfd_default_link_order (abfd, info, o, p))
3087 goto error_return;
3088 }
3089 }
3090 }
3091
3092 /* That wrote out all the local symbols. Finish up the symbol table
3093 with the global symbols. */
3094
3095 /* The sh_info field records the index of the first non local
3096 symbol. */
3097 symtab_hdr->sh_info = abfd->symcount;
3098 if (dynamic)
3099 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
3100
3101 /* We get the global symbols from the hash table. */
3102 eif.failed = false;
3103 eif.finfo = &finfo;
3104 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
3105 (PTR) &eif);
3106 if (eif.failed)
3107 return false;
3108
3109 /* Flush all symbols to the file. */
3110 if (! elf_link_flush_output_syms (&finfo))
3111 return false;
3112
3113 /* Now we know the size of the symtab section. */
3114 off += symtab_hdr->sh_size;
3115
3116 /* Finish up and write out the symbol string table (.strtab)
3117 section. */
3118 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3119 /* sh_name was set in prep_headers. */
3120 symstrtab_hdr->sh_type = SHT_STRTAB;
3121 symstrtab_hdr->sh_flags = 0;
3122 symstrtab_hdr->sh_addr = 0;
3123 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
3124 symstrtab_hdr->sh_entsize = 0;
3125 symstrtab_hdr->sh_link = 0;
3126 symstrtab_hdr->sh_info = 0;
3127 /* sh_offset is set just below. */
3128 symstrtab_hdr->sh_addralign = 1;
3129
3130 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
3131 elf_tdata (abfd)->next_file_pos = off;
3132
28c16b55
ILT
3133 if (abfd->symcount > 0)
3134 {
3135 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
3136 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
3137 return false;
3138 }
ede4eed4
KR
3139
3140 /* Adjust the relocs to have the correct symbol indices. */
3141 for (o = abfd->sections; o != NULL; o = o->next)
3142 {
3143 struct elf_link_hash_entry **rel_hash;
3144 Elf_Internal_Shdr *rel_hdr;
3145
3146 if ((o->flags & SEC_RELOC) == 0)
3147 continue;
3148
3149 rel_hash = elf_section_data (o)->rel_hashes;
3150 rel_hdr = &elf_section_data (o)->rel_hdr;
3151 for (i = 0; i < o->reloc_count; i++, rel_hash++)
3152 {
3153 if (*rel_hash == NULL)
3154 continue;
ff12f303 3155
ede4eed4
KR
3156 BFD_ASSERT ((*rel_hash)->indx >= 0);
3157
3158 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
3159 {
3160 Elf_External_Rel *erel;
3161 Elf_Internal_Rel irel;
3162
3163 erel = (Elf_External_Rel *) rel_hdr->contents + i;
3164 elf_swap_reloc_in (abfd, erel, &irel);
3165 irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
3166 ELF_R_TYPE (irel.r_info));
3167 elf_swap_reloc_out (abfd, &irel, erel);
3168 }
3169 else
3170 {
3171 Elf_External_Rela *erela;
3172 Elf_Internal_Rela irela;
3173
3174 BFD_ASSERT (rel_hdr->sh_entsize
3175 == sizeof (Elf_External_Rela));
3176
3177 erela = (Elf_External_Rela *) rel_hdr->contents + i;
3178 elf_swap_reloca_in (abfd, erela, &irela);
3179 irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
3180 ELF_R_TYPE (irela.r_info));
3181 elf_swap_reloca_out (abfd, &irela, erela);
3182 }
3183 }
3184
3185 /* Set the reloc_count field to 0 to prevent write_relocs from
3186 trying to swap the relocs out itself. */
3187 o->reloc_count = 0;
3188 }
3189
3190 /* If we are linking against a dynamic object, or generating a
3191 shared library, finish up the dynamic linking information. */
3192 if (dynamic)
3193 {
3194 Elf_External_Dyn *dyncon, *dynconend;
3195
3196 /* Fix up .dynamic entries. */
3197 o = bfd_get_section_by_name (dynobj, ".dynamic");
3198 BFD_ASSERT (o != NULL);
3199
3200 dyncon = (Elf_External_Dyn *) o->contents;
3201 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
3202 for (; dyncon < dynconend; dyncon++)
3203 {
3204 Elf_Internal_Dyn dyn;
3205 const char *name;
3206 unsigned int type;
3207
3208 elf_swap_dyn_in (dynobj, dyncon, &dyn);
3209
3210 switch (dyn.d_tag)
3211 {
3212 default:
3213 break;
3214
3215 /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
3216 magic _init and _fini symbols. This is pretty ugly,
3217 but we are compatible. */
3218 case DT_INIT:
3219 name = "_init";
3220 goto get_sym;
3221 case DT_FINI:
3222 name = "_fini";
3223 get_sym:
3224 {
3225 struct elf_link_hash_entry *h;
3226
3227 h = elf_link_hash_lookup (elf_hash_table (info), name,
3228 false, false, true);
d6f672b8
ILT
3229 if (h != NULL
3230 && (h->root.type == bfd_link_hash_defined
3231 || h->root.type == bfd_link_hash_defweak))
ede4eed4
KR
3232 {
3233 dyn.d_un.d_val = h->root.u.def.value;
3234 o = h->root.u.def.section;
3235 if (o->output_section != NULL)
3236 dyn.d_un.d_val += (o->output_section->vma
3237 + o->output_offset);
3238 else
d6f672b8
ILT
3239 {
3240 /* The symbol is imported from another shared
3241 library and does not apply to this one. */
3242 dyn.d_un.d_val = 0;
3243 }
3244
3245 elf_swap_dyn_out (dynobj, &dyn, dyncon);
ede4eed4 3246 }
ede4eed4
KR
3247 }
3248 break;
3249
3250 case DT_HASH:
3251 name = ".hash";
3252 goto get_vma;
3253 case DT_STRTAB:
3254 name = ".dynstr";
3255 goto get_vma;
3256 case DT_SYMTAB:
3257 name = ".dynsym";
d044b40a
ILT
3258 goto get_vma;
3259 case DT_VERDEF:
3260 name = ".gnu.version_d";
3261 goto get_vma;
3262 case DT_VERNEED:
3263 name = ".gnu.version_r";
3264 goto get_vma;
3265 case DT_VERSYM:
3266 name = ".gnu.version";
ede4eed4
KR
3267 get_vma:
3268 o = bfd_get_section_by_name (abfd, name);
3269 BFD_ASSERT (o != NULL);
3270 dyn.d_un.d_ptr = o->vma;
3271 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3272 break;
3273
3274 case DT_REL:
3275 case DT_RELA:
3276 case DT_RELSZ:
3277 case DT_RELASZ:
3278 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
3279 type = SHT_REL;
3280 else
3281 type = SHT_RELA;
3282 dyn.d_un.d_val = 0;
3283 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
3284 {
3285 Elf_Internal_Shdr *hdr;
3286
3287 hdr = elf_elfsections (abfd)[i];
3288 if (hdr->sh_type == type
3289 && (hdr->sh_flags & SHF_ALLOC) != 0)
3290 {
3291 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
3292 dyn.d_un.d_val += hdr->sh_size;
3293 else
3294 {
3295 if (dyn.d_un.d_val == 0
3296 || hdr->sh_addr < dyn.d_un.d_val)
3297 dyn.d_un.d_val = hdr->sh_addr;
3298 }
3299 }
3300 }
3301 elf_swap_dyn_out (dynobj, &dyn, dyncon);
3302 break;
3303 }
3304 }
3305 }
3306
3307 /* If we have created any dynamic sections, then output them. */
3308 if (dynobj != NULL)
3309 {
3310 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
3311 goto error_return;
3312
3313 for (o = dynobj->sections; o != NULL; o = o->next)
3314 {
3315 if ((o->flags & SEC_HAS_CONTENTS) == 0
3316 || o->_raw_size == 0)
3317 continue;
ff12f303 3318 if ((o->flags & SEC_LINKER_CREATED) == 0)
ede4eed4
KR
3319 {
3320 /* At this point, we are only interested in sections
ff12f303 3321 created by elf_link_create_dynamic_sections. */
ede4eed4
KR
3322 continue;
3323 }
3324 if ((elf_section_data (o->output_section)->this_hdr.sh_type
3325 != SHT_STRTAB)
3326 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
3327 {
3328 if (! bfd_set_section_contents (abfd, o->output_section,
3329 o->contents, o->output_offset,
3330 o->_raw_size))
3331 goto error_return;
3332 }
3333 else
3334 {
3335 file_ptr off;
3336
3337 /* The contents of the .dynstr section are actually in a
3338 stringtab. */
3339 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
3340 if (bfd_seek (abfd, off, SEEK_SET) != 0
3341 || ! _bfd_stringtab_emit (abfd,
3342 elf_hash_table (info)->dynstr))
3343 goto error_return;
3344 }
3345 }
3346 }
3347
1726b8f0
ILT
3348 /* If we have optimized stabs strings, output them. */
3349 if (elf_hash_table (info)->stab_info != NULL)
3350 {
3351 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
3352 goto error_return;
3353 }
3354
ede4eed4
KR
3355 if (finfo.symstrtab != NULL)
3356 _bfd_stringtab_free (finfo.symstrtab);
3357 if (finfo.contents != NULL)
3358 free (finfo.contents);
3359 if (finfo.external_relocs != NULL)
3360 free (finfo.external_relocs);
3361 if (finfo.internal_relocs != NULL)
3362 free (finfo.internal_relocs);
3363 if (finfo.external_syms != NULL)
3364 free (finfo.external_syms);
3365 if (finfo.internal_syms != NULL)
3366 free (finfo.internal_syms);
3367 if (finfo.indices != NULL)
3368 free (finfo.indices);
3369 if (finfo.sections != NULL)
3370 free (finfo.sections);
3371 if (finfo.symbuf != NULL)
3372 free (finfo.symbuf);
3373 for (o = abfd->sections; o != NULL; o = o->next)
3374 {
3375 if ((o->flags & SEC_RELOC) != 0
3376 && elf_section_data (o)->rel_hashes != NULL)
3377 free (elf_section_data (o)->rel_hashes);
3378 }
3379
3380 elf_tdata (abfd)->linker = true;
3381
3382 return true;
3383
3384 error_return:
3385 if (finfo.symstrtab != NULL)
3386 _bfd_stringtab_free (finfo.symstrtab);
3387 if (finfo.contents != NULL)
3388 free (finfo.contents);
3389 if (finfo.external_relocs != NULL)
3390 free (finfo.external_relocs);
3391 if (finfo.internal_relocs != NULL)
3392 free (finfo.internal_relocs);
3393 if (finfo.external_syms != NULL)
3394 free (finfo.external_syms);
3395 if (finfo.internal_syms != NULL)
3396 free (finfo.internal_syms);
3397 if (finfo.indices != NULL)
3398 free (finfo.indices);
3399 if (finfo.sections != NULL)
3400 free (finfo.sections);
3401 if (finfo.symbuf != NULL)
3402 free (finfo.symbuf);
3403 for (o = abfd->sections; o != NULL; o = o->next)
3404 {
3405 if ((o->flags & SEC_RELOC) != 0
3406 && elf_section_data (o)->rel_hashes != NULL)
3407 free (elf_section_data (o)->rel_hashes);
3408 }
3409
3410 return false;
3411}
3412
3413/* Add a symbol to the output symbol table. */
3414
3415static boolean
3416elf_link_output_sym (finfo, name, elfsym, input_sec)
3417 struct elf_final_link_info *finfo;
3418 const char *name;
3419 Elf_Internal_Sym *elfsym;
3420 asection *input_sec;
3421{
3422 boolean (*output_symbol_hook) PARAMS ((bfd *,
3423 struct bfd_link_info *info,
3424 const char *,
3425 Elf_Internal_Sym *,
3426 asection *));
3427
3428 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
3429 elf_backend_link_output_symbol_hook;
3430 if (output_symbol_hook != NULL)
3431 {
3432 if (! ((*output_symbol_hook)
3433 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
3434 return false;
3435 }
3436
3437 if (name == (const char *) NULL || *name == '\0')
3438 elfsym->st_name = 0;
3439 else
3440 {
3441 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
3442 name, true,
3443 false);
3444 if (elfsym->st_name == (unsigned long) -1)
3445 return false;
3446 }
3447
3448 if (finfo->symbuf_count >= finfo->symbuf_size)
3449 {
3450 if (! elf_link_flush_output_syms (finfo))
3451 return false;
3452 }
3453
3454 elf_swap_symbol_out (finfo->output_bfd, elfsym,
cf9fb9f2 3455 (PTR) (finfo->symbuf + finfo->symbuf_count));
ede4eed4
KR
3456 ++finfo->symbuf_count;
3457
3458 ++finfo->output_bfd->symcount;
3459
3460 return true;
3461}
3462
3463/* Flush the output symbols to the file. */
3464
3465static boolean
3466elf_link_flush_output_syms (finfo)
3467 struct elf_final_link_info *finfo;
3468{
28c16b55
ILT
3469 if (finfo->symbuf_count > 0)
3470 {
3471 Elf_Internal_Shdr *symtab;
ede4eed4 3472
28c16b55 3473 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
ede4eed4 3474
28c16b55
ILT
3475 if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
3476 SEEK_SET) != 0
3477 || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
3478 sizeof (Elf_External_Sym), finfo->output_bfd)
3479 != finfo->symbuf_count * sizeof (Elf_External_Sym)))
3480 return false;
ede4eed4 3481
28c16b55 3482 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
ede4eed4 3483
28c16b55
ILT
3484 finfo->symbuf_count = 0;
3485 }
ede4eed4
KR
3486
3487 return true;
3488}
3489
3490/* Add an external symbol to the symbol table. This is called from
3491 the hash table traversal routine. */
3492
3493static boolean
3494elf_link_output_extsym (h, data)
3495 struct elf_link_hash_entry *h;
3496 PTR data;
3497{
3498 struct elf_finfo_failed *eif = (struct elf_finfo_failed *) data;
3499 struct elf_final_link_info *finfo = eif->finfo;
3500 boolean strip;
3501 Elf_Internal_Sym sym;
3502 asection *input_sec;
3503
3504 /* If we are not creating a shared library, and this symbol is
3505 referenced by a shared library but is not defined anywhere, then
3506 warn that it is undefined. If we do not do this, the runtime
3507 linker will complain that the symbol is undefined when the
3508 program is run. We don't have to worry about symbols that are
3509 referenced by regular files, because we will already have issued
252239f8 3510 warnings for them. */
ede4eed4
KR
3511 if (! finfo->info->relocateable
3512 && ! finfo->info->shared
3513 && h->root.type == bfd_link_hash_undefined
3514 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
252239f8 3515 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
ede4eed4
KR
3516 {
3517 if (! ((*finfo->info->callbacks->undefined_symbol)
3518 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
3519 (asection *) NULL, 0)))
3520 {
3521 eif->failed = true;
3522 return false;
3523 }
3524 }
3525
3526 /* We don't want to output symbols that have never been mentioned by
3527 a regular file, or that we have been told to strip. However, if
3528 h->indx is set to -2, the symbol is used by a reloc and we must
3529 output it. */
3530 if (h->indx == -2)
3531 strip = false;
3532 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3533 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3534 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3535 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3536 strip = true;
3537 else if (finfo->info->strip == strip_all
3538 || (finfo->info->strip == strip_some
3539 && bfd_hash_lookup (finfo->info->keep_hash,
3540 h->root.root.string,
3541 false, false) == NULL))
3542 strip = true;
3543 else
3544 strip = false;
3545
3546 /* If we're stripping it, and it's not a dynamic symbol, there's
3547 nothing else to do. */
3548 if (strip && h->dynindx == -1)
3549 return true;
3550
3551 sym.st_value = 0;
3552 sym.st_size = h->size;
6c02f1a0 3553 sym.st_other = h->other;
ede4eed4
KR
3554 if (h->root.type == bfd_link_hash_undefweak
3555 || h->root.type == bfd_link_hash_defweak)
3556 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
3557 else
3558 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
3559
3560 switch (h->root.type)
3561 {
3562 default:
3563 case bfd_link_hash_new:
3564 abort ();
3565 return false;
3566
3567 case bfd_link_hash_undefined:
3568 input_sec = bfd_und_section_ptr;
3569 sym.st_shndx = SHN_UNDEF;
3570 break;
3571
3572 case bfd_link_hash_undefweak:
3573 input_sec = bfd_und_section_ptr;
3574 sym.st_shndx = SHN_UNDEF;
3575 break;
3576
3577 case bfd_link_hash_defined:
3578 case bfd_link_hash_defweak:
3579 {
3580 input_sec = h->root.u.def.section;
3581 if (input_sec->output_section != NULL)
3582 {
3583 sym.st_shndx =
3584 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
3585 input_sec->output_section);
3586 if (sym.st_shndx == (unsigned short) -1)
3587 {
3588 eif->failed = true;
3589 return false;
3590 }
3591
3592 /* ELF symbols in relocateable files are section relative,
3593 but in nonrelocateable files they are virtual
3594 addresses. */
3595 sym.st_value = h->root.u.def.value + input_sec->output_offset;
3596 if (! finfo->info->relocateable)
3597 sym.st_value += input_sec->output_section->vma;
3598 }
3599 else
3600 {
d044b40a 3601 BFD_ASSERT ((input_sec->owner->flags & DYNAMIC) != 0);
ede4eed4
KR
3602 sym.st_shndx = SHN_UNDEF;
3603 input_sec = bfd_und_section_ptr;
3604 }
3605 }
3606 break;
3607
3608 case bfd_link_hash_common:
3609 input_sec = bfd_com_section_ptr;
3610 sym.st_shndx = SHN_COMMON;
3611 sym.st_value = 1 << h->root.u.c.p->alignment_power;
3612 break;
3613
3614 case bfd_link_hash_indirect:
d044b40a
ILT
3615 /* These symbols are created by symbol versioning. They point
3616 to the decorated version of the name. For example, if the
3617 symbol foo@@GNU_1.2 is the default, which should be used when
3618 foo is used with no version, then we add an indirect symbol
3619 foo which points to foo@@GNU_1.2. */
3620 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3621 return true;
3622
3623 /* Fall through. */
ede4eed4 3624 case bfd_link_hash_warning:
d044b40a
ILT
3625 /* We can't represent these symbols in ELF, although a warning
3626 symbol may have come from a .gnu.warning.SYMBOL section. We
1f4ae0d6
ILT
3627 just put the target symbol in the hash table. If the target
3628 symbol does not really exist, don't do anything. */
3629 if (h->root.u.i.link->type == bfd_link_hash_new)
3630 return true;
0cb70568
ILT
3631 return (elf_link_output_extsym
3632 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
ede4eed4
KR
3633 }
3634
3635 /* If this symbol should be put in the .dynsym section, then put it
3636 there now. We have already know the symbol index. We also fill
3637 in the entry in the .hash section. */
3638 if (h->dynindx != -1
3639 && elf_hash_table (finfo->info)->dynamic_sections_created)
3640 {
3641 struct elf_backend_data *bed;
d044b40a
ILT
3642 char *p, *copy;
3643 const char *name;
ede4eed4
KR
3644 size_t bucketcount;
3645 size_t bucket;
3646 bfd_byte *bucketpos;
3647 bfd_vma chain;
3648
3649 sym.st_name = h->dynstr_index;
3650
3651 /* Give the processor backend a chance to tweak the symbol
3652 value, and also to finish up anything that needs to be done
3653 for this symbol. */
3654 bed = get_elf_backend_data (finfo->output_bfd);
3655 if (! ((*bed->elf_backend_finish_dynamic_symbol)
3656 (finfo->output_bfd, finfo->info, h, &sym)))
3657 {
3658 eif->failed = true;
3659 return false;
3660 }
3661
3662 elf_swap_symbol_out (finfo->output_bfd, &sym,
cf9fb9f2
ILT
3663 (PTR) (((Elf_External_Sym *)
3664 finfo->dynsym_sec->contents)
3665 + h->dynindx));
ede4eed4 3666
d044b40a
ILT
3667 /* We didn't include the version string in the dynamic string
3668 table, so we must not consider it in the hash table. */
3669 name = h->root.root.string;
3670 p = strchr (name, ELF_VER_CHR);
3671 if (p == NULL)
3672 copy = NULL;
3673 else
3674 {
3675 copy = bfd_alloc (finfo->output_bfd, p - name + 1);
3676 strncpy (copy, name, p - name);
3677 copy[p - name] = '\0';
3678 name = copy;
3679 }
3680
ede4eed4 3681 bucketcount = elf_hash_table (finfo->info)->bucketcount;
d044b40a 3682 bucket = bfd_elf_hash ((const unsigned char *) name) % bucketcount;
ede4eed4
KR
3683 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
3684 + (bucket + 2) * (ARCH_SIZE / 8));
3685 chain = get_word (finfo->output_bfd, bucketpos);
3686 put_word (finfo->output_bfd, h->dynindx, bucketpos);
3687 put_word (finfo->output_bfd, chain,
3688 ((bfd_byte *) finfo->hash_sec->contents
3689 + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
d044b40a
ILT
3690
3691 if (copy != NULL)
3692 bfd_release (finfo->output_bfd, copy);
3693
3694 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
3695 {
3696 Elf_Internal_Versym iversym;
3697
3698 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3699 {
3700 if (h->verinfo.verdef == NULL)
3701 iversym.vs_vers = 0;
3702 else
3703 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
3704 }
3705 else
3706 {
3707 if (h->verinfo.vertree == NULL)
3708 iversym.vs_vers = 1;
3709 else
3710 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3711 }
3712
3713 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
3714 iversym.vs_vers |= VERSYM_HIDDEN;
3715
3716 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym,
3717 (((Elf_External_Versym *)
3718 finfo->symver_sec->contents)
3719 + h->dynindx));
3720 }
ede4eed4
KR
3721 }
3722
3723 /* If we're stripping it, then it was just a dynamic symbol, and
3724 there's nothing else to do. */
3725 if (strip)
3726 return true;
3727
3728 h->indx = finfo->output_bfd->symcount;
3729
3730 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
3731 {
3732 eif->failed = true;
3733 return false;
3734 }
3735
3736 return true;
3737}
3738
3739/* Link an input file into the linker output file. This function
3740 handles all the sections and relocations of the input file at once.
3741 This is so that we only have to read the local symbols once, and
3742 don't have to keep them in memory. */
3743
3744static boolean
3745elf_link_input_bfd (finfo, input_bfd)
3746 struct elf_final_link_info *finfo;
3747 bfd *input_bfd;
3748{
3749 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
3750 bfd *, asection *, bfd_byte *,
3751 Elf_Internal_Rela *,
3752 Elf_Internal_Sym *, asection **));
3753 bfd *output_bfd;
3754 Elf_Internal_Shdr *symtab_hdr;
3755 size_t locsymcount;
3756 size_t extsymoff;
c86158e5 3757 Elf_External_Sym *external_syms;
ede4eed4
KR
3758 Elf_External_Sym *esym;
3759 Elf_External_Sym *esymend;
3760 Elf_Internal_Sym *isym;
3761 long *pindex;
3762 asection **ppsection;
3763 asection *o;
3764
3765 output_bfd = finfo->output_bfd;
3766 relocate_section =
3767 get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
3768
3769 /* If this is a dynamic object, we don't want to do anything here:
3770 we don't want the local symbols, and we don't want the section
3771 contents. */
d044b40a 3772 if ((input_bfd->flags & DYNAMIC) != 0)
ede4eed4
KR
3773 return true;
3774
3775 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3776 if (elf_bad_symtab (input_bfd))
3777 {
3778 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
3779 extsymoff = 0;
3780 }
3781 else
3782 {
3783 locsymcount = symtab_hdr->sh_info;
3784 extsymoff = symtab_hdr->sh_info;
3785 }
3786
3787 /* Read the local symbols. */
c86158e5
ILT
3788 if (symtab_hdr->contents != NULL)
3789 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
3790 else if (locsymcount == 0)
3791 external_syms = NULL;
3792 else
3793 {
3794 external_syms = finfo->external_syms;
3795 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
3796 || (bfd_read (external_syms, sizeof (Elf_External_Sym),
ede4eed4 3797 locsymcount, input_bfd)
c86158e5
ILT
3798 != locsymcount * sizeof (Elf_External_Sym)))
3799 return false;
3800 }
ede4eed4
KR
3801
3802 /* Swap in the local symbols and write out the ones which we know
3803 are going into the output file. */
c86158e5 3804 esym = external_syms;
ede4eed4
KR
3805 esymend = esym + locsymcount;
3806 isym = finfo->internal_syms;
3807 pindex = finfo->indices;
3808 ppsection = finfo->sections;
3809 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
3810 {
3811 asection *isec;
3812 const char *name;
3813 Elf_Internal_Sym osym;
3814
3815 elf_swap_symbol_in (input_bfd, esym, isym);
3816 *pindex = -1;
3817
3818 if (elf_bad_symtab (input_bfd))
3819 {
3820 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
3821 {
3822 *ppsection = NULL;
3823 continue;
3824 }
3825 }
3826
3827 if (isym->st_shndx == SHN_UNDEF)
3828 isec = bfd_und_section_ptr;
3829 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
3830 isec = section_from_elf_index (input_bfd, isym->st_shndx);
3831 else if (isym->st_shndx == SHN_ABS)
3832 isec = bfd_abs_section_ptr;
3833 else if (isym->st_shndx == SHN_COMMON)
3834 isec = bfd_com_section_ptr;
3835 else
3836 {
3837 /* Who knows? */
3838 isec = NULL;
3839 }
3840
3841 *ppsection = isec;
3842
3843 /* Don't output the first, undefined, symbol. */
c86158e5 3844 if (esym == external_syms)
ede4eed4
KR
3845 continue;
3846
3847 /* If we are stripping all symbols, we don't want to output this
3848 one. */
3849 if (finfo->info->strip == strip_all)
3850 continue;
3851
3852 /* We never output section symbols. Instead, we use the section
3853 symbol of the corresponding section in the output file. */
3854 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
3855 continue;
3856
3857 /* If we are discarding all local symbols, we don't want to
3858 output this one. If we are generating a relocateable output
3859 file, then some of the local symbols may be required by
3860 relocs; we output them below as we discover that they are
3861 needed. */
3862 if (finfo->info->discard == discard_all)
3863 continue;
3864
258b1f5d
ILT
3865 /* If this symbol is defined in a section which we are
3866 discarding, we don't need to keep it. For the benefit of the
3867 MIPS ELF linker, we check SEC_EXCLUDE as well as linker_mark. */
3868 if (isym->st_shndx > 0
3869 && isym->st_shndx < SHN_LORESERVE
3870 && isec != NULL
3871 && (! isec->linker_mark
3872 || (! finfo->info->relocateable
3873 && (isec->flags & SEC_EXCLUDE) != 0)))
3874 continue;
3875
ede4eed4
KR
3876 /* Get the name of the symbol. */
3877 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
258b1f5d 3878 isym->st_name);
ede4eed4
KR
3879 if (name == NULL)
3880 return false;
3881
3882 /* See if we are discarding symbols with this name. */
3883 if ((finfo->info->strip == strip_some
3884 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3885 == NULL))
3886 || (finfo->info->discard == discard_l
3887 && strncmp (name, finfo->info->lprefix,
3888 finfo->info->lprefix_len) == 0))
3889 continue;
3890
3891 /* If we get here, we are going to output this symbol. */
3892
3893 osym = *isym;
3894
3895 /* Adjust the section index for the output file. */
3896 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
3897 isec->output_section);
3898 if (osym.st_shndx == (unsigned short) -1)
3899 return false;
3900
3901 *pindex = output_bfd->symcount;
3902
3903 /* ELF symbols in relocateable files are section relative, but
3904 in executable files they are virtual addresses. Note that
3905 this code assumes that all ELF sections have an associated
3906 BFD section with a reasonable value for output_offset; below
3907 we assume that they also have a reasonable value for
3908 output_section. Any special sections must be set up to meet
3909 these requirements. */
3910 osym.st_value += isec->output_offset;
3911 if (! finfo->info->relocateable)
3912 osym.st_value += isec->output_section->vma;
3913
3914 if (! elf_link_output_sym (finfo, name, &osym, isec))
3915 return false;
3916 }
3917
3918 /* Relocate the contents of each section. */
3919 for (o = input_bfd->sections; o != NULL; o = o->next)
3920 {
c86158e5
ILT
3921 bfd_byte *contents;
3922
ff0e4a93 3923 if (! o->linker_mark)
7ec49f91
ILT
3924 {
3925 /* This section was omitted from the link. */
3926 continue;
3927 }
3928
1726b8f0
ILT
3929 if ((o->flags & SEC_HAS_CONTENTS) == 0
3930 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
ede4eed4
KR
3931 continue;
3932
ff12f303 3933 if ((o->flags & SEC_LINKER_CREATED) != 0)
ede4eed4 3934 {
ff12f303
ILT
3935 /* Section was created by elf_link_create_dynamic_sections
3936 or somesuch. */
ede4eed4
KR
3937 continue;
3938 }
3939
c86158e5
ILT
3940 /* Get the contents of the section. They have been cached by a
3941 relaxation routine. Note that o is a section in an input
3942 file, so the contents field will not have been set by any of
3943 the routines which work on output files. */
3944 if (elf_section_data (o)->this_hdr.contents != NULL)
3945 contents = elf_section_data (o)->this_hdr.contents;
3946 else
3947 {
3948 contents = finfo->contents;
3949 if (! bfd_get_section_contents (input_bfd, o, contents,
3950 (file_ptr) 0, o->_raw_size))
3951 return false;
3952 }
ede4eed4
KR
3953
3954 if ((o->flags & SEC_RELOC) != 0)
3955 {
3956 Elf_Internal_Rela *internal_relocs;
3957
3958 /* Get the swapped relocs. */
c86158e5
ILT
3959 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
3960 (input_bfd, o, finfo->external_relocs,
3961 finfo->internal_relocs, false));
ede4eed4
KR
3962 if (internal_relocs == NULL
3963 && o->reloc_count > 0)
3964 return false;
3965
3966 /* Relocate the section by invoking a back end routine.
3967
3968 The back end routine is responsible for adjusting the
3969 section contents as necessary, and (if using Rela relocs
3970 and generating a relocateable output file) adjusting the
3971 reloc addend as necessary.
3972
3973 The back end routine does not have to worry about setting
3974 the reloc address or the reloc symbol index.
3975
3976 The back end routine is given a pointer to the swapped in
3977 internal symbols, and can access the hash table entries
3978 for the external symbols via elf_sym_hashes (input_bfd).
3979
3980 When generating relocateable output, the back end routine
3981 must handle STB_LOCAL/STT_SECTION symbols specially. The
3982 output symbol is going to be a section symbol
3983 corresponding to the output section, which will require
3984 the addend to be adjusted. */
3985
3986 if (! (*relocate_section) (output_bfd, finfo->info,
c86158e5 3987 input_bfd, o, contents,
ede4eed4
KR
3988 internal_relocs,
3989 finfo->internal_syms,
3990 finfo->sections))
3991 return false;
3992
3993 if (finfo->info->relocateable)
3994 {
3995 Elf_Internal_Rela *irela;
3996 Elf_Internal_Rela *irelaend;
3997 struct elf_link_hash_entry **rel_hash;
3998 Elf_Internal_Shdr *input_rel_hdr;
3999 Elf_Internal_Shdr *output_rel_hdr;
4000
4001 /* Adjust the reloc addresses and symbol indices. */
4002
4003 irela = internal_relocs;
4004 irelaend = irela + o->reloc_count;
4005 rel_hash = (elf_section_data (o->output_section)->rel_hashes
4006 + o->output_section->reloc_count);
4007 for (; irela < irelaend; irela++, rel_hash++)
4008 {
ae115e51 4009 unsigned long r_symndx;
ede4eed4
KR
4010 Elf_Internal_Sym *isym;
4011 asection *sec;
4012
4013 irela->r_offset += o->output_offset;
4014
4015 r_symndx = ELF_R_SYM (irela->r_info);
4016
4017 if (r_symndx == 0)
4018 continue;
4019
4020 if (r_symndx >= locsymcount
4021 || (elf_bad_symtab (input_bfd)
4022 && finfo->sections[r_symndx] == NULL))
4023 {
4024 long indx;
4025
4026 /* This is a reloc against a global symbol. We
4027 have not yet output all the local symbols, so
4028 we do not know the symbol index of any global
4029 symbol. We set the rel_hash entry for this
4030 reloc to point to the global hash table entry
4031 for this symbol. The symbol index is then
4032 set at the end of elf_bfd_final_link. */
4033 indx = r_symndx - extsymoff;
4034 *rel_hash = elf_sym_hashes (input_bfd)[indx];
4035
4036 /* Setting the index to -2 tells
4037 elf_link_output_extsym that this symbol is
4038 used by a reloc. */
4039 BFD_ASSERT ((*rel_hash)->indx < 0);
4040 (*rel_hash)->indx = -2;
4041
4042 continue;
4043 }
4044
4045 /* This is a reloc against a local symbol. */
4046
4047 *rel_hash = NULL;
4048 isym = finfo->internal_syms + r_symndx;
4049 sec = finfo->sections[r_symndx];
4050 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4051 {
4052 /* I suppose the backend ought to fill in the
4053 section of any STT_SECTION symbol against a
ba4a4594
ILT
4054 processor specific section. If we have
4055 discarded a section, the output_section will
4056 be the absolute section. */
4057 if (sec != NULL
4058 && (bfd_is_abs_section (sec)
4059 || (sec->output_section != NULL
4060 && bfd_is_abs_section (sec->output_section))))
ede4eed4
KR
4061 r_symndx = 0;
4062 else if (sec == NULL || sec->owner == NULL)
4063 {
4064 bfd_set_error (bfd_error_bad_value);
4065 return false;
4066 }
4067 else
4068 {
4069 r_symndx = sec->output_section->target_index;
4070 BFD_ASSERT (r_symndx != 0);
4071 }
4072 }
4073 else
4074 {
4075 if (finfo->indices[r_symndx] == -1)
4076 {
4077 unsigned long link;
4078 const char *name;
4079 asection *osec;
4080
4081 if (finfo->info->strip == strip_all)
4082 {
4083 /* You can't do ld -r -s. */
4084 bfd_set_error (bfd_error_invalid_operation);
4085 return false;
4086 }
4087
4088 /* This symbol was skipped earlier, but
4089 since it is needed by a reloc, we
4090 must output it now. */
4091 link = symtab_hdr->sh_link;
4092 name = bfd_elf_string_from_elf_section (input_bfd,
4093 link,
4094 isym->st_name);
4095 if (name == NULL)
4096 return false;
4097
4098 osec = sec->output_section;
4099 isym->st_shndx =
4100 _bfd_elf_section_from_bfd_section (output_bfd,
4101 osec);
4102 if (isym->st_shndx == (unsigned short) -1)
4103 return false;
4104
4105 isym->st_value += sec->output_offset;
4106 if (! finfo->info->relocateable)
4107 isym->st_value += osec->vma;
4108
4109 finfo->indices[r_symndx] = output_bfd->symcount;
4110
4111 if (! elf_link_output_sym (finfo, name, isym, sec))
4112 return false;
4113 }
4114
4115 r_symndx = finfo->indices[r_symndx];
4116 }
4117
4118 irela->r_info = ELF_R_INFO (r_symndx,
4119 ELF_R_TYPE (irela->r_info));
4120 }
4121
4122 /* Swap out the relocs. */
4123 input_rel_hdr = &elf_section_data (o)->rel_hdr;
4124 output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
4125 BFD_ASSERT (output_rel_hdr->sh_entsize
4126 == input_rel_hdr->sh_entsize);
4127 irela = internal_relocs;
4128 irelaend = irela + o->reloc_count;
4129 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4130 {
4131 Elf_External_Rel *erel;
4132
4133 erel = ((Elf_External_Rel *) output_rel_hdr->contents
4134 + o->output_section->reloc_count);
4135 for (; irela < irelaend; irela++, erel++)
4136 {
4137 Elf_Internal_Rel irel;
4138
4139 irel.r_offset = irela->r_offset;
4140 irel.r_info = irela->r_info;
4141 BFD_ASSERT (irela->r_addend == 0);
4142 elf_swap_reloc_out (output_bfd, &irel, erel);
4143 }
4144 }
4145 else
4146 {
4147 Elf_External_Rela *erela;
4148
4149 BFD_ASSERT (input_rel_hdr->sh_entsize
4150 == sizeof (Elf_External_Rela));
4151 erela = ((Elf_External_Rela *) output_rel_hdr->contents
4152 + o->output_section->reloc_count);
4153 for (; irela < irelaend; irela++, erela++)
4154 elf_swap_reloca_out (output_bfd, irela, erela);
4155 }
4156
4157 o->output_section->reloc_count += o->reloc_count;
4158 }
4159 }
4160
4161 /* Write out the modified section contents. */
1726b8f0
ILT
4162 if (elf_section_data (o)->stab_info == NULL)
4163 {
4164 if (! bfd_set_section_contents (output_bfd, o->output_section,
c86158e5 4165 contents, o->output_offset,
1726b8f0
ILT
4166 (o->_cooked_size != 0
4167 ? o->_cooked_size
4168 : o->_raw_size)))
4169 return false;
4170 }
4171 else
4172 {
4173 if (! _bfd_write_section_stabs (output_bfd, o,
4174 &elf_section_data (o)->stab_info,
c86158e5 4175 contents))
1726b8f0
ILT
4176 return false;
4177 }
ede4eed4
KR
4178 }
4179
4180 return true;
4181}
4182
4183/* Generate a reloc when linking an ELF file. This is a reloc
4184 requested by the linker, and does come from any input file. This
4185 is used to build constructor and destructor tables when linking
4186 with -Ur. */
4187
4188static boolean
4189elf_reloc_link_order (output_bfd, info, output_section, link_order)
4190 bfd *output_bfd;
4191 struct bfd_link_info *info;
4192 asection *output_section;
4193 struct bfd_link_order *link_order;
4194{
4195 reloc_howto_type *howto;
4196 long indx;
4197 bfd_vma offset;
5b3b9ff6 4198 bfd_vma addend;
ede4eed4
KR
4199 struct elf_link_hash_entry **rel_hash_ptr;
4200 Elf_Internal_Shdr *rel_hdr;
4201
4202 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4203 if (howto == NULL)
4204 {
4205 bfd_set_error (bfd_error_bad_value);
4206 return false;
4207 }
4208
5b3b9ff6
ILT
4209 addend = link_order->u.reloc.p->addend;
4210
4211 /* Figure out the symbol index. */
4212 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
4213 + output_section->reloc_count);
4214 if (link_order->type == bfd_section_reloc_link_order)
4215 {
4216 indx = link_order->u.reloc.p->u.section->target_index;
4217 BFD_ASSERT (indx != 0);
4218 *rel_hash_ptr = NULL;
4219 }
4220 else
4221 {
4222 struct elf_link_hash_entry *h;
4223
4224 /* Treat a reloc against a defined symbol as though it were
4225 actually against the section. */
8881b321
ILT
4226 h = ((struct elf_link_hash_entry *)
4227 bfd_wrapped_link_hash_lookup (output_bfd, info,
4228 link_order->u.reloc.p->u.name,
4229 false, false, true));
5b3b9ff6
ILT
4230 if (h != NULL
4231 && (h->root.type == bfd_link_hash_defined
4232 || h->root.type == bfd_link_hash_defweak))
4233 {
4234 asection *section;
4235
4236 section = h->root.u.def.section;
4237 indx = section->output_section->target_index;
4238 *rel_hash_ptr = NULL;
4239 /* It seems that we ought to add the symbol value to the
4240 addend here, but in practice it has already been added
4241 because it was passed to constructor_callback. */
4242 addend += section->output_section->vma + section->output_offset;
4243 }
4244 else if (h != NULL)
4245 {
4246 /* Setting the index to -2 tells elf_link_output_extsym that
4247 this symbol is used by a reloc. */
4248 h->indx = -2;
4249 *rel_hash_ptr = h;
4250 indx = 0;
4251 }
4252 else
4253 {
4254 if (! ((*info->callbacks->unattached_reloc)
4255 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4256 (asection *) NULL, (bfd_vma) 0)))
4257 return false;
4258 indx = 0;
4259 }
4260 }
4261
ede4eed4
KR
4262 /* If this is an inplace reloc, we must write the addend into the
4263 object file. */
5b3b9ff6 4264 if (howto->partial_inplace && addend != 0)
ede4eed4
KR
4265 {
4266 bfd_size_type size;
4267 bfd_reloc_status_type rstat;
4268 bfd_byte *buf;
4269 boolean ok;
4270
4271 size = bfd_get_reloc_size (howto);
4272 buf = (bfd_byte *) bfd_zmalloc (size);
4273 if (buf == (bfd_byte *) NULL)
a9713b91 4274 return false;
5b3b9ff6 4275 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
ede4eed4
KR
4276 switch (rstat)
4277 {
4278 case bfd_reloc_ok:
4279 break;
4280 default:
4281 case bfd_reloc_outofrange:
4282 abort ();
4283 case bfd_reloc_overflow:
4284 if (! ((*info->callbacks->reloc_overflow)
4285 (info,
4286 (link_order->type == bfd_section_reloc_link_order
4287 ? bfd_section_name (output_bfd,
4288 link_order->u.reloc.p->u.section)
4289 : link_order->u.reloc.p->u.name),
5b3b9ff6
ILT
4290 howto->name, addend, (bfd *) NULL, (asection *) NULL,
4291 (bfd_vma) 0)))
ede4eed4
KR
4292 {
4293 free (buf);
4294 return false;
4295 }
4296 break;
4297 }
4298 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4299 (file_ptr) link_order->offset, size);
4300 free (buf);
4301 if (! ok)
4302 return false;
4303 }
4304
ede4eed4
KR
4305 /* The address of a reloc is relative to the section in a
4306 relocateable file, and is a virtual address in an executable
4307 file. */
4308 offset = link_order->offset;
4309 if (! info->relocateable)
4310 offset += output_section->vma;
4311
4312 rel_hdr = &elf_section_data (output_section)->rel_hdr;
4313
4314 if (rel_hdr->sh_type == SHT_REL)
4315 {
4316 Elf_Internal_Rel irel;
4317 Elf_External_Rel *erel;
4318
4319 irel.r_offset = offset;
4320 irel.r_info = ELF_R_INFO (indx, howto->type);
4321 erel = ((Elf_External_Rel *) rel_hdr->contents
4322 + output_section->reloc_count);
4323 elf_swap_reloc_out (output_bfd, &irel, erel);
4324 }
4325 else
4326 {
4327 Elf_Internal_Rela irela;
4328 Elf_External_Rela *erela;
4329
4330 irela.r_offset = offset;
4331 irela.r_info = ELF_R_INFO (indx, howto->type);
5b3b9ff6 4332 irela.r_addend = addend;
ede4eed4
KR
4333 erela = ((Elf_External_Rela *) rel_hdr->contents
4334 + output_section->reloc_count);
4335 elf_swap_reloca_out (output_bfd, &irela, erela);
4336 }
4337
4338 ++output_section->reloc_count;
4339
4340 return true;
4341}
4342
3b3753b8
MM
4343\f
4344/* Allocate a pointer to live in a linker created section. */
4345
4346boolean
4347elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
4348 bfd *abfd;
4349 struct bfd_link_info *info;
4350 elf_linker_section_t *lsect;
4351 struct elf_link_hash_entry *h;
4352 const Elf_Internal_Rela *rel;
4353{
4354 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
4355 elf_linker_section_pointers_t *linker_section_ptr;
4356 unsigned long r_symndx = ELF_R_SYM (rel->r_info);;
4357
4358 BFD_ASSERT (lsect != NULL);
4359
4360 /* Is this a global symbol? */
4361 if (h != NULL)
4362 {
4363 /* Has this symbol already been allocated, if so, our work is done */
4364 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
4365 rel->r_addend,
4366 lsect->which))
4367 return true;
4368
4369 ptr_linker_section_ptr = &h->linker_section_pointer;
4370 /* Make sure this symbol is output as a dynamic symbol. */
4371 if (h->dynindx == -1)
4372 {
4373 if (! elf_link_record_dynamic_symbol (info, h))
4374 return false;
4375 }
4376
eb82bc60
MM
4377 if (lsect->rel_section)
4378 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
3b3753b8
MM
4379 }
4380
4381 else /* Allocation of a pointer to a local symbol */
4382 {
4383 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
4384
4385 /* Allocate a table to hold the local symbols if first time */
4386 if (!ptr)
4387 {
4388 int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
4389 register unsigned int i;
4390
4391 ptr = (elf_linker_section_pointers_t **)
4392 bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *));
4393
4394 if (!ptr)
4395 return false;
4396
4397 elf_local_ptr_offsets (abfd) = ptr;
4398 for (i = 0; i < num_symbols; i++)
4399 ptr[i] = (elf_linker_section_pointers_t *)0;
4400 }
4401
4402 /* Has this symbol already been allocated, if so, our work is done */
4403 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
4404 rel->r_addend,
4405 lsect->which))
4406 return true;
4407
4408 ptr_linker_section_ptr = &ptr[r_symndx];
4409
4410 if (info->shared)
4411 {
4412 /* If we are generating a shared object, we need to
05f927dd 4413 output a R_<xxx>_RELATIVE reloc so that the
3b3753b8
MM
4414 dynamic linker can adjust this GOT entry. */
4415 BFD_ASSERT (lsect->rel_section != NULL);
4416 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
4417 }
4418 }
4419
4420 /* Allocate space for a pointer in the linker section, and allocate a new pointer record
4421 from internal memory. */
4422 BFD_ASSERT (ptr_linker_section_ptr != NULL);
4423 linker_section_ptr = (elf_linker_section_pointers_t *)
4424 bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t));
4425
4426 if (!linker_section_ptr)
4427 return false;
4428
4429 linker_section_ptr->next = *ptr_linker_section_ptr;
4430 linker_section_ptr->addend = rel->r_addend;
4431 linker_section_ptr->which = lsect->which;
4432 linker_section_ptr->written_address_p = false;
4433 *ptr_linker_section_ptr = linker_section_ptr;
4434
cb73f5d7 4435#if 0
3b3753b8
MM
4436 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
4437 {
cb73f5d7 4438 linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8);
3b3753b8
MM
4439 lsect->hole_offset += ARCH_SIZE / 8;
4440 lsect->sym_offset += ARCH_SIZE / 8;
4441 if (lsect->sym_hash) /* Bump up symbol value if needed */
4a4953f5
MM
4442 {
4443 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
4444#ifdef DEBUG
4445 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
4446 lsect->sym_hash->root.root.string,
4447 (long)ARCH_SIZE / 8,
4448 (long)lsect->sym_hash->root.u.def.value);
4449#endif
4450 }
3b3753b8
MM
4451 }
4452 else
cb73f5d7 4453#endif
3b3753b8
MM
4454 linker_section_ptr->offset = lsect->section->_raw_size;
4455
4456 lsect->section->_raw_size += ARCH_SIZE / 8;
4457
4458#ifdef DEBUG
4459 fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
4460 lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size);
4461#endif
4462
4463 return true;
4464}
4465
4466\f
4467#if ARCH_SIZE==64
4468#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
4469#endif
4470#if ARCH_SIZE==32
4471#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
4472#endif
4473
4474/* Fill in the address for a pointer generated in alinker section. */
4475
4476bfd_vma
4477elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc)
4478 bfd *output_bfd;
4479 bfd *input_bfd;
4480 struct bfd_link_info *info;
4481 elf_linker_section_t *lsect;
4482 struct elf_link_hash_entry *h;
4483 bfd_vma relocation;
4484 const Elf_Internal_Rela *rel;
4485 int relative_reloc;
4486{
4487 elf_linker_section_pointers_t *linker_section_ptr;
4488
4489 BFD_ASSERT (lsect != NULL);
4490
3b3753b8
MM
4491 if (h != NULL) /* global symbol */
4492 {
4493 linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
4494 rel->r_addend,
4495 lsect->which);
4496
4497 BFD_ASSERT (linker_section_ptr != NULL);
4498
4499 if (! elf_hash_table (info)->dynamic_sections_created
4500 || (info->shared
4501 && info->symbolic
4502 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
4503 {
4504 /* This is actually a static link, or it is a
4505 -Bsymbolic link and the symbol is defined
4506 locally. We must initialize this entry in the
4507 global section.
4508
4509 When doing a dynamic link, we create a .rela.<xxx>
4510 relocation entry to initialize the value. This
4511 is done in the finish_dynamic_symbol routine. */
4512 if (!linker_section_ptr->written_address_p)
4513 {
4514 linker_section_ptr->written_address_p = true;
4515 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
4516 lsect->section->contents + linker_section_ptr->offset);
4517 }
4518 }
4519 }
4520 else /* local symbol */
4521 {
4522 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
4523 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4524 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
4525 linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx],
4526 rel->r_addend,
4527 lsect->which);
4528
4529 BFD_ASSERT (linker_section_ptr != NULL);
4530
4531 /* Write out pointer if it hasn't been rewritten out before */
4532 if (!linker_section_ptr->written_address_p)
4533 {
4534 linker_section_ptr->written_address_p = true;
4535 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
4536 lsect->section->contents + linker_section_ptr->offset);
4537
4538 if (info->shared)
4539 {
4540 asection *srel = lsect->rel_section;
4541 Elf_Internal_Rela outrel;
4542
4543 /* We need to generate a relative reloc for the dynamic linker. */
4544 if (!srel)
4545 lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
4546 lsect->rel_name);
4547
4548 BFD_ASSERT (srel != NULL);
4549
4550 outrel.r_offset = (lsect->section->output_section->vma
4551 + lsect->section->output_offset
4552 + linker_section_ptr->offset);
4553 outrel.r_info = ELF_R_INFO (0, relative_reloc);
4554 outrel.r_addend = 0;
4555 elf_swap_reloca_out (output_bfd, &outrel,
5a5bac64 4556 (((Elf_External_Rela *)
3b3753b8
MM
4557 lsect->section->contents)
4558 + lsect->section->reloc_count));
4559 ++lsect->section->reloc_count;
4560 }
4561 }
4562 }
4563
4564 relocation = (lsect->section->output_offset
4565 + linker_section_ptr->offset
4566 - lsect->hole_offset
4567 - lsect->sym_offset);
4568
4569#ifdef DEBUG
4570 fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4571 lsect->name, (long)relocation, (long)relocation);
4572#endif
4573
4574 /* Subtract out the addend, because it will get added back in by the normal
4575 processing. */
4576 return relocation - linker_section_ptr->addend;
4577}
This page took 0.267084 seconds and 4 git commands to generate.