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