daily update
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
c8e5ddc8 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* ELF linker code. */
22
23/* This struct is used to pass information to routines called via
24 elf_link_hash_traverse which must return failure. */
25
26struct elf_info_failed
27{
28 boolean failed;
29 struct bfd_link_info *info;
bc2b6df7 30 struct bfd_elf_version_tree *verdefs;
252b5132
RH
31};
32
a7b97311
AM
33static boolean is_global_data_symbol_definition
34 PARAMS ((bfd *, Elf_Internal_Sym *));
35static boolean elf_link_is_defined_archive_symbol
36 PARAMS ((bfd *, carsym *));
252b5132
RH
37static boolean elf_link_add_object_symbols
38 PARAMS ((bfd *, struct bfd_link_info *));
39static boolean elf_link_add_archive_symbols
40 PARAMS ((bfd *, struct bfd_link_info *));
41static boolean elf_merge_symbol
215007a6
L
42 PARAMS ((bfd *, struct bfd_link_info *, const char *,
43 Elf_Internal_Sym *, asection **, bfd_vma *,
44 struct elf_link_hash_entry **, boolean *, boolean *,
45 boolean *, boolean));
46static boolean elf_add_default_symbol
47 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
48 const char *, Elf_Internal_Sym *, asection **, bfd_vma *,
49 boolean *, boolean, boolean));
252b5132
RH
50static boolean elf_export_symbol
51 PARAMS ((struct elf_link_hash_entry *, PTR));
2b0f7ef9
JJ
52static boolean elf_finalize_dynstr
53 PARAMS ((bfd *, struct bfd_link_info *));
252b5132
RH
54static boolean elf_fix_symbol_flags
55 PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
56static boolean elf_adjust_dynamic_symbol
57 PARAMS ((struct elf_link_hash_entry *, PTR));
58static boolean elf_link_find_version_dependencies
59 PARAMS ((struct elf_link_hash_entry *, PTR));
60static boolean elf_link_find_version_dependencies
61 PARAMS ((struct elf_link_hash_entry *, PTR));
62static boolean elf_link_assign_sym_version
63 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132
RH
64static boolean elf_collect_hash_codes
65 PARAMS ((struct elf_link_hash_entry *, PTR));
3e932841 66static boolean elf_link_read_relocs_from_section
6b5bd373 67 PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
a7b97311
AM
68static size_t compute_bucket_count
69 PARAMS ((struct bfd_link_info *));
23bc299b
MM
70static void elf_link_output_relocs
71 PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
72static boolean elf_link_size_reloc_section
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
3e932841
KH
74static void elf_link_adjust_relocs
75 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
31367b81 76 struct elf_link_hash_entry **));
db6751f2
JJ
77static int elf_link_sort_cmp1
78 PARAMS ((const void *, const void *));
79static int elf_link_sort_cmp2
80 PARAMS ((const void *, const void *));
81static size_t elf_link_sort_relocs
82 PARAMS ((bfd *, struct bfd_link_info *, asection **));
73d074b4
DJ
83static boolean elf_section_ignore_discarded_relocs
84 PARAMS ((asection *));
252b5132
RH
85
86/* Given an ELF BFD, add symbols to the global hash table as
87 appropriate. */
88
89boolean
90elf_bfd_link_add_symbols (abfd, info)
91 bfd *abfd;
92 struct bfd_link_info *info;
93{
94 switch (bfd_get_format (abfd))
95 {
96 case bfd_object:
97 return elf_link_add_object_symbols (abfd, info);
98 case bfd_archive:
99 return elf_link_add_archive_symbols (abfd, info);
100 default:
101 bfd_set_error (bfd_error_wrong_format);
102 return false;
103 }
104}
105\f
7da9d88f 106/* Return true iff this is a non-common, definition of a non-function symbol. */
48dfb430 107static boolean
7da9d88f 108is_global_data_symbol_definition (abfd, sym)
86033394 109 bfd * abfd ATTRIBUTE_UNUSED;
48dfb430
NC
110 Elf_Internal_Sym * sym;
111{
112 /* Local symbols do not count, but target specific ones might. */
113 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
114 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
115 return false;
116
7da9d88f
NC
117 /* Function symbols do not count. */
118 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
119 return false;
120
48dfb430
NC
121 /* If the section is undefined, then so is the symbol. */
122 if (sym->st_shndx == SHN_UNDEF)
123 return false;
3e932841 124
48dfb430
NC
125 /* If the symbol is defined in the common section, then
126 it is a common definition and so does not count. */
127 if (sym->st_shndx == SHN_COMMON)
128 return false;
129
130 /* If the symbol is in a target specific section then we
131 must rely upon the backend to tell us what it is. */
132 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
133 /* FIXME - this function is not coded yet:
3e932841 134
48dfb430 135 return _bfd_is_global_symbol_definition (abfd, sym);
3e932841 136
48dfb430
NC
137 Instead for now assume that the definition is not global,
138 Even if this is wrong, at least the linker will behave
139 in the same way that it used to do. */
140 return false;
3e932841 141
48dfb430
NC
142 return true;
143}
144
a3a8c91d 145/* Search the symbol table of the archive element of the archive ABFD
4e8a9624 146 whose archive map contains a mention of SYMDEF, and determine if
a3a8c91d
NC
147 the symbol is defined in this element. */
148static boolean
149elf_link_is_defined_archive_symbol (abfd, symdef)
150 bfd * abfd;
151 carsym * symdef;
152{
153 Elf_Internal_Shdr * hdr;
9ad5cbcf 154 Elf_Internal_Shdr * shndx_hdr;
a3a8c91d
NC
155 Elf_External_Sym * esym;
156 Elf_External_Sym * esymend;
157 Elf_External_Sym * buf = NULL;
9ad5cbcf
AM
158 Elf_External_Sym_Shndx * shndx_buf = NULL;
159 Elf_External_Sym_Shndx * shndx;
dc810e39
AM
160 bfd_size_type symcount;
161 bfd_size_type extsymcount;
162 bfd_size_type extsymoff;
a3a8c91d 163 boolean result = false;
dc810e39
AM
164 file_ptr pos;
165 bfd_size_type amt;
3e932841 166
a3a8c91d
NC
167 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
168 if (abfd == (bfd *) NULL)
169 return false;
170
171 if (! bfd_check_format (abfd, bfd_object))
172 return false;
173
48dfb430
NC
174 /* If we have already included the element containing this symbol in the
175 link then we do not need to include it again. Just claim that any symbol
176 it contains is not a definition, so that our caller will not decide to
177 (re)include this element. */
178 if (abfd->archive_pass)
179 return false;
3e932841 180
a3a8c91d
NC
181 /* Select the appropriate symbol table. */
182 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
9ad5cbcf
AM
183 {
184 hdr = &elf_tdata (abfd)->symtab_hdr;
185 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
186 }
a3a8c91d 187 else
9ad5cbcf
AM
188 {
189 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
190 shndx_hdr = NULL;
191 }
a3a8c91d
NC
192
193 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
194
195 /* The sh_info field of the symtab header tells us where the
196 external symbols start. We don't care about the local symbols. */
197 if (elf_bad_symtab (abfd))
198 {
199 extsymcount = symcount;
200 extsymoff = 0;
201 }
202 else
203 {
204 extsymcount = symcount - hdr->sh_info;
205 extsymoff = hdr->sh_info;
206 }
207
dc810e39
AM
208 amt = extsymcount * sizeof (Elf_External_Sym);
209 buf = (Elf_External_Sym *) bfd_malloc (amt);
a3a8c91d
NC
210 if (buf == NULL && extsymcount != 0)
211 return false;
212
213 /* Read in the symbol table.
214 FIXME: This ought to be cached somewhere. */
dc810e39
AM
215 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
216 if (bfd_seek (abfd, pos, SEEK_SET) != 0
217 || bfd_bread ((PTR) buf, amt, abfd) != amt)
9ad5cbcf
AM
218 goto error_exit;
219
220 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
a3a8c91d 221 {
9ad5cbcf
AM
222 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
223 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
224 if (shndx_buf == NULL && extsymcount != 0)
225 goto error_exit;
226
227 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
228 if (bfd_seek (abfd, pos, SEEK_SET) != 0
229 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
230 goto error_exit;
a3a8c91d
NC
231 }
232
233 /* Scan the symbol table looking for SYMDEF. */
234 esymend = buf + extsymcount;
9ad5cbcf 235 for (esym = buf, shndx = shndx_buf;
a3a8c91d 236 esym < esymend;
9ad5cbcf 237 esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
a3a8c91d
NC
238 {
239 Elf_Internal_Sym sym;
240 const char * name;
241
9ad5cbcf 242 elf_swap_symbol_in (abfd, esym, shndx, &sym);
a3a8c91d
NC
243
244 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
245 if (name == (const char *) NULL)
246 break;
247
248 if (strcmp (name, symdef->name) == 0)
249 {
7da9d88f 250 result = is_global_data_symbol_definition (abfd, & sym);
a3a8c91d
NC
251 break;
252 }
253 }
254
9ad5cbcf
AM
255 error_exit:
256 if (shndx_buf != NULL)
257 free (shndx_buf);
258 if (buf != NULL)
259 free (buf);
3e932841 260
a3a8c91d
NC
261 return result;
262}
263\f
252b5132
RH
264/* Add symbols from an ELF archive file to the linker hash table. We
265 don't use _bfd_generic_link_add_archive_symbols because of a
266 problem which arises on UnixWare. The UnixWare libc.so is an
267 archive which includes an entry libc.so.1 which defines a bunch of
268 symbols. The libc.so archive also includes a number of other
269 object files, which also define symbols, some of which are the same
270 as those defined in libc.so.1. Correct linking requires that we
271 consider each object file in turn, and include it if it defines any
272 symbols we need. _bfd_generic_link_add_archive_symbols does not do
273 this; it looks through the list of undefined symbols, and includes
274 any object file which defines them. When this algorithm is used on
275 UnixWare, it winds up pulling in libc.so.1 early and defining a
276 bunch of symbols. This means that some of the other objects in the
277 archive are not included in the link, which is incorrect since they
278 precede libc.so.1 in the archive.
279
280 Fortunately, ELF archive handling is simpler than that done by
281 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
282 oddities. In ELF, if we find a symbol in the archive map, and the
283 symbol is currently undefined, we know that we must pull in that
284 object file.
285
286 Unfortunately, we do have to make multiple passes over the symbol
287 table until nothing further is resolved. */
288
289static boolean
290elf_link_add_archive_symbols (abfd, info)
291 bfd *abfd;
292 struct bfd_link_info *info;
293{
294 symindex c;
295 boolean *defined = NULL;
296 boolean *included = NULL;
297 carsym *symdefs;
298 boolean loop;
dc810e39 299 bfd_size_type amt;
252b5132
RH
300
301 if (! bfd_has_map (abfd))
302 {
303 /* An empty archive is a special case. */
304 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
305 return true;
306 bfd_set_error (bfd_error_no_armap);
307 return false;
308 }
309
310 /* Keep track of all symbols we know to be already defined, and all
311 files we know to be already included. This is to speed up the
312 second and subsequent passes. */
313 c = bfd_ardata (abfd)->symdef_count;
314 if (c == 0)
315 return true;
dc810e39
AM
316 amt = c;
317 amt *= sizeof (boolean);
318 defined = (boolean *) bfd_malloc (amt);
319 included = (boolean *) bfd_malloc (amt);
252b5132
RH
320 if (defined == (boolean *) NULL || included == (boolean *) NULL)
321 goto error_return;
dc810e39
AM
322 memset (defined, 0, (size_t) amt);
323 memset (included, 0, (size_t) amt);
252b5132
RH
324
325 symdefs = bfd_ardata (abfd)->symdefs;
326
327 do
328 {
329 file_ptr last;
330 symindex i;
331 carsym *symdef;
332 carsym *symdefend;
333
334 loop = false;
335 last = -1;
336
337 symdef = symdefs;
338 symdefend = symdef + c;
339 for (i = 0; symdef < symdefend; symdef++, i++)
340 {
341 struct elf_link_hash_entry *h;
342 bfd *element;
343 struct bfd_link_hash_entry *undefs_tail;
344 symindex mark;
345
346 if (defined[i] || included[i])
347 continue;
348 if (symdef->file_offset == last)
349 {
350 included[i] = true;
351 continue;
352 }
353
354 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
355 false, false, false);
356
357 if (h == NULL)
358 {
359 char *p, *copy;
360
361 /* If this is a default version (the name contains @@),
362 look up the symbol again without the version. The
363 effect is that references to the symbol without the
364 version will be matched by the default symbol in the
365 archive. */
366
367 p = strchr (symdef->name, ELF_VER_CHR);
368 if (p == NULL || p[1] != ELF_VER_CHR)
369 continue;
370
dc810e39 371 copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
252b5132
RH
372 if (copy == NULL)
373 goto error_return;
dc810e39 374 memcpy (copy, symdef->name, (size_t) (p - symdef->name));
252b5132
RH
375 copy[p - symdef->name] = '\0';
376
377 h = elf_link_hash_lookup (elf_hash_table (info), copy,
378 false, false, false);
379
380 bfd_release (abfd, copy);
381 }
382
383 if (h == NULL)
384 continue;
385
a3a8c91d
NC
386 if (h->root.type == bfd_link_hash_common)
387 {
388 /* We currently have a common symbol. The archive map contains
389 a reference to this symbol, so we may want to include it. We
390 only want to include it however, if this archive element
391 contains a definition of the symbol, not just another common
392 declaration of it.
393
394 Unfortunately some archivers (including GNU ar) will put
395 declarations of common symbols into their archive maps, as
396 well as real definitions, so we cannot just go by the archive
397 map alone. Instead we must read in the element's symbol
398 table and check that to see what kind of symbol definition
399 this is. */
400 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
401 continue;
402 }
403 else if (h->root.type != bfd_link_hash_undefined)
252b5132
RH
404 {
405 if (h->root.type != bfd_link_hash_undefweak)
406 defined[i] = true;
407 continue;
408 }
409
410 /* We need to include this archive member. */
252b5132
RH
411 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
412 if (element == (bfd *) NULL)
413 goto error_return;
414
415 if (! bfd_check_format (element, bfd_object))
416 goto error_return;
417
418 /* Doublecheck that we have not included this object
419 already--it should be impossible, but there may be
420 something wrong with the archive. */
421 if (element->archive_pass != 0)
422 {
423 bfd_set_error (bfd_error_bad_value);
424 goto error_return;
425 }
426 element->archive_pass = 1;
427
428 undefs_tail = info->hash->undefs_tail;
429
430 if (! (*info->callbacks->add_archive_element) (info, element,
431 symdef->name))
432 goto error_return;
433 if (! elf_link_add_object_symbols (element, info))
434 goto error_return;
435
436 /* If there are any new undefined symbols, we need to make
437 another pass through the archive in order to see whether
438 they can be defined. FIXME: This isn't perfect, because
439 common symbols wind up on undefs_tail and because an
440 undefined symbol which is defined later on in this pass
441 does not require another pass. This isn't a bug, but it
442 does make the code less efficient than it could be. */
443 if (undefs_tail != info->hash->undefs_tail)
444 loop = true;
445
446 /* Look backward to mark all symbols from this object file
447 which we have already seen in this pass. */
448 mark = i;
449 do
450 {
451 included[mark] = true;
452 if (mark == 0)
453 break;
454 --mark;
455 }
456 while (symdefs[mark].file_offset == symdef->file_offset);
457
458 /* We mark subsequent symbols from this object file as we go
459 on through the loop. */
460 last = symdef->file_offset;
461 }
462 }
463 while (loop);
464
465 free (defined);
466 free (included);
467
468 return true;
469
470 error_return:
471 if (defined != (boolean *) NULL)
472 free (defined);
473 if (included != (boolean *) NULL)
474 free (included);
475 return false;
476}
477
478/* This function is called when we want to define a new symbol. It
479 handles the various cases which arise when we find a definition in
480 a dynamic object, or when there is already a definition in a
481 dynamic object. The new symbol is described by NAME, SYM, PSEC,
482 and PVALUE. We set SYM_HASH to the hash table entry. We set
483 OVERRIDE if the old symbol is overriding a new definition. We set
484 TYPE_CHANGE_OK if it is OK for the type to change. We set
485 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
486 change, we mean that we shouldn't warn if the type or size does
456981d7
L
487 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
488 a shared object. */
252b5132
RH
489
490static boolean
491elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
456981d7 492 override, type_change_ok, size_change_ok, dt_needed)
252b5132
RH
493 bfd *abfd;
494 struct bfd_link_info *info;
495 const char *name;
496 Elf_Internal_Sym *sym;
497 asection **psec;
498 bfd_vma *pvalue;
499 struct elf_link_hash_entry **sym_hash;
500 boolean *override;
501 boolean *type_change_ok;
502 boolean *size_change_ok;
456981d7 503 boolean dt_needed;
252b5132
RH
504{
505 asection *sec;
506 struct elf_link_hash_entry *h;
507 int bind;
508 bfd *oldbfd;
509 boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
510
511 *override = false;
512
513 sec = *psec;
514 bind = ELF_ST_BIND (sym->st_info);
515
516 if (! bfd_is_und_section (sec))
517 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
518 else
519 h = ((struct elf_link_hash_entry *)
520 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
521 if (h == NULL)
522 return false;
523 *sym_hash = h;
524
525 /* This code is for coping with dynamic objects, and is only useful
526 if we are doing an ELF link. */
527 if (info->hash->creator != abfd->xvec)
528 return true;
529
530 /* For merging, we only care about real symbols. */
531
532 while (h->root.type == bfd_link_hash_indirect
533 || h->root.type == bfd_link_hash_warning)
534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
535
536 /* If we just created the symbol, mark it as being an ELF symbol.
537 Other than that, there is nothing to do--there is no merge issue
538 with a newly defined symbol--so we just return. */
539
540 if (h->root.type == bfd_link_hash_new)
541 {
542 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
543 return true;
544 }
545
546 /* OLDBFD is a BFD associated with the existing symbol. */
547
548 switch (h->root.type)
549 {
550 default:
551 oldbfd = NULL;
552 break;
553
554 case bfd_link_hash_undefined:
555 case bfd_link_hash_undefweak:
556 oldbfd = h->root.u.undef.abfd;
557 break;
558
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 oldbfd = h->root.u.def.section->owner;
562 break;
563
564 case bfd_link_hash_common:
565 oldbfd = h->root.u.c.p->section->owner;
566 break;
567 }
568
b4536acd
ILT
569 /* In cases involving weak versioned symbols, we may wind up trying
570 to merge a symbol with itself. Catch that here, to avoid the
571 confusion that results if we try to override a symbol with
accc7f69
ILT
572 itself. The additional tests catch cases like
573 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
574 dynamic object, which we do want to handle here. */
575 if (abfd == oldbfd
576 && ((abfd->flags & DYNAMIC) == 0
577 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
b4536acd
ILT
578 return true;
579
252b5132
RH
580 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
581 respectively, is from a dynamic object. */
582
583 if ((abfd->flags & DYNAMIC) != 0)
584 newdyn = true;
585 else
586 newdyn = false;
587
0035bd7b
ILT
588 if (oldbfd != NULL)
589 olddyn = (oldbfd->flags & DYNAMIC) != 0;
252b5132 590 else
0035bd7b
ILT
591 {
592 asection *hsec;
593
594 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
c44233aa 595 indices used by MIPS ELF. */
0035bd7b
ILT
596 switch (h->root.type)
597 {
598 default:
599 hsec = NULL;
600 break;
601
602 case bfd_link_hash_defined:
603 case bfd_link_hash_defweak:
604 hsec = h->root.u.def.section;
605 break;
606
607 case bfd_link_hash_common:
608 hsec = h->root.u.c.p->section;
609 break;
610 }
611
612 if (hsec == NULL)
613 olddyn = false;
614 else
615 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
616 }
252b5132
RH
617
618 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
619 respectively, appear to be a definition rather than reference. */
620
621 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
622 newdef = false;
623 else
624 newdef = true;
625
626 if (h->root.type == bfd_link_hash_undefined
627 || h->root.type == bfd_link_hash_undefweak
628 || h->root.type == bfd_link_hash_common)
629 olddef = false;
630 else
631 olddef = true;
632
633 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
634 symbol, respectively, appears to be a common symbol in a dynamic
635 object. If a symbol appears in an uninitialized section, and is
636 not weak, and is not a function, then it may be a common symbol
637 which was resolved when the dynamic object was created. We want
638 to treat such symbols specially, because they raise special
639 considerations when setting the symbol size: if the symbol
640 appears as a common symbol in a regular object, and the size in
641 the regular object is larger, we must make sure that we use the
642 larger size. This problematic case can always be avoided in C,
643 but it must be handled correctly when using Fortran shared
644 libraries.
645
646 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
647 likewise for OLDDYNCOMMON and OLDDEF.
648
649 Note that this test is just a heuristic, and that it is quite
650 possible to have an uninitialized symbol in a shared object which
651 is really a definition, rather than a common symbol. This could
652 lead to some minor confusion when the symbol really is a common
653 symbol in some regular object. However, I think it will be
654 harmless. */
655
656 if (newdyn
657 && newdef
658 && (sec->flags & SEC_ALLOC) != 0
659 && (sec->flags & SEC_LOAD) == 0
660 && sym->st_size > 0
661 && bind != STB_WEAK
662 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
663 newdyncommon = true;
664 else
665 newdyncommon = false;
666
667 if (olddyn
668 && olddef
669 && h->root.type == bfd_link_hash_defined
670 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
671 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
672 && (h->root.u.def.section->flags & SEC_LOAD) == 0
673 && h->size > 0
674 && h->type != STT_FUNC)
675 olddyncommon = true;
676 else
677 olddyncommon = false;
678
679 /* It's OK to change the type if either the existing symbol or the
456981d7
L
680 new symbol is weak unless it comes from a DT_NEEDED entry of
681 a shared object, in which case, the DT_NEEDED entry may not be
3e932841 682 required at the run time. */
252b5132 683
456981d7 684 if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
252b5132
RH
685 || h->root.type == bfd_link_hash_undefweak
686 || bind == STB_WEAK)
687 *type_change_ok = true;
688
689 /* It's OK to change the size if either the existing symbol or the
690 new symbol is weak, or if the old symbol is undefined. */
691
692 if (*type_change_ok
693 || h->root.type == bfd_link_hash_undefined)
694 *size_change_ok = true;
695
696 /* If both the old and the new symbols look like common symbols in a
697 dynamic object, set the size of the symbol to the larger of the
698 two. */
699
700 if (olddyncommon
701 && newdyncommon
702 && sym->st_size != h->size)
703 {
704 /* Since we think we have two common symbols, issue a multiple
c44233aa
AM
705 common warning if desired. Note that we only warn if the
706 size is different. If the size is the same, we simply let
707 the old symbol override the new one as normally happens with
708 symbols defined in dynamic objects. */
252b5132
RH
709
710 if (! ((*info->callbacks->multiple_common)
711 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
712 h->size, abfd, bfd_link_hash_common, sym->st_size)))
713 return false;
714
715 if (sym->st_size > h->size)
716 h->size = sym->st_size;
717
718 *size_change_ok = true;
719 }
720
721 /* If we are looking at a dynamic object, and we have found a
722 definition, we need to see if the symbol was already defined by
723 some other object. If so, we want to use the existing
724 definition, and we do not want to report a multiple symbol
725 definition error; we do this by clobbering *PSEC to be
726 bfd_und_section_ptr.
727
728 We treat a common symbol as a definition if the symbol in the
729 shared library is a function, since common symbols always
730 represent variables; this can cause confusion in principle, but
731 any such confusion would seem to indicate an erroneous program or
732 shared library. We also permit a common symbol in a regular
0525d26e
ILT
733 object to override a weak symbol in a shared object.
734
735 We prefer a non-weak definition in a shared library to a weak
456981d7
L
736 definition in the executable unless it comes from a DT_NEEDED
737 entry of a shared object, in which case, the DT_NEEDED entry
3e932841 738 may not be required at the run time. */
252b5132
RH
739
740 if (newdyn
741 && newdef
742 && (olddef
743 || (h->root.type == bfd_link_hash_common
744 && (bind == STB_WEAK
0525d26e 745 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
3e932841 746 && (h->root.type != bfd_link_hash_defweak
456981d7 747 || dt_needed
0525d26e 748 || bind == STB_WEAK))
252b5132
RH
749 {
750 *override = true;
751 newdef = false;
752 newdyncommon = false;
753
754 *psec = sec = bfd_und_section_ptr;
755 *size_change_ok = true;
756
757 /* If we get here when the old symbol is a common symbol, then
c44233aa
AM
758 we are explicitly letting it override a weak symbol or
759 function in a dynamic object, and we don't want to warn about
760 a type change. If the old symbol is a defined symbol, a type
761 change warning may still be appropriate. */
252b5132
RH
762
763 if (h->root.type == bfd_link_hash_common)
764 *type_change_ok = true;
765 }
766
767 /* Handle the special case of an old common symbol merging with a
768 new symbol which looks like a common symbol in a shared object.
769 We change *PSEC and *PVALUE to make the new symbol look like a
770 common symbol, and let _bfd_generic_link_add_one_symbol will do
771 the right thing. */
772
773 if (newdyncommon
774 && h->root.type == bfd_link_hash_common)
775 {
776 *override = true;
777 newdef = false;
778 newdyncommon = false;
779 *pvalue = sym->st_size;
780 *psec = sec = bfd_com_section_ptr;
781 *size_change_ok = true;
782 }
783
784 /* If the old symbol is from a dynamic object, and the new symbol is
785 a definition which is not from a dynamic object, then the new
786 symbol overrides the old symbol. Symbols from regular files
787 always take precedence over symbols from dynamic objects, even if
788 they are defined after the dynamic object in the link.
789
790 As above, we again permit a common symbol in a regular object to
791 override a definition in a shared object if the shared object
0525d26e
ILT
792 symbol is a function or is weak.
793
794 As above, we permit a non-weak definition in a shared object to
795 override a weak definition in a regular object. */
252b5132
RH
796
797 if (! newdyn
798 && (newdef
799 || (bfd_is_com_section (sec)
800 && (h->root.type == bfd_link_hash_defweak
801 || h->type == STT_FUNC)))
802 && olddyn
803 && olddef
0525d26e
ILT
804 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
805 && (bind != STB_WEAK
806 || h->root.type == bfd_link_hash_defweak))
252b5132
RH
807 {
808 /* Change the hash table entry to undefined, and let
809 _bfd_generic_link_add_one_symbol do the right thing with the
810 new definition. */
811
812 h->root.type = bfd_link_hash_undefined;
813 h->root.u.undef.abfd = h->root.u.def.section->owner;
814 *size_change_ok = true;
815
816 olddef = false;
817 olddyncommon = false;
818
819 /* We again permit a type change when a common symbol may be
c44233aa 820 overriding a function. */
252b5132
RH
821
822 if (bfd_is_com_section (sec))
823 *type_change_ok = true;
824
825 /* This union may have been set to be non-NULL when this symbol
826 was seen in a dynamic object. We must force the union to be
827 NULL, so that it is correct for a regular symbol. */
828
829 h->verinfo.vertree = NULL;
830
831 /* In this special case, if H is the target of an indirection,
c44233aa
AM
832 we want the caller to frob with H rather than with the
833 indirect symbol. That will permit the caller to redefine the
834 target of the indirection, rather than the indirect symbol
835 itself. FIXME: This will break the -y option if we store a
836 symbol with a different name. */
252b5132
RH
837 *sym_hash = h;
838 }
839
840 /* Handle the special case of a new common symbol merging with an
841 old symbol that looks like it might be a common symbol defined in
842 a shared object. Note that we have already handled the case in
843 which a new common symbol should simply override the definition
844 in the shared library. */
845
846 if (! newdyn
847 && bfd_is_com_section (sec)
848 && olddyncommon)
849 {
850 /* It would be best if we could set the hash table entry to a
851 common symbol, but we don't know what to use for the section
852 or the alignment. */
853 if (! ((*info->callbacks->multiple_common)
854 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
855 h->size, abfd, bfd_link_hash_common, sym->st_size)))
856 return false;
857
858 /* If the predumed common symbol in the dynamic object is
c44233aa 859 larger, pretend that the new symbol has its size. */
252b5132
RH
860
861 if (h->size > *pvalue)
862 *pvalue = h->size;
863
864 /* FIXME: We no longer know the alignment required by the symbol
865 in the dynamic object, so we just wind up using the one from
866 the regular object. */
867
868 olddef = false;
869 olddyncommon = false;
870
871 h->root.type = bfd_link_hash_undefined;
872 h->root.u.undef.abfd = h->root.u.def.section->owner;
873
874 *size_change_ok = true;
875 *type_change_ok = true;
876
877 h->verinfo.vertree = NULL;
878 }
879
0525d26e
ILT
880 /* Handle the special case of a weak definition in a regular object
881 followed by a non-weak definition in a shared object. In this
456981d7
L
882 case, we prefer the definition in the shared object unless it
883 comes from a DT_NEEDED entry of a shared object, in which case,
3e932841 884 the DT_NEEDED entry may not be required at the run time. */
0525d26e 885 if (olddef
456981d7 886 && ! dt_needed
0525d26e
ILT
887 && h->root.type == bfd_link_hash_defweak
888 && newdef
889 && newdyn
890 && bind != STB_WEAK)
b4536acd
ILT
891 {
892 /* To make this work we have to frob the flags so that the rest
c44233aa
AM
893 of the code does not think we are using the regular
894 definition. */
64df8d0b
ILT
895 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
896 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
897 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
898 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
899 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
900 | ELF_LINK_HASH_DEF_DYNAMIC);
b4536acd
ILT
901
902 /* If H is the target of an indirection, we want the caller to
c44233aa
AM
903 use H rather than the indirect symbol. Otherwise if we are
904 defining a new indirect symbol we will wind up attaching it
905 to the entry we are overriding. */
b4536acd
ILT
906 *sym_hash = h;
907 }
0525d26e
ILT
908
909 /* Handle the special case of a non-weak definition in a shared
910 object followed by a weak definition in a regular object. In
911 this case we prefer to definition in the shared object. To make
912 this work we have to tell the caller to not treat the new symbol
913 as a definition. */
914 if (olddef
915 && olddyn
916 && h->root.type != bfd_link_hash_defweak
917 && newdef
918 && ! newdyn
919 && bind == STB_WEAK)
920 *override = true;
921
252b5132
RH
922 return true;
923}
924
215007a6
L
925/* This function is called to create an indirect symbol from the
926 default for the symbol with the default version if needed. The
927 symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE. We
928 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
929 indicates if it comes from a DT_NEEDED entry of a shared object. */
930
931static boolean
932elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
933 dynsym, override, dt_needed)
934 bfd *abfd;
935 struct bfd_link_info *info;
936 struct elf_link_hash_entry *h;
937 const char *name;
938 Elf_Internal_Sym *sym;
939 asection **sec;
940 bfd_vma *value;
941 boolean *dynsym;
942 boolean override;
943 boolean dt_needed;
944{
945 boolean type_change_ok;
946 boolean size_change_ok;
947 char *shortname;
948 struct elf_link_hash_entry *hi;
949 struct elf_backend_data *bed;
950 boolean collect;
951 boolean dynamic;
952 char *p;
953
954 /* If this symbol has a version, and it is the default version, we
955 create an indirect symbol from the default name to the fully
956 decorated name. This will cause external references which do not
957 specify a version to be bound to this version of the symbol. */
958 p = strchr (name, ELF_VER_CHR);
959 if (p == NULL || p[1] != ELF_VER_CHR)
960 return true;
961
962 if (override)
963 {
964 /* We are overridden by an old defition. We need to check if we
965 need to crreate the indirect symbol from the default name. */
966 hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
967 false, false);
968 BFD_ASSERT (hi != NULL);
969 if (hi == h)
970 return true;
971 while (hi->root.type == bfd_link_hash_indirect
972 || hi->root.type == bfd_link_hash_warning)
973 {
974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
975 if (hi == h)
976 return true;
977 }
978 }
979
980 bed = get_elf_backend_data (abfd);
981 collect = bed->collect;
982 dynamic = (abfd->flags & DYNAMIC) != 0;
983
984 shortname = bfd_hash_allocate (&info->hash->table,
985 (size_t) (p - name + 1));
986 if (shortname == NULL)
987 return false;
988 strncpy (shortname, name, (size_t) (p - name));
989 shortname [p - name] = '\0';
990
991 /* We are going to create a new symbol. Merge it with any existing
992 symbol with this name. For the purposes of the merge, act as
993 though we were defining the symbol we just defined, although we
994 actually going to define an indirect symbol. */
995 type_change_ok = false;
996 size_change_ok = false;
997 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
998 &hi, &override, &type_change_ok,
999 &size_change_ok, dt_needed))
1000 return false;
1001
1002 if (! override)
1003 {
1004 if (! (_bfd_generic_link_add_one_symbol
1005 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1006 (bfd_vma) 0, name, false, collect,
1007 (struct bfd_link_hash_entry **) &hi)))
1008 return false;
1009 }
1010 else
1011 {
1012 /* In this case the symbol named SHORTNAME is overriding the
1013 indirect symbol we want to add. We were planning on making
1014 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1015 is the name without a version. NAME is the fully versioned
1016 name, and it is the default version.
1017
1018 Overriding means that we already saw a definition for the
1019 symbol SHORTNAME in a regular object, and it is overriding
1020 the symbol defined in the dynamic object.
1021
1022 When this happens, we actually want to change NAME, the
1023 symbol we just added, to refer to SHORTNAME. This will cause
1024 references to NAME in the shared object to become references
1025 to SHORTNAME in the regular object. This is what we expect
1026 when we override a function in a shared object: that the
1027 references in the shared object will be mapped to the
1028 definition in the regular object. */
1029
1030 while (hi->root.type == bfd_link_hash_indirect
1031 || hi->root.type == bfd_link_hash_warning)
1032 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1033
1034 h->root.type = bfd_link_hash_indirect;
1035 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1036 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1037 {
1038 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1039 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1040 if (hi->elf_link_hash_flags
1041 & (ELF_LINK_HASH_REF_REGULAR
1042 | ELF_LINK_HASH_DEF_REGULAR))
1043 {
1044 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1045 return false;
1046 }
1047 }
1048
1049 /* Now set HI to H, so that the following code will set the
c44233aa 1050 other fields correctly. */
215007a6
L
1051 hi = h;
1052 }
1053
1054 /* If there is a duplicate definition somewhere, then HI may not
1055 point to an indirect symbol. We will have reported an error to
1056 the user in that case. */
1057
1058 if (hi->root.type == bfd_link_hash_indirect)
1059 {
1060 struct elf_link_hash_entry *ht;
1061
1062 /* If the symbol became indirect, then we assume that we have
1063 not seen a definition before. */
1064 BFD_ASSERT ((hi->elf_link_hash_flags
1065 & (ELF_LINK_HASH_DEF_DYNAMIC
1066 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1067
1068 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1069 (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
1070
1071 /* See if the new flags lead us to realize that the symbol must
1072 be dynamic. */
1073 if (! *dynsym)
1074 {
1075 if (! dynamic)
1076 {
1077 if (info->shared
1078 || ((hi->elf_link_hash_flags
1079 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1080 *dynsym = true;
1081 }
1082 else
1083 {
1084 if ((hi->elf_link_hash_flags
1085 & ELF_LINK_HASH_REF_REGULAR) != 0)
1086 *dynsym = true;
1087 }
1088 }
1089 }
1090
1091 /* We also need to define an indirection from the nondefault version
1092 of the symbol. */
1093
1094 shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
1095 if (shortname == NULL)
1096 return false;
1097 strncpy (shortname, name, (size_t) (p - name));
1098 strcpy (shortname + (p - name), p + 1);
1099
1100 /* Once again, merge with any existing symbol. */
1101 type_change_ok = false;
1102 size_change_ok = false;
1103 if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
1104 &hi, &override, &type_change_ok,
1105 &size_change_ok, dt_needed))
1106 return false;
1107
1108 if (override)
1109 {
1110 /* Here SHORTNAME is a versioned name, so we don't expect to see
1111 the type of override we do in the case above. */
1112 (*_bfd_error_handler)
1113 (_("%s: warning: unexpected redefinition of `%s'"),
1114 bfd_archive_filename (abfd), shortname);
1115 }
1116 else
1117 {
1118 if (! (_bfd_generic_link_add_one_symbol
1119 (info, abfd, shortname, BSF_INDIRECT,
1120 bfd_ind_section_ptr, (bfd_vma) 0, name, false,
1121 collect, (struct bfd_link_hash_entry **) &hi)))
1122 return false;
1123
1124 /* If there is a duplicate definition somewhere, then HI may not
1125 point to an indirect symbol. We will have reported an error
1126 to the user in that case. */
1127
1128 if (hi->root.type == bfd_link_hash_indirect)
1129 {
1130 /* If the symbol became indirect, then we assume that we have
1131 not seen a definition before. */
1132 BFD_ASSERT ((hi->elf_link_hash_flags
1133 & (ELF_LINK_HASH_DEF_DYNAMIC
1134 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1135
c44233aa 1136 (*bed->elf_backend_copy_indirect_symbol) (h, hi);
215007a6
L
1137
1138 /* See if the new flags lead us to realize that the symbol
1139 must be dynamic. */
1140 if (! *dynsym)
1141 {
1142 if (! dynamic)
1143 {
1144 if (info->shared
1145 || ((hi->elf_link_hash_flags
1146 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1147 *dynsym = true;
1148 }
1149 else
1150 {
1151 if ((hi->elf_link_hash_flags
1152 & ELF_LINK_HASH_REF_REGULAR) != 0)
1153 *dynsym = true;
1154 }
1155 }
1156 }
1157 }
1158
1159 return true;
1160}
1161
252b5132
RH
1162/* Add symbols from an ELF object file to the linker hash table. */
1163
1164static boolean
1165elf_link_add_object_symbols (abfd, info)
1166 bfd *abfd;
1167 struct bfd_link_info *info;
1168{
1169 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
1170 const Elf_Internal_Sym *,
1171 const char **, flagword *,
1172 asection **, bfd_vma *));
1173 boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
1174 asection *, const Elf_Internal_Rela *));
1175 boolean collect;
1176 Elf_Internal_Shdr *hdr;
9ad5cbcf 1177 Elf_Internal_Shdr *shndx_hdr;
dc810e39
AM
1178 bfd_size_type symcount;
1179 bfd_size_type extsymcount;
1180 bfd_size_type extsymoff;
252b5132 1181 Elf_External_Sym *buf = NULL;
9ad5cbcf
AM
1182 Elf_External_Sym_Shndx *shndx_buf = NULL;
1183 Elf_External_Sym_Shndx *shndx;
252b5132
RH
1184 struct elf_link_hash_entry **sym_hash;
1185 boolean dynamic;
252b5132
RH
1186 Elf_External_Versym *extversym = NULL;
1187 Elf_External_Versym *ever;
1188 Elf_External_Dyn *dynbuf = NULL;
1189 struct elf_link_hash_entry *weaks;
1190 Elf_External_Sym *esym;
1191 Elf_External_Sym *esymend;
c61b8717 1192 struct elf_backend_data *bed;
74816898 1193 boolean dt_needed;
8ea2e4bd 1194 struct elf_link_hash_table * hash_table;
dc810e39
AM
1195 file_ptr pos;
1196 bfd_size_type amt;
8ea2e4bd
NC
1197
1198 hash_table = elf_hash_table (info);
252b5132 1199
c61b8717
RH
1200 bed = get_elf_backend_data (abfd);
1201 add_symbol_hook = bed->elf_add_symbol_hook;
1202 collect = bed->collect;
252b5132
RH
1203
1204 if ((abfd->flags & DYNAMIC) == 0)
1205 dynamic = false;
1206 else
1207 {
1208 dynamic = true;
1209
1210 /* You can't use -r against a dynamic object. Also, there's no
1211 hope of using a dynamic object which does not exactly match
1212 the format of the output file. */
1213 if (info->relocateable || info->hash->creator != abfd->xvec)
1214 {
1215 bfd_set_error (bfd_error_invalid_operation);
1216 goto error_return;
1217 }
1218 }
1219
1220 /* As a GNU extension, any input sections which are named
1221 .gnu.warning.SYMBOL are treated as warning symbols for the given
1222 symbol. This differs from .gnu.warning sections, which generate
1223 warnings when they are included in an output file. */
1224 if (! info->shared)
1225 {
1226 asection *s;
1227
1228 for (s = abfd->sections; s != NULL; s = s->next)
1229 {
1230 const char *name;
1231
1232 name = bfd_get_section_name (abfd, s);
1233 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
1234 {
1235 char *msg;
1236 bfd_size_type sz;
1237
1238 name += sizeof ".gnu.warning." - 1;
1239
1240 /* If this is a shared object, then look up the symbol
1241 in the hash table. If it is there, and it is already
1242 been defined, then we will not be using the entry
1243 from this shared object, so we don't need to warn.
1244 FIXME: If we see the definition in a regular object
1245 later on, we will warn, but we shouldn't. The only
1246 fix is to keep track of what warnings we are supposed
1247 to emit, and then handle them all at the end of the
1248 link. */
1249 if (dynamic && abfd->xvec == info->hash->creator)
1250 {
1251 struct elf_link_hash_entry *h;
1252
8ea2e4bd 1253 h = elf_link_hash_lookup (hash_table, name,
252b5132
RH
1254 false, false, true);
1255
1256 /* FIXME: What about bfd_link_hash_common? */
1257 if (h != NULL
1258 && (h->root.type == bfd_link_hash_defined
1259 || h->root.type == bfd_link_hash_defweak))
1260 {
1261 /* We don't want to issue this warning. Clobber
c44233aa
AM
1262 the section size so that the warning does not
1263 get copied into the output file. */
252b5132
RH
1264 s->_raw_size = 0;
1265 continue;
1266 }
1267 }
1268
1269 sz = bfd_section_size (abfd, s);
1270 msg = (char *) bfd_alloc (abfd, sz + 1);
1271 if (msg == NULL)
1272 goto error_return;
1273
1274 if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
1275 goto error_return;
1276
1277 msg[sz] = '\0';
1278
1279 if (! (_bfd_generic_link_add_one_symbol
1280 (info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
1281 false, collect, (struct bfd_link_hash_entry **) NULL)))
1282 goto error_return;
1283
1284 if (! info->relocateable)
1285 {
1286 /* Clobber the section size so that the warning does
c44233aa 1287 not get copied into the output file. */
252b5132
RH
1288 s->_raw_size = 0;
1289 }
1290 }
1291 }
1292 }
1293
1294 /* If this is a dynamic object, we always link against the .dynsym
1295 symbol table, not the .symtab symbol table. The dynamic linker
1296 will only see the .dynsym symbol table, so there is no reason to
1297 look at .symtab for a dynamic object. */
1298
1299 if (! dynamic || elf_dynsymtab (abfd) == 0)
9ad5cbcf
AM
1300 {
1301 hdr = &elf_tdata (abfd)->symtab_hdr;
1302 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1303 }
252b5132 1304 else
9ad5cbcf
AM
1305 {
1306 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1307 shndx_hdr = NULL;
1308 }
252b5132
RH
1309
1310 if (dynamic)
1311 {
1312 /* Read in any version definitions. */
1313
1314 if (! _bfd_elf_slurp_version_tables (abfd))
1315 goto error_return;
1316
1317 /* Read in the symbol versions, but don't bother to convert them
c44233aa 1318 to internal format. */
252b5132
RH
1319 if (elf_dynversym (abfd) != 0)
1320 {
1321 Elf_Internal_Shdr *versymhdr;
1322
1323 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
6e5222be 1324 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
252b5132
RH
1325 if (extversym == NULL)
1326 goto error_return;
dc810e39 1327 amt = versymhdr->sh_size;
252b5132 1328 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
dc810e39 1329 || bfd_bread ((PTR) extversym, amt, abfd) != amt)
252b5132
RH
1330 goto error_return;
1331 }
1332 }
1333
1334 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1335
1336 /* The sh_info field of the symtab header tells us where the
1337 external symbols start. We don't care about the local symbols at
1338 this point. */
1339 if (elf_bad_symtab (abfd))
1340 {
1341 extsymcount = symcount;
1342 extsymoff = 0;
1343 }
1344 else
1345 {
1346 extsymcount = symcount - hdr->sh_info;
1347 extsymoff = hdr->sh_info;
1348 }
1349
dc810e39
AM
1350 amt = extsymcount * sizeof (Elf_External_Sym);
1351 buf = (Elf_External_Sym *) bfd_malloc (amt);
252b5132
RH
1352 if (buf == NULL && extsymcount != 0)
1353 goto error_return;
1354
9ad5cbcf
AM
1355 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1356 {
1357 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1358 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1359 if (shndx_buf == NULL && extsymcount != 0)
1360 goto error_return;
1361 }
1362
252b5132
RH
1363 /* We store a pointer to the hash table entry for each external
1364 symbol. */
dc810e39
AM
1365 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
1366 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
252b5132
RH
1367 if (sym_hash == NULL)
1368 goto error_return;
1369 elf_sym_hashes (abfd) = sym_hash;
1370
74816898
L
1371 dt_needed = false;
1372
252b5132
RH
1373 if (! dynamic)
1374 {
1375 /* If we are creating a shared library, create all the dynamic
c44233aa
AM
1376 sections immediately. We need to attach them to something,
1377 so we attach them to this BFD, provided it is the right
1378 format. FIXME: If there are no input BFD's of the same
1379 format as the output, we can't make a shared library. */
252b5132 1380 if (info->shared
8ea2e4bd
NC
1381 && is_elf_hash_table (info)
1382 && ! hash_table->dynamic_sections_created
252b5132
RH
1383 && abfd->xvec == info->hash->creator)
1384 {
1385 if (! elf_link_create_dynamic_sections (abfd, info))
1386 goto error_return;
1387 }
1388 }
8ea2e4bd
NC
1389 else if (! is_elf_hash_table (info))
1390 goto error_return;
252b5132
RH
1391 else
1392 {
1393 asection *s;
1394 boolean add_needed;
1395 const char *name;
1396 bfd_size_type oldsize;
1397 bfd_size_type strindex;
1398
1399 /* Find the name to use in a DT_NEEDED entry that refers to this
1400 object. If the object has a DT_SONAME entry, we use it.
1401 Otherwise, if the generic linker stuck something in
1402 elf_dt_name, we use that. Otherwise, we just use the file
1403 name. If the generic linker put a null string into
1404 elf_dt_name, we don't make a DT_NEEDED entry at all, even if
1405 there is a DT_SONAME entry. */
1406 add_needed = true;
7913c838 1407 name = bfd_get_filename (abfd);
252b5132
RH
1408 if (elf_dt_name (abfd) != NULL)
1409 {
1410 name = elf_dt_name (abfd);
1411 if (*name == '\0')
74816898
L
1412 {
1413 if (elf_dt_soname (abfd) != NULL)
c44233aa 1414 dt_needed = true;
74816898
L
1415
1416 add_needed = false;
1417 }
252b5132
RH
1418 }
1419 s = bfd_get_section_by_name (abfd, ".dynamic");
1420 if (s != NULL)
1421 {
1422 Elf_External_Dyn *extdyn;
1423 Elf_External_Dyn *extdynend;
1424 int elfsec;
dc810e39 1425 unsigned long shlink;
a963dc6a
L
1426 int rpath;
1427 int runpath;
252b5132 1428
dc810e39 1429 dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
252b5132
RH
1430 if (dynbuf == NULL)
1431 goto error_return;
1432
1433 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
1434 (file_ptr) 0, s->_raw_size))
1435 goto error_return;
1436
1437 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1438 if (elfsec == -1)
1439 goto error_return;
dc810e39 1440 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132 1441
20e29382
JL
1442 {
1443 /* The shared libraries distributed with hpux11 have a bogus
1444 sh_link field for the ".dynamic" section. This code detects
dc810e39
AM
1445 when SHLINK refers to a section that is not a string table
1446 and tries to find the string table for the ".dynsym" section
20e29382 1447 instead. */
dc810e39
AM
1448 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
1449 if (shdr->sh_type != SHT_STRTAB)
20e29382 1450 {
dc810e39
AM
1451 asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
1452 int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
1453 if (elfdsec == -1)
20e29382 1454 goto error_return;
dc810e39 1455 shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
20e29382
JL
1456 }
1457 }
1458
252b5132
RH
1459 extdyn = dynbuf;
1460 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
a963dc6a
L
1461 rpath = 0;
1462 runpath = 0;
252b5132
RH
1463 for (; extdyn < extdynend; extdyn++)
1464 {
1465 Elf_Internal_Dyn dyn;
1466
1467 elf_swap_dyn_in (abfd, extdyn, &dyn);
1468 if (dyn.d_tag == DT_SONAME)
1469 {
dc810e39
AM
1470 unsigned int tagv = dyn.d_un.d_val;
1471 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1472 if (name == NULL)
1473 goto error_return;
1474 }
1475 if (dyn.d_tag == DT_NEEDED)
1476 {
1477 struct bfd_link_needed_list *n, **pn;
1478 char *fnm, *anm;
dc810e39 1479 unsigned int tagv = dyn.d_un.d_val;
252b5132 1480
dc810e39
AM
1481 amt = sizeof (struct bfd_link_needed_list);
1482 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1483 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1484 if (n == NULL || fnm == NULL)
1485 goto error_return;
dc810e39 1486 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
252b5132
RH
1487 if (anm == NULL)
1488 goto error_return;
1489 strcpy (anm, fnm);
1490 n->name = anm;
1491 n->by = abfd;
1492 n->next = NULL;
8ea2e4bd 1493 for (pn = & hash_table->needed;
252b5132
RH
1494 *pn != NULL;
1495 pn = &(*pn)->next)
1496 ;
1497 *pn = n;
1498 }
a963dc6a
L
1499 if (dyn.d_tag == DT_RUNPATH)
1500 {
1501 struct bfd_link_needed_list *n, **pn;
1502 char *fnm, *anm;
dc810e39 1503 unsigned int tagv = dyn.d_un.d_val;
a963dc6a
L
1504
1505 /* When we see DT_RPATH before DT_RUNPATH, we have
512a2384
AM
1506 to clear runpath. Do _NOT_ bfd_release, as that
1507 frees all more recently bfd_alloc'd blocks as
1508 well. */
8ea2e4bd
NC
1509 if (rpath && hash_table->runpath)
1510 hash_table->runpath = NULL;
a963dc6a 1511
dc810e39
AM
1512 amt = sizeof (struct bfd_link_needed_list);
1513 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1514 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a
L
1515 if (n == NULL || fnm == NULL)
1516 goto error_return;
dc810e39 1517 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
a963dc6a
L
1518 if (anm == NULL)
1519 goto error_return;
1520 strcpy (anm, fnm);
1521 n->name = anm;
1522 n->by = abfd;
1523 n->next = NULL;
8ea2e4bd 1524 for (pn = & hash_table->runpath;
a963dc6a
L
1525 *pn != NULL;
1526 pn = &(*pn)->next)
1527 ;
1528 *pn = n;
1529 runpath = 1;
1530 rpath = 0;
1531 }
3e932841 1532 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
a963dc6a 1533 if (!runpath && dyn.d_tag == DT_RPATH)
c44233aa 1534 {
a963dc6a
L
1535 struct bfd_link_needed_list *n, **pn;
1536 char *fnm, *anm;
dc810e39 1537 unsigned int tagv = dyn.d_un.d_val;
a963dc6a 1538
dc810e39
AM
1539 amt = sizeof (struct bfd_link_needed_list);
1540 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1541 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
a963dc6a
L
1542 if (n == NULL || fnm == NULL)
1543 goto error_return;
dc810e39 1544 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
a963dc6a
L
1545 if (anm == NULL)
1546 goto error_return;
1547 strcpy (anm, fnm);
1548 n->name = anm;
1549 n->by = abfd;
1550 n->next = NULL;
8ea2e4bd 1551 for (pn = & hash_table->runpath;
a963dc6a
L
1552 *pn != NULL;
1553 pn = &(*pn)->next)
1554 ;
1555 *pn = n;
1556 rpath = 1;
1557 }
252b5132
RH
1558 }
1559
1560 free (dynbuf);
1561 dynbuf = NULL;
1562 }
1563
1564 /* We do not want to include any of the sections in a dynamic
1565 object in the output file. We hack by simply clobbering the
1566 list of sections in the BFD. This could be handled more
1567 cleanly by, say, a new section flag; the existing
1568 SEC_NEVER_LOAD flag is not the one we want, because that one
1569 still implies that the section takes up space in the output
1570 file. */
c601ffdb 1571 bfd_section_list_clear (abfd);
252b5132
RH
1572
1573 /* If this is the first dynamic object found in the link, create
1574 the special sections required for dynamic linking. */
8ea2e4bd
NC
1575 if (! hash_table->dynamic_sections_created)
1576 if (! elf_link_create_dynamic_sections (abfd, info))
1577 goto error_return;
252b5132
RH
1578
1579 if (add_needed)
1580 {
1581 /* Add a DT_NEEDED entry for this dynamic object. */
2b0f7ef9
JJ
1582 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
1583 strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
252b5132
RH
1584 if (strindex == (bfd_size_type) -1)
1585 goto error_return;
1586
2b0f7ef9 1587 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
252b5132
RH
1588 {
1589 asection *sdyn;
1590 Elf_External_Dyn *dyncon, *dynconend;
1591
1592 /* The hash table size did not change, which means that
1593 the dynamic object name was already entered. If we
1594 have already included this dynamic object in the
1595 link, just ignore it. There is no reason to include
1596 a particular dynamic object more than once. */
8ea2e4bd 1597 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
252b5132
RH
1598 BFD_ASSERT (sdyn != NULL);
1599
1600 dyncon = (Elf_External_Dyn *) sdyn->contents;
1601 dynconend = (Elf_External_Dyn *) (sdyn->contents +
1602 sdyn->_raw_size);
1603 for (; dyncon < dynconend; dyncon++)
1604 {
1605 Elf_Internal_Dyn dyn;
1606
8ea2e4bd 1607 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
252b5132
RH
1608 if (dyn.d_tag == DT_NEEDED
1609 && dyn.d_un.d_val == strindex)
1610 {
1611 if (buf != NULL)
1612 free (buf);
1613 if (extversym != NULL)
1614 free (extversym);
2b0f7ef9 1615 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
252b5132
RH
1616 return true;
1617 }
1618 }
1619 }
1620
dc810e39 1621 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
252b5132
RH
1622 goto error_return;
1623 }
1624
1625 /* Save the SONAME, if there is one, because sometimes the
c44233aa 1626 linker emulation code will need to know it. */
252b5132 1627 if (*name == '\0')
210ba1e8 1628 name = basename (bfd_get_filename (abfd));
252b5132
RH
1629 elf_dt_name (abfd) = name;
1630 }
1631
dc810e39
AM
1632 pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
1633 amt = extsymcount * sizeof (Elf_External_Sym);
1634 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1635 || bfd_bread ((PTR) buf, amt, abfd) != amt)
252b5132
RH
1636 goto error_return;
1637
9ad5cbcf
AM
1638 if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
1639 {
1640 amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
1641 pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
1642 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1643 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1644 goto error_return;
1645 }
1646
252b5132
RH
1647 weaks = NULL;
1648
1649 ever = extversym != NULL ? extversym + extsymoff : NULL;
1650 esymend = buf + extsymcount;
9ad5cbcf 1651 for (esym = buf, shndx = shndx_buf;
252b5132 1652 esym < esymend;
9ad5cbcf
AM
1653 esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
1654 shndx = (shndx != NULL ? shndx + 1 : NULL))
252b5132
RH
1655 {
1656 Elf_Internal_Sym sym;
1657 int bind;
1658 bfd_vma value;
1659 asection *sec;
1660 flagword flags;
1661 const char *name;
1662 struct elf_link_hash_entry *h;
1663 boolean definition;
1664 boolean size_change_ok, type_change_ok;
1665 boolean new_weakdef;
1666 unsigned int old_alignment;
215007a6
L
1667 boolean override;
1668
1669 override = false;
252b5132 1670
9ad5cbcf 1671 elf_swap_symbol_in (abfd, esym, shndx, &sym);
252b5132
RH
1672
1673 flags = BSF_NO_FLAGS;
1674 sec = NULL;
1675 value = sym.st_value;
1676 *sym_hash = NULL;
1677
1678 bind = ELF_ST_BIND (sym.st_info);
1679 if (bind == STB_LOCAL)
1680 {
1681 /* This should be impossible, since ELF requires that all
1682 global symbols follow all local symbols, and that sh_info
1683 point to the first global symbol. Unfortunatealy, Irix 5
1684 screws this up. */
1685 continue;
1686 }
1687 else if (bind == STB_GLOBAL)
1688 {
1689 if (sym.st_shndx != SHN_UNDEF
1690 && sym.st_shndx != SHN_COMMON)
1691 flags = BSF_GLOBAL;
252b5132
RH
1692 }
1693 else if (bind == STB_WEAK)
1694 flags = BSF_WEAK;
1695 else
1696 {
1697 /* Leave it up to the processor backend. */
1698 }
1699
1700 if (sym.st_shndx == SHN_UNDEF)
1701 sec = bfd_und_section_ptr;
9ad5cbcf 1702 else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
252b5132
RH
1703 {
1704 sec = section_from_elf_index (abfd, sym.st_shndx);
1705 if (sec == NULL)
1706 sec = bfd_abs_section_ptr;
1707 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1708 value -= sec->vma;
1709 }
1710 else if (sym.st_shndx == SHN_ABS)
1711 sec = bfd_abs_section_ptr;
1712 else if (sym.st_shndx == SHN_COMMON)
1713 {
1714 sec = bfd_com_section_ptr;
1715 /* What ELF calls the size we call the value. What ELF
1716 calls the value we call the alignment. */
1717 value = sym.st_size;
1718 }
1719 else
1720 {
1721 /* Leave it up to the processor backend. */
1722 }
1723
1724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
1725 if (name == (const char *) NULL)
1726 goto error_return;
1727
1728 if (add_symbol_hook)
1729 {
1730 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
1731 &value))
1732 goto error_return;
1733
1734 /* The hook function sets the name to NULL if this symbol
1735 should be skipped for some reason. */
1736 if (name == (const char *) NULL)
1737 continue;
1738 }
1739
1740 /* Sanity check that all possibilities were handled. */
1741 if (sec == (asection *) NULL)
1742 {
1743 bfd_set_error (bfd_error_bad_value);
1744 goto error_return;
1745 }
1746
1747 if (bfd_is_und_section (sec)
1748 || bfd_is_com_section (sec))
1749 definition = false;
1750 else
1751 definition = true;
1752
1753 size_change_ok = false;
1754 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
1755 old_alignment = 0;
1756 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1757 {
1758 Elf_Internal_Versym iver;
1759 unsigned int vernum = 0;
252b5132
RH
1760
1761 if (ever != NULL)
1762 {
1763 _bfd_elf_swap_versym_in (abfd, ever, &iver);
1764 vernum = iver.vs_vers & VERSYM_VERSION;
1765
1766 /* If this is a hidden symbol, or if it is not version
c44233aa
AM
1767 1, we append the version name to the symbol name.
1768 However, we do not modify a non-hidden absolute
1769 symbol, because it might be the version symbol
1770 itself. FIXME: What if it isn't? */
252b5132
RH
1771 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
1772 || (vernum > 1 && ! bfd_is_abs_section (sec)))
1773 {
1774 const char *verstr;
dc810e39
AM
1775 unsigned int namelen;
1776 bfd_size_type newlen;
252b5132
RH
1777 char *newname, *p;
1778
1779 if (sym.st_shndx != SHN_UNDEF)
1780 {
1781 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
1782 {
1783 (*_bfd_error_handler)
1784 (_("%s: %s: invalid version %u (max %d)"),
8f615d07 1785 bfd_archive_filename (abfd), name, vernum,
252b5132
RH
1786 elf_tdata (abfd)->dynverdef_hdr.sh_info);
1787 bfd_set_error (bfd_error_bad_value);
1788 goto error_return;
1789 }
1790 else if (vernum > 1)
1791 verstr =
1792 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1793 else
1794 verstr = "";
1795 }
1796 else
1797 {
1798 /* We cannot simply test for the number of
1799 entries in the VERNEED section since the
1800 numbers for the needed versions do not start
1801 at 0. */
1802 Elf_Internal_Verneed *t;
1803
1804 verstr = NULL;
1805 for (t = elf_tdata (abfd)->verref;
1806 t != NULL;
1807 t = t->vn_nextref)
1808 {
1809 Elf_Internal_Vernaux *a;
1810
1811 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1812 {
1813 if (a->vna_other == vernum)
1814 {
1815 verstr = a->vna_nodename;
1816 break;
1817 }
1818 }
1819 if (a != NULL)
1820 break;
1821 }
1822 if (verstr == NULL)
1823 {
1824 (*_bfd_error_handler)
1825 (_("%s: %s: invalid needed version %d"),
8f615d07 1826 bfd_archive_filename (abfd), name, vernum);
252b5132
RH
1827 bfd_set_error (bfd_error_bad_value);
1828 goto error_return;
1829 }
1830 }
1831
1832 namelen = strlen (name);
1833 newlen = namelen + strlen (verstr) + 2;
1834 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1835 ++newlen;
1836
1837 newname = (char *) bfd_alloc (abfd, newlen);
1838 if (newname == NULL)
1839 goto error_return;
1840 strcpy (newname, name);
1841 p = newname + namelen;
1842 *p++ = ELF_VER_CHR;
1287d1cc
ILT
1843 /* If this is a defined non-hidden version symbol,
1844 we add another @ to the name. This indicates the
1845 default version of the symbol. */
1846 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
1847 && sym.st_shndx != SHN_UNDEF)
252b5132
RH
1848 *p++ = ELF_VER_CHR;
1849 strcpy (p, verstr);
1850
1851 name = newname;
1852 }
1853 }
1854
1855 if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
1856 sym_hash, &override, &type_change_ok,
456981d7 1857 &size_change_ok, dt_needed))
252b5132
RH
1858 goto error_return;
1859
1860 if (override)
1861 definition = false;
1862
1863 h = *sym_hash;
1864 while (h->root.type == bfd_link_hash_indirect
1865 || h->root.type == bfd_link_hash_warning)
1866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1867
1868 /* Remember the old alignment if this is a common symbol, so
c44233aa
AM
1869 that we don't reduce the alignment later on. We can't
1870 check later, because _bfd_generic_link_add_one_symbol
1871 will set a default for the alignment which we want to
1872 override. */
252b5132
RH
1873 if (h->root.type == bfd_link_hash_common)
1874 old_alignment = h->root.u.c.p->alignment_power;
1875
1876 if (elf_tdata (abfd)->verdef != NULL
1877 && ! override
1878 && vernum > 1
1879 && definition)
1880 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
1881 }
1882
1883 if (! (_bfd_generic_link_add_one_symbol
1884 (info, abfd, name, flags, sec, value, (const char *) NULL,
1885 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
1886 goto error_return;
1887
1888 h = *sym_hash;
1889 while (h->root.type == bfd_link_hash_indirect
1890 || h->root.type == bfd_link_hash_warning)
1891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1892 *sym_hash = h;
1893
1894 new_weakdef = false;
1895 if (dynamic
1896 && definition
1897 && (flags & BSF_WEAK) != 0
1898 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
1899 && info->hash->creator->flavour == bfd_target_elf_flavour
1900 && h->weakdef == NULL)
1901 {
1902 /* Keep a list of all weak defined non function symbols from
1903 a dynamic object, using the weakdef field. Later in this
1904 function we will set the weakdef field to the correct
1905 value. We only put non-function symbols from dynamic
1906 objects on this list, because that happens to be the only
1907 time we need to know the normal symbol corresponding to a
1908 weak symbol, and the information is time consuming to
1909 figure out. If the weakdef field is not already NULL,
1910 then this symbol was already defined by some previous
1911 dynamic object, and we will be using that previous
1912 definition anyhow. */
1913
1914 h->weakdef = weaks;
1915 weaks = h;
1916 new_weakdef = true;
1917 }
1918
1919 /* Set the alignment of a common symbol. */
1920 if (sym.st_shndx == SHN_COMMON
1921 && h->root.type == bfd_link_hash_common)
1922 {
1923 unsigned int align;
1924
1925 align = bfd_log2 (sym.st_value);
724982f6
NC
1926 if (align > old_alignment
1927 /* Permit an alignment power of zero if an alignment of one
1928 is specified and no other alignments have been specified. */
1929 || (sym.st_value == 1 && old_alignment == 0))
252b5132
RH
1930 h->root.u.c.p->alignment_power = align;
1931 }
1932
1933 if (info->hash->creator->flavour == bfd_target_elf_flavour)
1934 {
1935 int old_flags;
1936 boolean dynsym;
1937 int new_flag;
1938
1939 /* Remember the symbol size and type. */
1940 if (sym.st_size != 0
1941 && (definition || h->size == 0))
1942 {
1943 if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
1944 (*_bfd_error_handler)
1945 (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
1946 name, (unsigned long) h->size, (unsigned long) sym.st_size,
8f615d07 1947 bfd_archive_filename (abfd));
252b5132
RH
1948
1949 h->size = sym.st_size;
1950 }
1951
1952 /* If this is a common symbol, then we always want H->SIZE
c44233aa
AM
1953 to be the size of the common symbol. The code just above
1954 won't fix the size if a common symbol becomes larger. We
1955 don't warn about a size change here, because that is
1956 covered by --warn-common. */
252b5132
RH
1957 if (h->root.type == bfd_link_hash_common)
1958 h->size = h->root.u.c.size;
1959
1960 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
1961 && (definition || h->type == STT_NOTYPE))
1962 {
1963 if (h->type != STT_NOTYPE
1964 && h->type != ELF_ST_TYPE (sym.st_info)
1965 && ! type_change_ok)
1966 (*_bfd_error_handler)
1967 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
1968 name, h->type, ELF_ST_TYPE (sym.st_info),
8f615d07 1969 bfd_archive_filename (abfd));
252b5132
RH
1970
1971 h->type = ELF_ST_TYPE (sym.st_info);
1972 }
1973
7a13edea
NC
1974 /* If st_other has a processor-specific meaning, specific code
1975 might be needed here. */
1976 if (sym.st_other != 0)
1977 {
1978 /* Combine visibilities, using the most constraining one. */
1979 unsigned char hvis = ELF_ST_VISIBILITY (h->other);
1980 unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
3e932841 1981
7a13edea 1982 if (symvis && (hvis > symvis || hvis == 0))
38048eb9 1983 h->other = sym.st_other;
3e932841 1984
7a13edea 1985 /* If neither has visibility, use the st_other of the
c44233aa
AM
1986 definition. This is an arbitrary choice, since the
1987 other bits have no general meaning. */
7a13edea
NC
1988 if (!symvis && !hvis
1989 && (definition || h->other == 0))
1990 h->other = sym.st_other;
1991 }
252b5132
RH
1992
1993 /* Set a flag in the hash table entry indicating the type of
1994 reference or definition we just found. Keep a count of
1995 the number of dynamic symbols we find. A dynamic symbol
1996 is one which is referenced or defined by both a regular
1997 object and a shared object. */
1998 old_flags = h->elf_link_hash_flags;
1999 dynsym = false;
2000 if (! dynamic)
2001 {
2002 if (! definition)
2003 {
2004 new_flag = ELF_LINK_HASH_REF_REGULAR;
2005 if (bind != STB_WEAK)
2006 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
2007 }
2008 else
2009 new_flag = ELF_LINK_HASH_DEF_REGULAR;
2010 if (info->shared
2011 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2012 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
2013 dynsym = true;
2014 }
2015 else
2016 {
2017 if (! definition)
2018 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
2019 else
2020 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
2021 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
2022 | ELF_LINK_HASH_REF_REGULAR)) != 0
2023 || (h->weakdef != NULL
2024 && ! new_weakdef
2025 && h->weakdef->dynindx != -1))
2026 dynsym = true;
2027 }
2028
2029 h->elf_link_hash_flags |= new_flag;
2030
215007a6
L
2031 /* Check to see if we need to add an indirect symbol for
2032 the default name. */
051b8577 2033 if (definition || h->root.type == bfd_link_hash_common)
215007a6
L
2034 if (! elf_add_default_symbol (abfd, info, h, name, &sym,
2035 &sec, &value, &dynsym,
2036 override, dt_needed))
2037 goto error_return;
252b5132
RH
2038
2039 if (dynsym && h->dynindx == -1)
2040 {
2041 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2042 goto error_return;
2043 if (h->weakdef != NULL
2044 && ! new_weakdef
2045 && h->weakdef->dynindx == -1)
2046 {
a7b97311 2047 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
252b5132
RH
2048 goto error_return;
2049 }
2050 }
38048eb9 2051 else if (dynsym && h->dynindx != -1)
0444bdd4
L
2052 /* If the symbol already has a dynamic index, but
2053 visibility says it should not be visible, turn it into
2054 a local symbol. */
2055 switch (ELF_ST_VISIBILITY (h->other))
2056 {
2057 case STV_INTERNAL:
3e932841 2058 case STV_HIDDEN:
e5094212 2059 (*bed->elf_backend_hide_symbol) (info, h, true);
0444bdd4
L
2060 break;
2061 }
74816898
L
2062
2063 if (dt_needed && definition
2064 && (h->elf_link_hash_flags
2065 & ELF_LINK_HASH_REF_REGULAR) != 0)
2066 {
2067 bfd_size_type oldsize;
2068 bfd_size_type strindex;
2069
8ea2e4bd
NC
2070 if (! is_elf_hash_table (info))
2071 goto error_return;
2072
74816898 2073 /* The symbol from a DT_NEEDED object is referenced from
c44233aa 2074 the regular object to create a dynamic executable. We
3e932841 2075 have to make sure there is a DT_NEEDED entry for it. */
74816898
L
2076
2077 dt_needed = false;
2b0f7ef9
JJ
2078 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2079 strindex = _bfd_elf_strtab_add (hash_table->dynstr,
2080 elf_dt_soname (abfd), false);
74816898
L
2081 if (strindex == (bfd_size_type) -1)
2082 goto error_return;
2083
2b0f7ef9 2084 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
74816898
L
2085 {
2086 asection *sdyn;
2087 Elf_External_Dyn *dyncon, *dynconend;
2088
8ea2e4bd 2089 sdyn = bfd_get_section_by_name (hash_table->dynobj,
74816898
L
2090 ".dynamic");
2091 BFD_ASSERT (sdyn != NULL);
2092
2093 dyncon = (Elf_External_Dyn *) sdyn->contents;
2094 dynconend = (Elf_External_Dyn *) (sdyn->contents +
2095 sdyn->_raw_size);
2096 for (; dyncon < dynconend; dyncon++)
2097 {
2098 Elf_Internal_Dyn dyn;
2099
8ea2e4bd 2100 elf_swap_dyn_in (hash_table->dynobj,
74816898
L
2101 dyncon, &dyn);
2102 BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
2103 dyn.d_un.d_val != strindex);
2104 }
2105 }
2106
dc810e39 2107 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
74816898
L
2108 goto error_return;
2109 }
252b5132
RH
2110 }
2111 }
2112
2113 /* Now set the weakdefs field correctly for all the weak defined
2114 symbols we found. The only way to do this is to search all the
2115 symbols. Since we only need the information for non functions in
2116 dynamic objects, that's the only time we actually put anything on
2117 the list WEAKS. We need this information so that if a regular
2118 object refers to a symbol defined weakly in a dynamic object, the
2119 real symbol in the dynamic object is also put in the dynamic
2120 symbols; we also must arrange for both symbols to point to the
2121 same memory location. We could handle the general case of symbol
2122 aliasing, but a general symbol alias can only be generated in
2123 assembler code, handling it correctly would be very time
2124 consuming, and other ELF linkers don't handle general aliasing
2125 either. */
2126 while (weaks != NULL)
2127 {
2128 struct elf_link_hash_entry *hlook;
2129 asection *slook;
2130 bfd_vma vlook;
2131 struct elf_link_hash_entry **hpp;
2132 struct elf_link_hash_entry **hppend;
2133
2134 hlook = weaks;
2135 weaks = hlook->weakdef;
2136 hlook->weakdef = NULL;
2137
2138 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
2139 || hlook->root.type == bfd_link_hash_defweak
2140 || hlook->root.type == bfd_link_hash_common
2141 || hlook->root.type == bfd_link_hash_indirect);
2142 slook = hlook->root.u.def.section;
2143 vlook = hlook->root.u.def.value;
2144
2145 hpp = elf_sym_hashes (abfd);
2146 hppend = hpp + extsymcount;
2147 for (; hpp < hppend; hpp++)
2148 {
2149 struct elf_link_hash_entry *h;
2150
2151 h = *hpp;
2152 if (h != NULL && h != hlook
2153 && h->root.type == bfd_link_hash_defined
2154 && h->root.u.def.section == slook
2155 && h->root.u.def.value == vlook)
2156 {
2157 hlook->weakdef = h;
2158
2159 /* If the weak definition is in the list of dynamic
2160 symbols, make sure the real definition is put there
2161 as well. */
2162 if (hlook->dynindx != -1
2163 && h->dynindx == -1)
2164 {
2165 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2166 goto error_return;
2167 }
2168
2169 /* If the real definition is in the list of dynamic
c44233aa
AM
2170 symbols, make sure the weak definition is put there
2171 as well. If we don't do this, then the dynamic
2172 loader might not merge the entries for the real
2173 definition and the weak definition. */
252b5132
RH
2174 if (h->dynindx != -1
2175 && hlook->dynindx == -1)
2176 {
2177 if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
2178 goto error_return;
2179 }
2180
2181 break;
2182 }
2183 }
2184 }
2185
2186 if (buf != NULL)
2187 {
2188 free (buf);
2189 buf = NULL;
2190 }
2191
2192 if (extversym != NULL)
2193 {
2194 free (extversym);
2195 extversym = NULL;
2196 }
2197
2198 /* If this object is the same format as the output object, and it is
2199 not a shared library, then let the backend look through the
2200 relocs.
2201
2202 This is required to build global offset table entries and to
2203 arrange for dynamic relocs. It is not required for the
2204 particular common case of linking non PIC code, even when linking
2205 against shared libraries, but unfortunately there is no way of
2206 knowing whether an object file has been compiled PIC or not.
2207 Looking through the relocs is not particularly time consuming.
2208 The problem is that we must either (1) keep the relocs in memory,
2209 which causes the linker to require additional runtime memory or
2210 (2) read the relocs twice from the input file, which wastes time.
2211 This would be a good case for using mmap.
2212
2213 I have no idea how to handle linking PIC code into a file of a
2214 different format. It probably can't be done. */
2215 check_relocs = get_elf_backend_data (abfd)->check_relocs;
2216 if (! dynamic
2217 && abfd->xvec == info->hash->creator
2218 && check_relocs != NULL)
2219 {
2220 asection *o;
2221
2222 for (o = abfd->sections; o != NULL; o = o->next)
2223 {
2224 Elf_Internal_Rela *internal_relocs;
2225 boolean ok;
2226
2227 if ((o->flags & SEC_RELOC) == 0
2228 || o->reloc_count == 0
2229 || ((info->strip == strip_all || info->strip == strip_debugger)
2230 && (o->flags & SEC_DEBUGGING) != 0)
2231 || bfd_is_abs_section (o->output_section))
2232 continue;
2233
2234 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
2235 (abfd, o, (PTR) NULL,
2236 (Elf_Internal_Rela *) NULL,
2237 info->keep_memory));
2238 if (internal_relocs == NULL)
2239 goto error_return;
2240
2241 ok = (*check_relocs) (abfd, info, o, internal_relocs);
2242
2243 if (! info->keep_memory)
2244 free (internal_relocs);
2245
2246 if (! ok)
2247 goto error_return;
2248 }
2249 }
2250
2251 /* If this is a non-traditional, non-relocateable link, try to
2252 optimize the handling of the .stab/.stabstr sections. */
2253 if (! dynamic
2254 && ! info->relocateable
2255 && ! info->traditional_format
2256 && info->hash->creator->flavour == bfd_target_elf_flavour
8ea2e4bd 2257 && is_elf_hash_table (info)
252b5132
RH
2258 && (info->strip != strip_all && info->strip != strip_debugger))
2259 {
2260 asection *stab, *stabstr;
2261
2262 stab = bfd_get_section_by_name (abfd, ".stab");
65765700 2263 if (stab != NULL && !(stab->flags & SEC_MERGE))
252b5132
RH
2264 {
2265 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
2266
2267 if (stabstr != NULL)
2268 {
2269 struct bfd_elf_section_data *secdata;
2270
2271 secdata = elf_section_data (stab);
2272 if (! _bfd_link_section_stabs (abfd,
8ea2e4bd 2273 & hash_table->stab_info,
252b5132 2274 stab, stabstr,
65765700 2275 &secdata->sec_info))
252b5132 2276 goto error_return;
65765700
JJ
2277 if (secdata->sec_info)
2278 secdata->sec_info_type = ELF_INFO_TYPE_STABS;
252b5132
RH
2279 }
2280 }
2281 }
2282
8ea2e4bd
NC
2283 if (! info->relocateable && ! dynamic
2284 && is_elf_hash_table (info))
f5fa8ca2
JJ
2285 {
2286 asection *s;
2287
2288 for (s = abfd->sections; s != NULL; s = s->next)
65765700
JJ
2289 if (s->flags & SEC_MERGE)
2290 {
2291 struct bfd_elf_section_data *secdata;
2292
2293 secdata = elf_section_data (s);
2294 if (! _bfd_merge_section (abfd,
2295 & hash_table->merge_info,
2296 s, &secdata->sec_info))
2297 goto error_return;
2298 else if (secdata->sec_info)
2299 secdata->sec_info_type = ELF_INFO_TYPE_MERGE;
2300 }
f5fa8ca2
JJ
2301 }
2302
252b5132
RH
2303 return true;
2304
2305 error_return:
2306 if (buf != NULL)
2307 free (buf);
2308 if (dynbuf != NULL)
2309 free (dynbuf);
252b5132
RH
2310 if (extversym != NULL)
2311 free (extversym);
2312 return false;
2313}
2314
2315/* Create some sections which will be filled in with dynamic linking
2316 information. ABFD is an input file which requires dynamic sections
2317 to be created. The dynamic sections take up virtual memory space
2318 when the final executable is run, so we need to create them before
2319 addresses are assigned to the output sections. We work out the
2320 actual contents and size of these sections later. */
2321
2322boolean
2323elf_link_create_dynamic_sections (abfd, info)
2324 bfd *abfd;
2325 struct bfd_link_info *info;
2326{
2327 flagword flags;
2328 register asection *s;
2329 struct elf_link_hash_entry *h;
2330 struct elf_backend_data *bed;
2331
8ea2e4bd
NC
2332 if (! is_elf_hash_table (info))
2333 return false;
2334
252b5132
RH
2335 if (elf_hash_table (info)->dynamic_sections_created)
2336 return true;
2337
2338 /* Make sure that all dynamic sections use the same input BFD. */
2339 if (elf_hash_table (info)->dynobj == NULL)
2340 elf_hash_table (info)->dynobj = abfd;
2341 else
2342 abfd = elf_hash_table (info)->dynobj;
2343
2344 /* Note that we set the SEC_IN_MEMORY flag for all of these
2345 sections. */
2346 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2347 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2348
2349 /* A dynamically linked executable has a .interp section, but a
2350 shared library does not. */
2351 if (! info->shared)
2352 {
2353 s = bfd_make_section (abfd, ".interp");
2354 if (s == NULL
2355 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2356 return false;
2357 }
2358
65765700
JJ
2359 if (! info->traditional_format
2360 && info->hash->creator->flavour == bfd_target_elf_flavour)
2361 {
2362 s = bfd_make_section (abfd, ".eh_frame_hdr");
2363 if (s == NULL
2364 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2365 || ! bfd_set_section_alignment (abfd, s, 2))
2366 return false;
2367 }
2368
252b5132
RH
2369 /* Create sections to hold version informations. These are removed
2370 if they are not needed. */
2371 s = bfd_make_section (abfd, ".gnu.version_d");
2372 if (s == NULL
2373 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2374 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2375 return false;
2376
2377 s = bfd_make_section (abfd, ".gnu.version");
2378 if (s == NULL
2379 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2380 || ! bfd_set_section_alignment (abfd, s, 1))
2381 return false;
2382
2383 s = bfd_make_section (abfd, ".gnu.version_r");
2384 if (s == NULL
2385 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2386 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2387 return false;
2388
2389 s = bfd_make_section (abfd, ".dynsym");
2390 if (s == NULL
2391 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2392 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2393 return false;
2394
2395 s = bfd_make_section (abfd, ".dynstr");
2396 if (s == NULL
2397 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
2398 return false;
2399
2400 /* Create a strtab to hold the dynamic symbol names. */
2401 if (elf_hash_table (info)->dynstr == NULL)
2402 {
2b0f7ef9 2403 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
252b5132
RH
2404 if (elf_hash_table (info)->dynstr == NULL)
2405 return false;
2406 }
2407
2408 s = bfd_make_section (abfd, ".dynamic");
2409 if (s == NULL
2410 || ! bfd_set_section_flags (abfd, s, flags)
2411 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2412 return false;
2413
2414 /* The special symbol _DYNAMIC is always set to the start of the
2415 .dynamic section. This call occurs before we have processed the
2416 symbols for any dynamic object, so we don't have to worry about
2417 overriding a dynamic definition. We could set _DYNAMIC in a
2418 linker script, but we only want to define it if we are, in fact,
2419 creating a .dynamic section. We don't want to define it if there
2420 is no .dynamic section, since on some ELF platforms the start up
2421 code examines it to decide how to initialize the process. */
2422 h = NULL;
2423 if (! (_bfd_generic_link_add_one_symbol
2424 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
2425 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
2426 (struct bfd_link_hash_entry **) &h)))
2427 return false;
2428 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2429 h->type = STT_OBJECT;
2430
2431 if (info->shared
2432 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2433 return false;
2434
c7ac6ff8
MM
2435 bed = get_elf_backend_data (abfd);
2436
252b5132
RH
2437 s = bfd_make_section (abfd, ".hash");
2438 if (s == NULL
2439 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2440 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
2441 return false;
c7ac6ff8 2442 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
2443
2444 /* Let the backend create the rest of the sections. This lets the
2445 backend set the right flags. The backend will normally create
2446 the .got and .plt sections. */
252b5132
RH
2447 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
2448 return false;
2449
2450 elf_hash_table (info)->dynamic_sections_created = true;
2451
2452 return true;
2453}
2454
2455/* Add an entry to the .dynamic table. */
2456
2457boolean
2458elf_add_dynamic_entry (info, tag, val)
2459 struct bfd_link_info *info;
2460 bfd_vma tag;
2461 bfd_vma val;
2462{
2463 Elf_Internal_Dyn dyn;
2464 bfd *dynobj;
2465 asection *s;
dc810e39 2466 bfd_size_type newsize;
252b5132
RH
2467 bfd_byte *newcontents;
2468
8ea2e4bd
NC
2469 if (! is_elf_hash_table (info))
2470 return false;
2471
252b5132
RH
2472 dynobj = elf_hash_table (info)->dynobj;
2473
2474 s = bfd_get_section_by_name (dynobj, ".dynamic");
2475 BFD_ASSERT (s != NULL);
2476
2477 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
2478 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
2479 if (newcontents == NULL)
2480 return false;
2481
2482 dyn.d_tag = tag;
2483 dyn.d_un.d_val = val;
2484 elf_swap_dyn_out (dynobj, &dyn,
2485 (Elf_External_Dyn *) (newcontents + s->_raw_size));
2486
2487 s->_raw_size = newsize;
2488 s->contents = newcontents;
2489
2490 return true;
2491}
30b30c21
RH
2492
2493/* Record a new local dynamic symbol. */
2494
2495boolean
2496elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
2497 struct bfd_link_info *info;
2498 bfd *input_bfd;
2499 long input_indx;
2500{
2501 struct elf_link_local_dynamic_entry *entry;
2502 struct elf_link_hash_table *eht;
2b0f7ef9 2503 struct elf_strtab_hash *dynstr;
30b30c21 2504 Elf_External_Sym esym;
9ad5cbcf
AM
2505 Elf_External_Sym_Shndx eshndx;
2506 Elf_External_Sym_Shndx *shndx;
30b30c21
RH
2507 unsigned long dynstr_index;
2508 char *name;
dc810e39
AM
2509 file_ptr pos;
2510 bfd_size_type amt;
30b30c21 2511
8ea2e4bd
NC
2512 if (! is_elf_hash_table (info))
2513 return false;
2514
30b30c21
RH
2515 /* See if the entry exists already. */
2516 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
2517 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
2518 return true;
2519
2520 entry = (struct elf_link_local_dynamic_entry *)
dc810e39 2521 bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
30b30c21
RH
2522 if (entry == NULL)
2523 return false;
2524
2525 /* Go find the symbol, so that we can find it's name. */
dc810e39
AM
2526 amt = sizeof (Elf_External_Sym);
2527 pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
2528 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
9ad5cbcf 2529 || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
30b30c21 2530 return false;
9ad5cbcf
AM
2531 shndx = NULL;
2532 if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
2533 {
2534 amt = sizeof (Elf_External_Sym_Shndx);
2535 pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
2536 pos += input_indx * amt;
2537 shndx = &eshndx;
2538 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
2539 || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
2540 return false;
2541 }
2542 elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
30b30c21
RH
2543
2544 name = (bfd_elf_string_from_elf_section
2545 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
2546 entry->isym.st_name));
2547
2548 dynstr = elf_hash_table (info)->dynstr;
2549 if (dynstr == NULL)
2550 {
2551 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 2552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
30b30c21
RH
2553 if (dynstr == NULL)
2554 return false;
2555 }
2556
2b0f7ef9 2557 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
30b30c21
RH
2558 if (dynstr_index == (unsigned long) -1)
2559 return false;
2560 entry->isym.st_name = dynstr_index;
2561
2562 eht = elf_hash_table (info);
2563
2564 entry->next = eht->dynlocal;
2565 eht->dynlocal = entry;
2566 entry->input_bfd = input_bfd;
2567 entry->input_indx = input_indx;
2568 eht->dynsymcount++;
2569
587ff49e
RH
2570 /* Whatever binding the symbol had before, it's now local. */
2571 entry->isym.st_info
2572 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
2573
30b30c21
RH
2574 /* The dynindx will be set at the end of size_dynamic_sections. */
2575
2576 return true;
2577}
252b5132 2578\f
6b5bd373
MM
2579/* Read and swap the relocs from the section indicated by SHDR. This
2580 may be either a REL or a RELA section. The relocations are
2581 translated into RELA relocations and stored in INTERNAL_RELOCS,
2582 which should have already been allocated to contain enough space.
2583 The EXTERNAL_RELOCS are a buffer where the external form of the
2584 relocations should be stored.
2585
2586 Returns false if something goes wrong. */
2587
2588static boolean
2589elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2590 internal_relocs)
2591 bfd *abfd;
2592 Elf_Internal_Shdr *shdr;
2593 PTR external_relocs;
2594 Elf_Internal_Rela *internal_relocs;
2595{
c7ac6ff8 2596 struct elf_backend_data *bed;
dc810e39 2597 bfd_size_type amt;
c7ac6ff8 2598
6b5bd373
MM
2599 /* If there aren't any relocations, that's OK. */
2600 if (!shdr)
2601 return true;
2602
2603 /* Position ourselves at the start of the section. */
2604 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2605 return false;
2606
2607 /* Read the relocations. */
dc810e39 2608 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
6b5bd373
MM
2609 return false;
2610
c7ac6ff8
MM
2611 bed = get_elf_backend_data (abfd);
2612
6b5bd373
MM
2613 /* Convert the external relocations to the internal format. */
2614 if (shdr->sh_entsize == sizeof (Elf_External_Rel))
2615 {
2616 Elf_External_Rel *erel;
2617 Elf_External_Rel *erelend;
2618 Elf_Internal_Rela *irela;
c7ac6ff8 2619 Elf_Internal_Rel *irel;
6b5bd373
MM
2620
2621 erel = (Elf_External_Rel *) external_relocs;
d9bc7a44 2622 erelend = erel + NUM_SHDR_ENTRIES (shdr);
6b5bd373 2623 irela = internal_relocs;
dc810e39
AM
2624 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
2625 irel = bfd_alloc (abfd, amt);
c7ac6ff8 2626 for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
6b5bd373 2627 {
4e8a9624 2628 unsigned int i;
c7ac6ff8
MM
2629
2630 if (bed->s->swap_reloc_in)
2631 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
2632 else
2633 elf_swap_reloc_in (abfd, erel, irel);
6b5bd373 2634
c7ac6ff8
MM
2635 for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
2636 {
2637 irela[i].r_offset = irel[i].r_offset;
2638 irela[i].r_info = irel[i].r_info;
2639 irela[i].r_addend = 0;
2640 }
6b5bd373
MM
2641 }
2642 }
2643 else
2644 {
2645 Elf_External_Rela *erela;
2646 Elf_External_Rela *erelaend;
2647 Elf_Internal_Rela *irela;
2648
2649 BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
2650
2651 erela = (Elf_External_Rela *) external_relocs;
d9bc7a44 2652 erelaend = erela + NUM_SHDR_ENTRIES (shdr);
6b5bd373 2653 irela = internal_relocs;
c7ac6ff8
MM
2654 for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
2655 {
2656 if (bed->s->swap_reloca_in)
2657 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
2658 else
2659 elf_swap_reloca_in (abfd, erela, irela);
2660 }
6b5bd373
MM
2661 }
2662
2663 return true;
2664}
2665
23bc299b
MM
2666/* Read and swap the relocs for a section O. They may have been
2667 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2668 not NULL, they are used as buffers to read into. They are known to
2669 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2670 the return value is allocated using either malloc or bfd_alloc,
2671 according to the KEEP_MEMORY argument. If O has two relocation
2672 sections (both REL and RELA relocations), then the REL_HDR
2673 relocations will appear first in INTERNAL_RELOCS, followed by the
2674 REL_HDR2 relocations. */
252b5132
RH
2675
2676Elf_Internal_Rela *
2677NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
2678 keep_memory)
2679 bfd *abfd;
2680 asection *o;
2681 PTR external_relocs;
2682 Elf_Internal_Rela *internal_relocs;
2683 boolean keep_memory;
2684{
2685 Elf_Internal_Shdr *rel_hdr;
2686 PTR alloc1 = NULL;
2687 Elf_Internal_Rela *alloc2 = NULL;
c7ac6ff8 2688 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
2689
2690 if (elf_section_data (o)->relocs != NULL)
2691 return elf_section_data (o)->relocs;
2692
2693 if (o->reloc_count == 0)
2694 return NULL;
2695
2696 rel_hdr = &elf_section_data (o)->rel_hdr;
2697
2698 if (internal_relocs == NULL)
2699 {
dc810e39 2700 bfd_size_type size;
252b5132 2701
dc810e39
AM
2702 size = o->reloc_count;
2703 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
252b5132
RH
2704 if (keep_memory)
2705 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2706 else
2707 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2708 if (internal_relocs == NULL)
2709 goto error_return;
2710 }
2711
2712 if (external_relocs == NULL)
2713 {
dc810e39 2714 bfd_size_type size = rel_hdr->sh_size;
6b5bd373
MM
2715
2716 if (elf_section_data (o)->rel_hdr2)
dc810e39 2717 size += elf_section_data (o)->rel_hdr2->sh_size;
6b5bd373 2718 alloc1 = (PTR) bfd_malloc (size);
252b5132
RH
2719 if (alloc1 == NULL)
2720 goto error_return;
2721 external_relocs = alloc1;
2722 }
2723
6b5bd373
MM
2724 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2725 external_relocs,
2726 internal_relocs))
2727 goto error_return;
3e932841
KH
2728 if (!elf_link_read_relocs_from_section
2729 (abfd,
6b5bd373 2730 elf_section_data (o)->rel_hdr2,
2f5116e2 2731 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
d9bc7a44 2732 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
c7ac6ff8 2733 * bed->s->int_rels_per_ext_rel)))
252b5132 2734 goto error_return;
252b5132
RH
2735
2736 /* Cache the results for next time, if we can. */
2737 if (keep_memory)
2738 elf_section_data (o)->relocs = internal_relocs;
2739
2740 if (alloc1 != NULL)
2741 free (alloc1);
2742
2743 /* Don't free alloc2, since if it was allocated we are passing it
2744 back (under the name of internal_relocs). */
2745
2746 return internal_relocs;
2747
2748 error_return:
2749 if (alloc1 != NULL)
2750 free (alloc1);
2751 if (alloc2 != NULL)
2752 free (alloc2);
2753 return NULL;
2754}
2755\f
252b5132
RH
2756/* Record an assignment to a symbol made by a linker script. We need
2757 this in case some dynamic object refers to this symbol. */
2758
252b5132
RH
2759boolean
2760NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
7442e600 2761 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2762 struct bfd_link_info *info;
2763 const char *name;
2764 boolean provide;
2765{
2766 struct elf_link_hash_entry *h;
2767
2768 if (info->hash->creator->flavour != bfd_target_elf_flavour)
2769 return true;
2770
2771 h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
2772 if (h == NULL)
2773 return false;
2774
2775 if (h->root.type == bfd_link_hash_new)
a7b97311 2776 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
252b5132
RH
2777
2778 /* If this symbol is being provided by the linker script, and it is
2779 currently defined by a dynamic object, but not by a regular
2780 object, then mark it as undefined so that the generic linker will
2781 force the correct value. */
2782 if (provide
2783 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2784 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2785 h->root.type = bfd_link_hash_undefined;
2786
2787 /* If this symbol is not being provided by the linker script, and it is
2788 currently defined by a dynamic object, but not by a regular object,
2789 then clear out any version information because the symbol will not be
2790 associated with the dynamic object any more. */
2791 if (!provide
2792 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2793 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2794 h->verinfo.verdef = NULL;
2795
2796 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
994819d2 2797
252b5132
RH
2798 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
2799 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
2800 || info->shared)
2801 && h->dynindx == -1)
2802 {
2803 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2804 return false;
2805
2806 /* If this is a weak defined symbol, and we know a corresponding
2807 real symbol from the same dynamic object, make sure the real
2808 symbol is also made into a dynamic symbol. */
2809 if (h->weakdef != NULL
2810 && h->weakdef->dynindx == -1)
2811 {
2812 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
2813 return false;
2814 }
2815 }
2816
2817 return true;
2818}
2819\f
2820/* This structure is used to pass information to
2821 elf_link_assign_sym_version. */
2822
2823struct elf_assign_sym_version_info
2824{
2825 /* Output BFD. */
2826 bfd *output_bfd;
2827 /* General link information. */
2828 struct bfd_link_info *info;
2829 /* Version tree. */
2830 struct bfd_elf_version_tree *verdefs;
252b5132
RH
2831 /* Whether we had a failure. */
2832 boolean failed;
2833};
2834
2835/* This structure is used to pass information to
2836 elf_link_find_version_dependencies. */
2837
2838struct elf_find_verdep_info
2839{
2840 /* Output BFD. */
2841 bfd *output_bfd;
2842 /* General link information. */
2843 struct bfd_link_info *info;
2844 /* The number of dependencies. */
2845 unsigned int vers;
2846 /* Whether we had a failure. */
2847 boolean failed;
2848};
2849
2850/* Array used to determine the number of hash table buckets to use
2851 based on the number of symbols there are. If there are fewer than
2852 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
2853 fewer than 37 we use 17 buckets, and so forth. We never use more
2854 than 32771 buckets. */
2855
2856static const size_t elf_buckets[] =
2857{
2858 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
2859 16411, 32771, 0
2860};
2861
2862/* Compute bucket count for hashing table. We do not use a static set
2863 of possible tables sizes anymore. Instead we determine for all
2864 possible reasonable sizes of the table the outcome (i.e., the
2865 number of collisions etc) and choose the best solution. The
2866 weighting functions are not too simple to allow the table to grow
2867 without bounds. Instead one of the weighting factors is the size.
2868 Therefore the result is always a good payoff between few collisions
2869 (= short chain lengths) and table size. */
2870static size_t
2871compute_bucket_count (info)
2872 struct bfd_link_info *info;
2873{
2874 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
7442e600 2875 size_t best_size = 0;
252b5132
RH
2876 unsigned long int *hashcodes;
2877 unsigned long int *hashcodesp;
2878 unsigned long int i;
dc810e39 2879 bfd_size_type amt;
252b5132
RH
2880
2881 /* Compute the hash values for all exported symbols. At the same
2882 time store the values in an array so that we could use them for
2883 optimizations. */
dc810e39
AM
2884 amt = dynsymcount;
2885 amt *= sizeof (unsigned long int);
2886 hashcodes = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2887 if (hashcodes == NULL)
2888 return 0;
2889 hashcodesp = hashcodes;
2890
2891 /* Put all hash values in HASHCODES. */
2892 elf_link_hash_traverse (elf_hash_table (info),
2893 elf_collect_hash_codes, &hashcodesp);
2894
2895/* We have a problem here. The following code to optimize the table
2896 size requires an integer type with more the 32 bits. If
2897 BFD_HOST_U_64_BIT is set we know about such a type. */
2898#ifdef BFD_HOST_U_64_BIT
2899 if (info->optimize == true)
2900 {
2901 unsigned long int nsyms = hashcodesp - hashcodes;
2902 size_t minsize;
2903 size_t maxsize;
2904 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
2905 unsigned long int *counts ;
2906
2907 /* Possible optimization parameters: if we have NSYMS symbols we say
2908 that the hashing table must at least have NSYMS/4 and at most
2909 2*NSYMS buckets. */
2910 minsize = nsyms / 4;
2911 if (minsize == 0)
2912 minsize = 1;
2913 best_size = maxsize = nsyms * 2;
2914
2915 /* Create array where we count the collisions in. We must use bfd_malloc
2916 since the size could be large. */
dc810e39
AM
2917 amt = maxsize;
2918 amt *= sizeof (unsigned long int);
2919 counts = (unsigned long int *) bfd_malloc (amt);
252b5132
RH
2920 if (counts == NULL)
2921 {
2922 free (hashcodes);
2923 return 0;
2924 }
2925
2926 /* Compute the "optimal" size for the hash table. The criteria is a
2927 minimal chain length. The minor criteria is (of course) the size
2928 of the table. */
2929 for (i = minsize; i < maxsize; ++i)
2930 {
2931 /* Walk through the array of hashcodes and count the collisions. */
2932 BFD_HOST_U_64_BIT max;
2933 unsigned long int j;
2934 unsigned long int fact;
2935
2936 memset (counts, '\0', i * sizeof (unsigned long int));
2937
2938 /* Determine how often each hash bucket is used. */
2939 for (j = 0; j < nsyms; ++j)
2940 ++counts[hashcodes[j] % i];
2941
2942 /* For the weight function we need some information about the
2943 pagesize on the target. This is information need not be 100%
2944 accurate. Since this information is not available (so far) we
2945 define it here to a reasonable default value. If it is crucial
2946 to have a better value some day simply define this value. */
2947# ifndef BFD_TARGET_PAGESIZE
2948# define BFD_TARGET_PAGESIZE (4096)
2949# endif
2950
2951 /* We in any case need 2 + NSYMS entries for the size values and
2952 the chains. */
2953 max = (2 + nsyms) * (ARCH_SIZE / 8);
2954
2955# if 1
2956 /* Variant 1: optimize for short chains. We add the squares
2957 of all the chain lengths (which favous many small chain
2958 over a few long chains). */
2959 for (j = 0; j < i; ++j)
2960 max += counts[j] * counts[j];
2961
2962 /* This adds penalties for the overall size of the table. */
2963 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2964 max *= fact * fact;
2965# else
2966 /* Variant 2: Optimize a lot more for small table. Here we
2967 also add squares of the size but we also add penalties for
2968 empty slots (the +1 term). */
2969 for (j = 0; j < i; ++j)
2970 max += (1 + counts[j]) * (1 + counts[j]);
2971
2972 /* The overall size of the table is considered, but not as
2973 strong as in variant 1, where it is squared. */
2974 fact = i / (BFD_TARGET_PAGESIZE / (ARCH_SIZE / 8)) + 1;
2975 max *= fact;
2976# endif
2977
2978 /* Compare with current best results. */
2979 if (max < best_chlen)
2980 {
2981 best_chlen = max;
2982 best_size = i;
2983 }
2984 }
2985
2986 free (counts);
2987 }
2988 else
2989#endif /* defined (BFD_HOST_U_64_BIT) */
2990 {
2991 /* This is the fallback solution if no 64bit type is available or if we
2992 are not supposed to spend much time on optimizations. We select the
2993 bucket count using a fixed set of numbers. */
2994 for (i = 0; elf_buckets[i] != 0; i++)
2995 {
2996 best_size = elf_buckets[i];
2997 if (dynsymcount < elf_buckets[i + 1])
2998 break;
2999 }
3000 }
3001
3002 /* Free the arrays we needed. */
3003 free (hashcodes);
3004
3005 return best_size;
3006}
3007
3008/* Set up the sizes and contents of the ELF dynamic sections. This is
3009 called by the ELF linker emulation before_allocation routine. We
3010 must set the sizes of the sections before the linker sets the
3011 addresses of the various sections. */
3012
3013boolean
3014NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
99293407 3015 filter_shlib,
252b5132
RH
3016 auxiliary_filters, info, sinterpptr,
3017 verdefs)
3018 bfd *output_bfd;
3019 const char *soname;
3020 const char *rpath;
252b5132
RH
3021 const char *filter_shlib;
3022 const char * const *auxiliary_filters;
3023 struct bfd_link_info *info;
3024 asection **sinterpptr;
3025 struct bfd_elf_version_tree *verdefs;
3026{
3027 bfd_size_type soname_indx;
25e27870 3028 bfd *dynobj;
252b5132 3029 struct elf_backend_data *bed;
252b5132
RH
3030 struct elf_assign_sym_version_info asvinfo;
3031
3032 *sinterpptr = NULL;
3033
3034 soname_indx = (bfd_size_type) -1;
3035
3036 if (info->hash->creator->flavour != bfd_target_elf_flavour)
3037 return true;
3038
8ea2e4bd
NC
3039 if (! is_elf_hash_table (info))
3040 return false;
3041
51b64d56
AM
3042 /* Any syms created from now on start with -1 in
3043 got.refcount/offset and plt.refcount/offset. */
3044 elf_hash_table (info)->init_refcount = -1;
3045
252b5132
RH
3046 /* The backend may have to create some sections regardless of whether
3047 we're dynamic or not. */
3048 bed = get_elf_backend_data (output_bfd);
3049 if (bed->elf_backend_always_size_sections
3050 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
3051 return false;
3052
3053 dynobj = elf_hash_table (info)->dynobj;
3054
3055 /* If there were no dynamic objects in the link, there is nothing to
3056 do here. */
3057 if (dynobj == NULL)
3058 return true;
3059
68f69152
JJ
3060 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
3061 return false;
3062
252b5132
RH
3063 if (elf_hash_table (info)->dynamic_sections_created)
3064 {
3065 struct elf_info_failed eif;
3066 struct elf_link_hash_entry *h;
fc8c40a0 3067 asection *dynstr;
252b5132
RH
3068
3069 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
3070 BFD_ASSERT (*sinterpptr != NULL || info->shared);
3071
3072 if (soname != NULL)
3073 {
2b0f7ef9
JJ
3074 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3075 soname, true);
252b5132 3076 if (soname_indx == (bfd_size_type) -1
dc810e39
AM
3077 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
3078 soname_indx))
252b5132
RH
3079 return false;
3080 }
3081
3082 if (info->symbolic)
3083 {
dc810e39
AM
3084 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
3085 (bfd_vma) 0))
252b5132 3086 return false;
d6cf2879 3087 info->flags |= DF_SYMBOLIC;
252b5132
RH
3088 }
3089
3090 if (rpath != NULL)
3091 {
3092 bfd_size_type indx;
3093
2b0f7ef9
JJ
3094 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
3095 true);
3096 if (info->new_dtags)
3097 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
252b5132 3098 if (indx == (bfd_size_type) -1
dc810e39 3099 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
c25373b7 3100 || (info->new_dtags
dc810e39
AM
3101 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
3102 indx)))
252b5132
RH
3103 return false;
3104 }
3105
3106 if (filter_shlib != NULL)
3107 {
3108 bfd_size_type indx;
3109
2b0f7ef9
JJ
3110 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3111 filter_shlib, true);
252b5132 3112 if (indx == (bfd_size_type) -1
dc810e39 3113 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
252b5132
RH
3114 return false;
3115 }
3116
3117 if (auxiliary_filters != NULL)
3118 {
3119 const char * const *p;
3120
3121 for (p = auxiliary_filters; *p != NULL; p++)
3122 {
3123 bfd_size_type indx;
3124
2b0f7ef9
JJ
3125 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3126 *p, true);
252b5132 3127 if (indx == (bfd_size_type) -1
dc810e39
AM
3128 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
3129 indx))
252b5132
RH
3130 return false;
3131 }
3132 }
3133
391a809a 3134 eif.info = info;
bc2b6df7 3135 eif.verdefs = verdefs;
391a809a
AM
3136 eif.failed = false;
3137
ea44b734 3138 /* If we are supposed to export all symbols into the dynamic symbol
c44233aa 3139 table (this is not the normal case), then do so. */
99293407 3140 if (info->export_dynamic)
ea44b734 3141 {
ea44b734 3142 elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
c44233aa 3143 (PTR) &eif);
ea44b734
RH
3144 if (eif.failed)
3145 return false;
3146 }
3147
252b5132
RH
3148 /* Attach all the symbols to their version information. */
3149 asvinfo.output_bfd = output_bfd;
3150 asvinfo.info = info;
3151 asvinfo.verdefs = verdefs;
252b5132
RH
3152 asvinfo.failed = false;
3153
3154 elf_link_hash_traverse (elf_hash_table (info),
3155 elf_link_assign_sym_version,
3156 (PTR) &asvinfo);
3157 if (asvinfo.failed)
3158 return false;
3159
3160 /* Find all symbols which were defined in a dynamic object and make
3161 the backend pick a reasonable value for them. */
252b5132
RH
3162 elf_link_hash_traverse (elf_hash_table (info),
3163 elf_adjust_dynamic_symbol,
3164 (PTR) &eif);
3165 if (eif.failed)
3166 return false;
3167
3168 /* Add some entries to the .dynamic section. We fill in some of the
3169 values later, in elf_bfd_final_link, but we must add the entries
3170 now so that we know the final size of the .dynamic section. */
f0c2e336
MM
3171
3172 /* If there are initialization and/or finalization functions to
3173 call then add the corresponding DT_INIT/DT_FINI entries. */
3174 h = (info->init_function
3e932841 3175 ? elf_link_hash_lookup (elf_hash_table (info),
f0c2e336
MM
3176 info->init_function, false,
3177 false, false)
3178 : NULL);
252b5132
RH
3179 if (h != NULL
3180 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3181 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3182 {
dc810e39 3183 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
252b5132
RH
3184 return false;
3185 }
f0c2e336 3186 h = (info->fini_function
3e932841 3187 ? elf_link_hash_lookup (elf_hash_table (info),
f0c2e336
MM
3188 info->fini_function, false,
3189 false, false)
3190 : NULL);
252b5132
RH
3191 if (h != NULL
3192 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
3193 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
3194 {
dc810e39 3195 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
252b5132
RH
3196 return false;
3197 }
f0c2e336 3198
fa7ea4d8
AM
3199 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
3200 {
3201 /* DT_PREINIT_ARRAY is not allowed in shared library. */
3202 if (info->shared)
3203 {
3204 bfd *sub;
3205 asection *o;
3206
3207 for (sub = info->input_bfds; sub != NULL;
3208 sub = sub->link_next)
3209 for (o = sub->sections; o != NULL; o = o->next)
3210 if (elf_section_data (o)->this_hdr.sh_type
3211 == SHT_PREINIT_ARRAY)
3212 {
3213 (*_bfd_error_handler)
3214 (_("%s: .preinit_array section is not allowed in DSO"),
3215 bfd_archive_filename (sub));
3216 break;
3217 }
60166579 3218
36b4f6e7 3219 bfd_set_error (bfd_error_nonrepresentable_section);
60166579 3220 return false;
fa7ea4d8
AM
3221 }
3222
3223 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
3224 (bfd_vma) 0)
3225 || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
3226 (bfd_vma) 0))
3227 return false;
3228 }
3229 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
3230 {
3231 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
3232 (bfd_vma) 0)
3233 || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
3234 (bfd_vma) 0))
3235 return false;
3236 }
3237 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
3238 {
3239 if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
3240 (bfd_vma) 0)
3241 || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
3242 (bfd_vma) 0))
3243 return false;
3244 }
30831527 3245
fc8c40a0
AM
3246 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
3247 /* If .dynstr is excluded from the link, we don't want any of
3248 these tags. Strictly, we should be checking each section
3249 individually; This quick check covers for the case where
3250 someone does a /DISCARD/ : { *(*) }. */
3251 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
3252 {
3253 bfd_size_type strsize;
3254
2b0f7ef9 3255 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
dc810e39
AM
3256 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
3257 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
3258 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
3259 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
3260 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
3261 (bfd_vma) sizeof (Elf_External_Sym)))
fc8c40a0
AM
3262 return false;
3263 }
252b5132
RH
3264 }
3265
3266 /* The backend must work out the sizes of all the other dynamic
3267 sections. */
252b5132
RH
3268 if (bed->elf_backend_size_dynamic_sections
3269 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
3270 return false;
3271
3272 if (elf_hash_table (info)->dynamic_sections_created)
3273 {
dc810e39 3274 bfd_size_type dynsymcount;
252b5132
RH
3275 asection *s;
3276 size_t bucketcount = 0;
c7ac6ff8 3277 size_t hash_entry_size;
db6751f2 3278 unsigned int dtagcount;
252b5132
RH
3279
3280 /* Set up the version definition section. */
3281 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3282 BFD_ASSERT (s != NULL);
3283
3284 /* We may have created additional version definitions if we are
c44233aa 3285 just linking a regular application. */
252b5132
RH
3286 verdefs = asvinfo.verdefs;
3287
6b9b879a
JJ
3288 /* Skip anonymous version tag. */
3289 if (verdefs != NULL && verdefs->vernum == 0)
3290 verdefs = verdefs->next;
3291
252b5132 3292 if (verdefs == NULL)
7f8d5fc9 3293 _bfd_strip_section_from_output (info, s);
252b5132
RH
3294 else
3295 {
3296 unsigned int cdefs;
3297 bfd_size_type size;
3298 struct bfd_elf_version_tree *t;
3299 bfd_byte *p;
3300 Elf_Internal_Verdef def;
3301 Elf_Internal_Verdaux defaux;
3302
252b5132
RH
3303 cdefs = 0;
3304 size = 0;
3305
3306 /* Make space for the base version. */
3307 size += sizeof (Elf_External_Verdef);
3308 size += sizeof (Elf_External_Verdaux);
3309 ++cdefs;
3310
3311 for (t = verdefs; t != NULL; t = t->next)
3312 {
3313 struct bfd_elf_version_deps *n;
3314
3315 size += sizeof (Elf_External_Verdef);
3316 size += sizeof (Elf_External_Verdaux);
3317 ++cdefs;
3318
3319 for (n = t->deps; n != NULL; n = n->next)
3320 size += sizeof (Elf_External_Verdaux);
3321 }
3322
3323 s->_raw_size = size;
3324 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3325 if (s->contents == NULL && s->_raw_size != 0)
3326 return false;
3327
3328 /* Fill in the version definition section. */
3329
3330 p = s->contents;
3331
3332 def.vd_version = VER_DEF_CURRENT;
3333 def.vd_flags = VER_FLG_BASE;
3334 def.vd_ndx = 1;
3335 def.vd_cnt = 1;
3336 def.vd_aux = sizeof (Elf_External_Verdef);
3337 def.vd_next = (sizeof (Elf_External_Verdef)
3338 + sizeof (Elf_External_Verdaux));
3339
3340 if (soname_indx != (bfd_size_type) -1)
3341 {
2b0f7ef9
JJ
3342 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3343 soname_indx);
3a99b017 3344 def.vd_hash = bfd_elf_hash (soname);
252b5132
RH
3345 defaux.vda_name = soname_indx;
3346 }
3347 else
3348 {
3349 const char *name;
3350 bfd_size_type indx;
3351
96fd004e 3352 name = basename (output_bfd->filename);
3a99b017 3353 def.vd_hash = bfd_elf_hash (name);
2b0f7ef9
JJ
3354 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3355 name, false);
252b5132
RH
3356 if (indx == (bfd_size_type) -1)
3357 return false;
3358 defaux.vda_name = indx;
3359 }
3360 defaux.vda_next = 0;
3361
3362 _bfd_elf_swap_verdef_out (output_bfd, &def,
a7b97311 3363 (Elf_External_Verdef *) p);
252b5132
RH
3364 p += sizeof (Elf_External_Verdef);
3365 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3366 (Elf_External_Verdaux *) p);
3367 p += sizeof (Elf_External_Verdaux);
3368
3369 for (t = verdefs; t != NULL; t = t->next)
3370 {
3371 unsigned int cdeps;
3372 struct bfd_elf_version_deps *n;
3373 struct elf_link_hash_entry *h;
3374
3375 cdeps = 0;
3376 for (n = t->deps; n != NULL; n = n->next)
3377 ++cdeps;
3378
3379 /* Add a symbol representing this version. */
3380 h = NULL;
3381 if (! (_bfd_generic_link_add_one_symbol
3382 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
3383 (bfd_vma) 0, (const char *) NULL, false,
3384 get_elf_backend_data (dynobj)->collect,
3385 (struct bfd_link_hash_entry **) &h)))
3386 return false;
3387 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
3388 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3389 h->type = STT_OBJECT;
3390 h->verinfo.vertree = t;
3391
3392 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3393 return false;
3394
3395 def.vd_version = VER_DEF_CURRENT;
3396 def.vd_flags = 0;
3397 if (t->globals == NULL && t->locals == NULL && ! t->used)
3398 def.vd_flags |= VER_FLG_WEAK;
3399 def.vd_ndx = t->vernum + 1;
3400 def.vd_cnt = cdeps + 1;
3a99b017 3401 def.vd_hash = bfd_elf_hash (t->name);
252b5132
RH
3402 def.vd_aux = sizeof (Elf_External_Verdef);
3403 if (t->next != NULL)
3404 def.vd_next = (sizeof (Elf_External_Verdef)
3405 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
3406 else
3407 def.vd_next = 0;
3408
3409 _bfd_elf_swap_verdef_out (output_bfd, &def,
3410 (Elf_External_Verdef *) p);
3411 p += sizeof (Elf_External_Verdef);
3412
3413 defaux.vda_name = h->dynstr_index;
2b0f7ef9
JJ
3414 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3415 h->dynstr_index);
252b5132
RH
3416 if (t->deps == NULL)
3417 defaux.vda_next = 0;
3418 else
3419 defaux.vda_next = sizeof (Elf_External_Verdaux);
3420 t->name_indx = defaux.vda_name;
3421
3422 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3423 (Elf_External_Verdaux *) p);
3424 p += sizeof (Elf_External_Verdaux);
3425
3426 for (n = t->deps; n != NULL; n = n->next)
3427 {
3428 if (n->version_needed == NULL)
3429 {
3430 /* This can happen if there was an error in the
3431 version script. */
3432 defaux.vda_name = 0;
3433 }
3434 else
2b0f7ef9
JJ
3435 {
3436 defaux.vda_name = n->version_needed->name_indx;
3437 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
3438 defaux.vda_name);
3439 }
252b5132
RH
3440 if (n->next == NULL)
3441 defaux.vda_next = 0;
3442 else
3443 defaux.vda_next = sizeof (Elf_External_Verdaux);
3444
3445 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
3446 (Elf_External_Verdaux *) p);
3447 p += sizeof (Elf_External_Verdaux);
3448 }
3449 }
3450
dc810e39
AM
3451 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
3452 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
3453 (bfd_vma) cdefs))
252b5132
RH
3454 return false;
3455
3456 elf_tdata (output_bfd)->cverdefs = cdefs;
3457 }
3458
c25373b7 3459 if (info->new_dtags && info->flags)
d6cf2879 3460 {
dc810e39 3461 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
d6cf2879
L
3462 return false;
3463 }
3464
4d538889 3465 if (info->flags_1)
d6cf2879
L
3466 {
3467 if (! info->shared)
3468 info->flags_1 &= ~ (DF_1_INITFIRST
3469 | DF_1_NODELETE
3470 | DF_1_NOOPEN);
dc810e39
AM
3471 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
3472 info->flags_1))
d6cf2879
L
3473 return false;
3474 }
3475
252b5132
RH
3476 /* Work out the size of the version reference section. */
3477
3478 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3479 BFD_ASSERT (s != NULL);
3480 {
3481 struct elf_find_verdep_info sinfo;
3482
3483 sinfo.output_bfd = output_bfd;
3484 sinfo.info = info;
3485 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
3486 if (sinfo.vers == 0)
3487 sinfo.vers = 1;
3488 sinfo.failed = false;
3489
3490 elf_link_hash_traverse (elf_hash_table (info),
3491 elf_link_find_version_dependencies,
3492 (PTR) &sinfo);
3493
3494 if (elf_tdata (output_bfd)->verref == NULL)
7f8d5fc9 3495 _bfd_strip_section_from_output (info, s);
252b5132
RH
3496 else
3497 {
3498 Elf_Internal_Verneed *t;
3499 unsigned int size;
3500 unsigned int crefs;
3501 bfd_byte *p;
3502
3503 /* Build the version definition section. */
3504 size = 0;
3505 crefs = 0;
3506 for (t = elf_tdata (output_bfd)->verref;
3507 t != NULL;
3508 t = t->vn_nextref)
3509 {
3510 Elf_Internal_Vernaux *a;
3511
3512 size += sizeof (Elf_External_Verneed);
3513 ++crefs;
3514 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3515 size += sizeof (Elf_External_Vernaux);
3516 }
3517
3518 s->_raw_size = size;
dc810e39 3519 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
252b5132
RH
3520 if (s->contents == NULL)
3521 return false;
3522
3523 p = s->contents;
3524 for (t = elf_tdata (output_bfd)->verref;
3525 t != NULL;
3526 t = t->vn_nextref)
3527 {
3528 unsigned int caux;
3529 Elf_Internal_Vernaux *a;
3530 bfd_size_type indx;
3531
3532 caux = 0;
3533 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3534 ++caux;
3535
3536 t->vn_version = VER_NEED_CURRENT;
3537 t->vn_cnt = caux;
2b0f7ef9
JJ
3538 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3539 elf_dt_name (t->vn_bfd) != NULL
3540 ? elf_dt_name (t->vn_bfd)
3541 : basename (t->vn_bfd->filename),
3542 false);
252b5132
RH
3543 if (indx == (bfd_size_type) -1)
3544 return false;
3545 t->vn_file = indx;
3546 t->vn_aux = sizeof (Elf_External_Verneed);
3547 if (t->vn_nextref == NULL)
3548 t->vn_next = 0;
3549 else
3550 t->vn_next = (sizeof (Elf_External_Verneed)
3551 + caux * sizeof (Elf_External_Vernaux));
3552
3553 _bfd_elf_swap_verneed_out (output_bfd, t,
3554 (Elf_External_Verneed *) p);
3555 p += sizeof (Elf_External_Verneed);
3556
3557 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3558 {
3a99b017 3559 a->vna_hash = bfd_elf_hash (a->vna_nodename);
2b0f7ef9
JJ
3560 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
3561 a->vna_nodename, false);
252b5132
RH
3562 if (indx == (bfd_size_type) -1)
3563 return false;
3564 a->vna_name = indx;
3565 if (a->vna_nextptr == NULL)
3566 a->vna_next = 0;
3567 else
3568 a->vna_next = sizeof (Elf_External_Vernaux);
3569
3570 _bfd_elf_swap_vernaux_out (output_bfd, a,
3571 (Elf_External_Vernaux *) p);
3572 p += sizeof (Elf_External_Vernaux);
3573 }
3574 }
3575
dc810e39
AM
3576 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
3577 (bfd_vma) 0)
3578 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
3579 (bfd_vma) crefs))
252b5132
RH
3580 return false;
3581
3582 elf_tdata (output_bfd)->cverrefs = crefs;
3583 }
3584 }
3585
3e932841 3586 /* Assign dynsym indicies. In a shared library we generate a
30b30c21
RH
3587 section symbol for each output section, which come first.
3588 Next come all of the back-end allocated local dynamic syms,
3589 followed by the rest of the global symbols. */
3590
3591 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3592
3593 /* Work out the size of the symbol version section. */
3594 s = bfd_get_section_by_name (dynobj, ".gnu.version");
3595 BFD_ASSERT (s != NULL);
3596 if (dynsymcount == 0
3597 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
3598 {
7f8d5fc9 3599 _bfd_strip_section_from_output (info, s);
42751cf3
MM
3600 /* The DYNSYMCOUNT might have changed if we were going to
3601 output a dynamic symbol table entry for S. */
30b30c21 3602 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
252b5132
RH
3603 }
3604 else
3605 {
3606 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
3607 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
3608 if (s->contents == NULL)
3609 return false;
3610
dc810e39 3611 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
252b5132
RH
3612 return false;
3613 }
3614
3615 /* Set the size of the .dynsym and .hash sections. We counted
3616 the number of dynamic symbols in elf_link_add_object_symbols.
3617 We will build the contents of .dynsym and .hash when we build
3618 the final symbol table, because until then we do not know the
3619 correct value to give the symbols. We built the .dynstr
3620 section as we went along in elf_link_add_object_symbols. */
3621 s = bfd_get_section_by_name (dynobj, ".dynsym");
3622 BFD_ASSERT (s != NULL);
3623 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
3624 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3625 if (s->contents == NULL && s->_raw_size != 0)
3626 return false;
3627
fc8c40a0
AM
3628 if (dynsymcount != 0)
3629 {
3630 Elf_Internal_Sym isym;
3631
3632 /* The first entry in .dynsym is a dummy symbol. */
3633 isym.st_value = 0;
3634 isym.st_size = 0;
3635 isym.st_name = 0;
3636 isym.st_info = 0;
3637 isym.st_other = 0;
3638 isym.st_shndx = 0;
9ad5cbcf 3639 elf_swap_symbol_out (output_bfd, &isym, (PTR) s->contents, (PTR) 0);
fc8c40a0 3640 }
252b5132
RH
3641
3642 /* Compute the size of the hashing table. As a side effect this
3643 computes the hash values for all the names we export. */
3644 bucketcount = compute_bucket_count (info);
3645
3646 s = bfd_get_section_by_name (dynobj, ".hash");
3647 BFD_ASSERT (s != NULL);
c7ac6ff8
MM
3648 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
3649 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
252b5132
RH
3650 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
3651 if (s->contents == NULL)
3652 return false;
3653 memset (s->contents, 0, (size_t) s->_raw_size);
3654
dc810e39
AM
3655 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
3656 s->contents);
3657 bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
c7ac6ff8 3658 s->contents + hash_entry_size);
252b5132
RH
3659
3660 elf_hash_table (info)->bucketcount = bucketcount;
3661
3662 s = bfd_get_section_by_name (dynobj, ".dynstr");
3663 BFD_ASSERT (s != NULL);
2b0f7ef9
JJ
3664
3665 elf_finalize_dynstr (output_bfd, info);
3666
3667 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
252b5132 3668
db6751f2 3669 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
dc810e39 3670 if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
db6751f2 3671 return false;
252b5132
RH
3672 }
3673
3674 return true;
3675}
3676\f
2b0f7ef9
JJ
3677/* This function is used to adjust offsets into .dynstr for
3678 dynamic symbols. This is called via elf_link_hash_traverse. */
c44233aa 3679
2b0f7ef9
JJ
3680static boolean elf_adjust_dynstr_offsets
3681PARAMS ((struct elf_link_hash_entry *, PTR));
c44233aa 3682
2b0f7ef9
JJ
3683static boolean
3684elf_adjust_dynstr_offsets (h, data)
3685 struct elf_link_hash_entry *h;
3686 PTR data;
3687{
3688 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3689
3690 if (h->dynindx != -1)
3691 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3692 return true;
3693}
3694
3695/* Assign string offsets in .dynstr, update all structures referencing
3696 them. */
3697
3698static boolean
3699elf_finalize_dynstr (output_bfd, info)
3700 bfd *output_bfd;
3701 struct bfd_link_info *info;
3702{
3703 struct elf_link_local_dynamic_entry *entry;
3704 struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
3705 bfd *dynobj = elf_hash_table (info)->dynobj;
3706 asection *sdyn;
3707 bfd_size_type size;
3708 Elf_External_Dyn *dyncon, *dynconend;
3709
3710 _bfd_elf_strtab_finalize (dynstr);
3711 size = _bfd_elf_strtab_size (dynstr);
3712
3713 /* Update all .dynamic entries referencing .dynstr strings. */
3714 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3715 BFD_ASSERT (sdyn != NULL);
3716
3717 dyncon = (Elf_External_Dyn *) sdyn->contents;
3718 dynconend = (Elf_External_Dyn *) (sdyn->contents +
3719 sdyn->_raw_size);
3720 for (; dyncon < dynconend; dyncon++)
3721 {
3722 Elf_Internal_Dyn dyn;
3723
3724 elf_swap_dyn_in (dynobj, dyncon, & dyn);
3725 switch (dyn.d_tag)
3726 {
3727 case DT_STRSZ:
3728 dyn.d_un.d_val = size;
3729 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3730 break;
3731 case DT_NEEDED:
3732 case DT_SONAME:
3733 case DT_RPATH:
3734 case DT_RUNPATH:
3735 case DT_FILTER:
3736 case DT_AUXILIARY:
3737 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3738 elf_swap_dyn_out (dynobj, & dyn, dyncon);
3739 break;
3740 default:
3741 break;
3742 }
3743 }
3744
3745 /* Now update local dynamic symbols. */
3746 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
3747 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3748 entry->isym.st_name);
3749
3750 /* And the rest of dynamic symbols. */
3751 elf_link_hash_traverse (elf_hash_table (info),
3752 elf_adjust_dynstr_offsets, dynstr);
3753
3754 /* Adjust version definitions. */
3755 if (elf_tdata (output_bfd)->cverdefs)
3756 {
3757 asection *s;
3758 bfd_byte *p;
3759 bfd_size_type i;
3760 Elf_Internal_Verdef def;
3761 Elf_Internal_Verdaux defaux;
c44233aa 3762
2b0f7ef9
JJ
3763 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3764 p = (bfd_byte *) s->contents;
3765 do
3766 {
3767 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3768 &def);
3769 p += sizeof (Elf_External_Verdef);
3770 for (i = 0; i < def.vd_cnt; ++i)
3771 {
3772 _bfd_elf_swap_verdaux_in (output_bfd,
3773 (Elf_External_Verdaux *) p, &defaux);
3774 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3775 defaux.vda_name);
3776 _bfd_elf_swap_verdaux_out (output_bfd,
3777 &defaux, (Elf_External_Verdaux *) p);
3778 p += sizeof (Elf_External_Verdaux);
3779 }
3780 }
3781 while (def.vd_next);
3782 }
3783
3784 /* Adjust version references. */
3785 if (elf_tdata (output_bfd)->verref)
3786 {
3787 asection *s;
3788 bfd_byte *p;
3789 bfd_size_type i;
3790 Elf_Internal_Verneed need;
3791 Elf_Internal_Vernaux needaux;
c44233aa 3792
2b0f7ef9
JJ
3793 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3794 p = (bfd_byte *) s->contents;
3795 do
3796 {
3797 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3798 &need);
3799 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3800 _bfd_elf_swap_verneed_out (output_bfd, &need,
3801 (Elf_External_Verneed *) p);
3802 p += sizeof (Elf_External_Verneed);
3803 for (i = 0; i < need.vn_cnt; ++i)
3804 {
3805 _bfd_elf_swap_vernaux_in (output_bfd,
3806 (Elf_External_Vernaux *) p, &needaux);
3807 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3808 needaux.vna_name);
3809 _bfd_elf_swap_vernaux_out (output_bfd,
3810 &needaux,
3811 (Elf_External_Vernaux *) p);
3812 p += sizeof (Elf_External_Vernaux);
3813 }
3814 }
3815 while (need.vn_next);
3816 }
3817
3818 return true;
3819}
3820
252b5132
RH
3821/* Fix up the flags for a symbol. This handles various cases which
3822 can only be fixed after all the input files are seen. This is
3823 currently called by both adjust_dynamic_symbol and
3824 assign_sym_version, which is unnecessary but perhaps more robust in
3825 the face of future changes. */
3826
3827static boolean
3828elf_fix_symbol_flags (h, eif)
3829 struct elf_link_hash_entry *h;
3830 struct elf_info_failed *eif;
3831{
3832 /* If this symbol was mentioned in a non-ELF file, try to set
3833 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3834 permit a non-ELF file to correctly refer to a symbol defined in
3835 an ELF dynamic object. */
3836 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
3837 {
94b6c40a
L
3838 while (h->root.type == bfd_link_hash_indirect)
3839 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3840
252b5132
RH
3841 if (h->root.type != bfd_link_hash_defined
3842 && h->root.type != bfd_link_hash_defweak)
3843 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3844 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3845 else
3846 {
3847 if (h->root.u.def.section->owner != NULL
3848 && (bfd_get_flavour (h->root.u.def.section->owner)
3849 == bfd_target_elf_flavour))
3850 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
3851 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
3852 else
3853 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3854 }
3855
3856 if (h->dynindx == -1
3857 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3858 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3859 {
3860 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
3861 {
3862 eif->failed = true;
3863 return false;
3864 }
3865 }
3866 }
3867 else
3868 {
3869 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
c44233aa
AM
3870 was first seen in a non-ELF file. Fortunately, if the symbol
3871 was first seen in an ELF file, we're probably OK unless the
3872 symbol was defined in a non-ELF file. Catch that case here.
3873 FIXME: We're still in trouble if the symbol was first seen in
3874 a dynamic object, and then later in a non-ELF regular object. */
252b5132
RH
3875 if ((h->root.type == bfd_link_hash_defined
3876 || h->root.type == bfd_link_hash_defweak)
3877 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3878 && (h->root.u.def.section->owner != NULL
3879 ? (bfd_get_flavour (h->root.u.def.section->owner)
3880 != bfd_target_elf_flavour)
3881 : (bfd_is_abs_section (h->root.u.def.section)
3882 && (h->elf_link_hash_flags
3883 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
3884 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3885 }
3886
3887 /* If this is a final link, and the symbol was defined as a common
3888 symbol in a regular object file, and there was no definition in
3889 any dynamic object, then the linker will have allocated space for
3890 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
3891 flag will not have been set. */
3892 if (h->root.type == bfd_link_hash_defined
3893 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3894 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
3895 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3896 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3897 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3898
3899 /* If -Bsymbolic was used (which means to bind references to global
3900 symbols to the definition within the shared object), and this
3901 symbol was defined in a regular object, then it actually doesn't
d954b040
HPN
3902 need a PLT entry, and we can accomplish that by forcing it local.
3903 Likewise, if the symbol has hidden or internal visibility.
3904 FIXME: It might be that we also do not need a PLT for other
3905 non-hidden visibilities, but we would have to tell that to the
3906 backend specifically; we can't just clear PLT-related data here. */
252b5132
RH
3907 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
3908 && eif->info->shared
8ea2e4bd 3909 && is_elf_hash_table (eif->info)
d954b040
HPN
3910 && (eif->info->symbolic
3911 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3912 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
252b5132
RH
3913 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3914 {
391a809a 3915 struct elf_backend_data *bed;
e5094212 3916 boolean force_local;
8ea2e4bd 3917
391a809a 3918 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
e5094212
AM
3919
3920 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3921 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3922 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
252b5132
RH
3923 }
3924
fc4cc5bb
ILT
3925 /* If this is a weak defined symbol in a dynamic object, and we know
3926 the real definition in the dynamic object, copy interesting flags
3927 over to the real definition. */
3928 if (h->weakdef != NULL)
3929 {
3930 struct elf_link_hash_entry *weakdef;
3931
3932 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3933 || h->root.type == bfd_link_hash_defweak);
3934 weakdef = h->weakdef;
3935 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
3936 || weakdef->root.type == bfd_link_hash_defweak);
3937 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
3938
3939 /* If the real definition is defined by a regular object file,
3940 don't do anything special. See the longer description in
3941 elf_adjust_dynamic_symbol, below. */
3942 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
3943 h->weakdef = NULL;
3944 else
0a991dfe
AM
3945 {
3946 struct elf_backend_data *bed;
3947
3948 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3949 (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
3950 }
fc4cc5bb
ILT
3951 }
3952
252b5132
RH
3953 return true;
3954}
3955
3956/* Make the backend pick a good value for a dynamic symbol. This is
3957 called via elf_link_hash_traverse, and also calls itself
3958 recursively. */
3959
3960static boolean
3961elf_adjust_dynamic_symbol (h, data)
3962 struct elf_link_hash_entry *h;
3963 PTR data;
3964{
3965 struct elf_info_failed *eif = (struct elf_info_failed *) data;
3966 bfd *dynobj;
3967 struct elf_backend_data *bed;
3968
3969 /* Ignore indirect symbols. These are added by the versioning code. */
3970 if (h->root.type == bfd_link_hash_indirect)
3971 return true;
3972
8ea2e4bd
NC
3973 if (! is_elf_hash_table (eif->info))
3974 return false;
3975
252b5132
RH
3976 /* Fix the symbol flags. */
3977 if (! elf_fix_symbol_flags (h, eif))
3978 return false;
3979
3980 /* If this symbol does not require a PLT entry, and it is not
3981 defined by a dynamic object, or is not referenced by a regular
3982 object, ignore it. We do have to handle a weak defined symbol,
3983 even if no regular object refers to it, if we decided to add it
3984 to the dynamic symbol table. FIXME: Do we normally need to worry
3985 about symbols which are defined by one dynamic object and
3986 referenced by another one? */
3987 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
3988 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
3989 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3990 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
3991 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
3992 {
3993 h->plt.offset = (bfd_vma) -1;
3994 return true;
3995 }
3996
3997 /* If we've already adjusted this symbol, don't do it again. This
3998 can happen via a recursive call. */
3999 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4000 return true;
4001
4002 /* Don't look at this symbol again. Note that we must set this
4003 after checking the above conditions, because we may look at a
4004 symbol once, decide not to do anything, and then get called
4005 recursively later after REF_REGULAR is set below. */
4006 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4007
4008 /* If this is a weak definition, and we know a real definition, and
4009 the real symbol is not itself defined by a regular object file,
4010 then get a good value for the real definition. We handle the
4011 real symbol first, for the convenience of the backend routine.
4012
4013 Note that there is a confusing case here. If the real definition
4014 is defined by a regular object file, we don't get the real symbol
4015 from the dynamic object, but we do get the weak symbol. If the
4016 processor backend uses a COPY reloc, then if some routine in the
4017 dynamic object changes the real symbol, we will not see that
4018 change in the corresponding weak symbol. This is the way other
4019 ELF linkers work as well, and seems to be a result of the shared
4020 library model.
4021
4022 I will clarify this issue. Most SVR4 shared libraries define the
4023 variable _timezone and define timezone as a weak synonym. The
4024 tzset call changes _timezone. If you write
4025 extern int timezone;
4026 int _timezone = 5;
4027 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4028 you might expect that, since timezone is a synonym for _timezone,
4029 the same number will print both times. However, if the processor
4030 backend uses a COPY reloc, then actually timezone will be copied
4031 into your process image, and, since you define _timezone
4032 yourself, _timezone will not. Thus timezone and _timezone will
4033 wind up at different memory locations. The tzset call will set
4034 _timezone, leaving timezone unchanged. */
4035
4036 if (h->weakdef != NULL)
4037 {
fc4cc5bb
ILT
4038 /* If we get to this point, we know there is an implicit
4039 reference by a regular object file via the weak symbol H.
4040 FIXME: Is this really true? What if the traversal finds
4041 H->WEAKDEF before it finds H? */
4042 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
252b5132 4043
fc4cc5bb
ILT
4044 if (! elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
4045 return false;
252b5132
RH
4046 }
4047
4048 /* If a symbol has no type and no size and does not require a PLT
4049 entry, then we are probably about to do the wrong thing here: we
4050 are probably going to create a COPY reloc for an empty object.
4051 This case can arise when a shared object is built with assembly
4052 code, and the assembly code fails to set the symbol type. */
4053 if (h->size == 0
4054 && h->type == STT_NOTYPE
4055 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4056 (*_bfd_error_handler)
4057 (_("warning: type and size of dynamic symbol `%s' are not defined"),
4058 h->root.root.string);
4059
4060 dynobj = elf_hash_table (eif->info)->dynobj;
4061 bed = get_elf_backend_data (dynobj);
4062 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
4063 {
4064 eif->failed = true;
4065 return false;
4066 }
4067
4068 return true;
4069}
4070\f
4071/* This routine is used to export all defined symbols into the dynamic
4072 symbol table. It is called via elf_link_hash_traverse. */
4073
4074static boolean
4075elf_export_symbol (h, data)
4076 struct elf_link_hash_entry *h;
4077 PTR data;
4078{
4079 struct elf_info_failed *eif = (struct elf_info_failed *) data;
4080
4081 /* Ignore indirect symbols. These are added by the versioning code. */
4082 if (h->root.type == bfd_link_hash_indirect)
4083 return true;
4084
4085 if (h->dynindx == -1
4086 && (h->elf_link_hash_flags
4087 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
4088 {
bc2b6df7
L
4089 struct bfd_elf_version_tree *t;
4090 struct bfd_elf_version_expr *d;
4091
4092 for (t = eif->verdefs; t != NULL; t = t->next)
252b5132 4093 {
bc2b6df7
L
4094 if (t->globals != NULL)
4095 {
4096 for (d = t->globals; d != NULL; d = d->next)
4097 {
4098 if ((*d->match) (d, h->root.root.string))
4099 goto doit;
4100 }
4101 }
4102
4103 if (t->locals != NULL)
4104 {
4105 for (d = t->locals ; d != NULL; d = d->next)
4106 {
4107 if ((*d->match) (d, h->root.root.string))
4108 return true;
4109 }
4110 }
252b5132 4111 }
bc2b6df7
L
4112
4113 if (!eif->verdefs)
c44233aa 4114 {
bc2b6df7
L
4115doit:
4116 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
4117 {
4118 eif->failed = true;
4119 return false;
4120 }
c44233aa 4121 }
252b5132
RH
4122 }
4123
4124 return true;
4125}
4126\f
4127/* Look through the symbols which are defined in other shared
4128 libraries and referenced here. Update the list of version
4129 dependencies. This will be put into the .gnu.version_r section.
4130 This function is called via elf_link_hash_traverse. */
4131
4132static boolean
4133elf_link_find_version_dependencies (h, data)
4134 struct elf_link_hash_entry *h;
4135 PTR data;
4136{
4137 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
4138 Elf_Internal_Verneed *t;
4139 Elf_Internal_Vernaux *a;
dc810e39 4140 bfd_size_type amt;
252b5132
RH
4141
4142 /* We only care about symbols defined in shared objects with version
4143 information. */
4144 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4145 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4146 || h->dynindx == -1
4147 || h->verinfo.verdef == NULL)
4148 return true;
4149
4150 /* See if we already know about this version. */
4151 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
4152 {
4153 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
4154 continue;
4155
4156 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4157 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
4158 return true;
4159
4160 break;
4161 }
4162
4163 /* This is a new version. Add it to tree we are building. */
4164
4165 if (t == NULL)
4166 {
dc810e39
AM
4167 amt = sizeof *t;
4168 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4169 if (t == NULL)
4170 {
4171 rinfo->failed = true;
4172 return false;
4173 }
4174
4175 t->vn_bfd = h->verinfo.verdef->vd_bfd;
4176 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
4177 elf_tdata (rinfo->output_bfd)->verref = t;
4178 }
4179
dc810e39
AM
4180 amt = sizeof *a;
4181 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
252b5132
RH
4182
4183 /* Note that we are copying a string pointer here, and testing it
4184 above. If bfd_elf_string_from_elf_section is ever changed to
4185 discard the string data when low in memory, this will have to be
4186 fixed. */
4187 a->vna_nodename = h->verinfo.verdef->vd_nodename;
4188
4189 a->vna_flags = h->verinfo.verdef->vd_flags;
4190 a->vna_nextptr = t->vn_auxptr;
4191
4192 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
4193 ++rinfo->vers;
4194
4195 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
4196
4197 t->vn_auxptr = a;
4198
4199 return true;
4200}
4201
4202/* Figure out appropriate versions for all the symbols. We may not
4203 have the version number script until we have read all of the input
4204 files, so until that point we don't know which symbols should be
4205 local. This function is called via elf_link_hash_traverse. */
4206
4207static boolean
4208elf_link_assign_sym_version (h, data)
4209 struct elf_link_hash_entry *h;
4210 PTR data;
4211{
dc810e39
AM
4212 struct elf_assign_sym_version_info *sinfo;
4213 struct bfd_link_info *info;
c61b8717 4214 struct elf_backend_data *bed;
252b5132
RH
4215 struct elf_info_failed eif;
4216 char *p;
dc810e39
AM
4217 bfd_size_type amt;
4218
4219 sinfo = (struct elf_assign_sym_version_info *) data;
4220 info = sinfo->info;
252b5132
RH
4221
4222 /* Fix the symbol flags. */
4223 eif.failed = false;
4224 eif.info = info;
4225 if (! elf_fix_symbol_flags (h, &eif))
4226 {
4227 if (eif.failed)
4228 sinfo->failed = true;
4229 return false;
4230 }
4231
4232 /* We only need version numbers for symbols defined in regular
4233 objects. */
4234 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4235 return true;
4236
c61b8717 4237 bed = get_elf_backend_data (sinfo->output_bfd);
252b5132
RH
4238 p = strchr (h->root.root.string, ELF_VER_CHR);
4239 if (p != NULL && h->verinfo.vertree == NULL)
4240 {
4241 struct bfd_elf_version_tree *t;
4242 boolean hidden;
4243
4244 hidden = true;
4245
4246 /* There are two consecutive ELF_VER_CHR characters if this is
c44233aa 4247 not a hidden symbol. */
252b5132
RH
4248 ++p;
4249 if (*p == ELF_VER_CHR)
4250 {
4251 hidden = false;
4252 ++p;
4253 }
4254
4255 /* If there is no version string, we can just return out. */
4256 if (*p == '\0')
4257 {
4258 if (hidden)
4259 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4260 return true;
4261 }
4262
4263 /* Look for the version. If we find it, it is no longer weak. */
4264 for (t = sinfo->verdefs; t != NULL; t = t->next)
4265 {
4266 if (strcmp (t->name, p) == 0)
4267 {
dc810e39 4268 size_t len;
252b5132
RH
4269 char *alc;
4270 struct bfd_elf_version_expr *d;
4271
4272 len = p - h->root.root.string;
e5094212 4273 alc = bfd_malloc ((bfd_size_type) len);
252b5132 4274 if (alc == NULL)
c44233aa 4275 return false;
252b5132
RH
4276 strncpy (alc, h->root.root.string, len - 1);
4277 alc[len - 1] = '\0';
4278 if (alc[len - 2] == ELF_VER_CHR)
c44233aa 4279 alc[len - 2] = '\0';
252b5132
RH
4280
4281 h->verinfo.vertree = t;
4282 t->used = true;
4283 d = NULL;
4284
4285 if (t->globals != NULL)
4286 {
4287 for (d = t->globals; d != NULL; d = d->next)
4288 if ((*d->match) (d, alc))
4289 break;
4290 }
4291
4292 /* See if there is anything to force this symbol to
c44233aa 4293 local scope. */
252b5132
RH
4294 if (d == NULL && t->locals != NULL)
4295 {
4296 for (d = t->locals; d != NULL; d = d->next)
4297 {
4298 if ((*d->match) (d, alc))
4299 {
4300 if (h->dynindx != -1
4301 && info->shared
99293407 4302 && ! info->export_dynamic)
252b5132 4303 {
e5094212 4304 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4305 }
4306
4307 break;
4308 }
4309 }
4310 }
4311
e5094212 4312 free (alc);
252b5132
RH
4313 break;
4314 }
4315 }
4316
4317 /* If we are building an application, we need to create a
c44233aa 4318 version node for this version. */
252b5132
RH
4319 if (t == NULL && ! info->shared)
4320 {
4321 struct bfd_elf_version_tree **pp;
4322 int version_index;
4323
4324 /* If we aren't going to export this symbol, we don't need
c44233aa 4325 to worry about it. */
252b5132
RH
4326 if (h->dynindx == -1)
4327 return true;
4328
dc810e39 4329 amt = sizeof *t;
252b5132 4330 t = ((struct bfd_elf_version_tree *)
dc810e39 4331 bfd_alloc (sinfo->output_bfd, amt));
252b5132
RH
4332 if (t == NULL)
4333 {
4334 sinfo->failed = true;
4335 return false;
4336 }
4337
4338 t->next = NULL;
4339 t->name = p;
4340 t->globals = NULL;
4341 t->locals = NULL;
4342 t->deps = NULL;
4343 t->name_indx = (unsigned int) -1;
4344 t->used = true;
4345
4346 version_index = 1;
6b9b879a
JJ
4347 /* Don't count anonymous version tag. */
4348 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
4349 version_index = 0;
252b5132
RH
4350 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
4351 ++version_index;
4352 t->vernum = version_index;
4353
4354 *pp = t;
4355
4356 h->verinfo.vertree = t;
4357 }
4358 else if (t == NULL)
4359 {
4360 /* We could not find the version for a symbol when
c44233aa 4361 generating a shared archive. Return an error. */
252b5132
RH
4362 (*_bfd_error_handler)
4363 (_("%s: undefined versioned symbol name %s"),
4364 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
4365 bfd_set_error (bfd_error_bad_value);
4366 sinfo->failed = true;
4367 return false;
4368 }
4369
4370 if (hidden)
4371 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
4372 }
4373
4374 /* If we don't have a version for this symbol, see if we can find
4375 something. */
4376 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
4377 {
4378 struct bfd_elf_version_tree *t;
4379 struct bfd_elf_version_tree *deflt;
4380 struct bfd_elf_version_expr *d;
4381
4382 /* See if can find what version this symbol is in. If the
c44233aa
AM
4383 symbol is supposed to be local, then don't actually register
4384 it. */
252b5132
RH
4385 deflt = NULL;
4386 for (t = sinfo->verdefs; t != NULL; t = t->next)
4387 {
4388 if (t->globals != NULL)
4389 {
4390 for (d = t->globals; d != NULL; d = d->next)
4391 {
4392 if ((*d->match) (d, h->root.root.string))
4393 {
4394 h->verinfo.vertree = t;
4395 break;
4396 }
4397 }
4398
4399 if (d != NULL)
4400 break;
4401 }
4402
4403 if (t->locals != NULL)
4404 {
4405 for (d = t->locals; d != NULL; d = d->next)
4406 {
4407 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
4408 deflt = t;
4409 else if ((*d->match) (d, h->root.root.string))
4410 {
4411 h->verinfo.vertree = t;
4412 if (h->dynindx != -1
4413 && info->shared
99293407 4414 && ! info->export_dynamic)
252b5132 4415 {
e5094212 4416 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4417 }
4418 break;
4419 }
4420 }
4421
4422 if (d != NULL)
4423 break;
4424 }
4425 }
4426
4427 if (deflt != NULL && h->verinfo.vertree == NULL)
4428 {
4429 h->verinfo.vertree = deflt;
4430 if (h->dynindx != -1
4431 && info->shared
99293407 4432 && ! info->export_dynamic)
252b5132 4433 {
e5094212 4434 (*bed->elf_backend_hide_symbol) (info, h, true);
252b5132
RH
4435 }
4436 }
4437 }
4438
4439 return true;
4440}
252b5132
RH
4441\f
4442/* Final phase of ELF linker. */
4443
4444/* A structure we use to avoid passing large numbers of arguments. */
4445
4446struct elf_final_link_info
4447{
4448 /* General link information. */
4449 struct bfd_link_info *info;
4450 /* Output BFD. */
4451 bfd *output_bfd;
4452 /* Symbol string table. */
4453 struct bfd_strtab_hash *symstrtab;
4454 /* .dynsym section. */
4455 asection *dynsym_sec;
4456 /* .hash section. */
4457 asection *hash_sec;
4458 /* symbol version section (.gnu.version). */
4459 asection *symver_sec;
4460 /* Buffer large enough to hold contents of any section. */
4461 bfd_byte *contents;
4462 /* Buffer large enough to hold external relocs of any section. */
4463 PTR external_relocs;
4464 /* Buffer large enough to hold internal relocs of any section. */
4465 Elf_Internal_Rela *internal_relocs;
4466 /* Buffer large enough to hold external local symbols of any input
4467 BFD. */
4468 Elf_External_Sym *external_syms;
9ad5cbcf
AM
4469 /* And a buffer for symbol section indices. */
4470 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
4471 /* Buffer large enough to hold internal local symbols of any input
4472 BFD. */
4473 Elf_Internal_Sym *internal_syms;
4474 /* Array large enough to hold a symbol index for each local symbol
4475 of any input BFD. */
4476 long *indices;
4477 /* Array large enough to hold a section pointer for each local
4478 symbol of any input BFD. */
4479 asection **sections;
4480 /* Buffer to hold swapped out symbols. */
4481 Elf_External_Sym *symbuf;
9ad5cbcf
AM
4482 /* And one for symbol section indices. */
4483 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
4484 /* Number of swapped out symbols in buffer. */
4485 size_t symbuf_count;
4486 /* Number of symbols which fit in symbuf. */
4487 size_t symbuf_size;
4488};
4489
4490static boolean elf_link_output_sym
4491 PARAMS ((struct elf_final_link_info *, const char *,
4492 Elf_Internal_Sym *, asection *));
4493static boolean elf_link_flush_output_syms
4494 PARAMS ((struct elf_final_link_info *));
4495static boolean elf_link_output_extsym
4496 PARAMS ((struct elf_link_hash_entry *, PTR));
f5fa8ca2
JJ
4497static boolean elf_link_sec_merge_syms
4498 PARAMS ((struct elf_link_hash_entry *, PTR));
252b5132
RH
4499static boolean elf_link_input_bfd
4500 PARAMS ((struct elf_final_link_info *, bfd *));
4501static boolean elf_reloc_link_order
4502 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4503 struct bfd_link_order *));
4504
4505/* This struct is used to pass information to elf_link_output_extsym. */
4506
4507struct elf_outext_info
4508{
4509 boolean failed;
4510 boolean localsyms;
4511 struct elf_final_link_info *finfo;
4512};
4513
23bc299b
MM
4514/* Compute the size of, and allocate space for, REL_HDR which is the
4515 section header for a section containing relocations for O. */
4516
4517static boolean
4518elf_link_size_reloc_section (abfd, rel_hdr, o)
4519 bfd *abfd;
4520 Elf_Internal_Shdr *rel_hdr;
4521 asection *o;
4522{
dc810e39
AM
4523 bfd_size_type reloc_count;
4524 bfd_size_type num_rel_hashes;
23bc299b 4525
b037af20
MM
4526 /* Figure out how many relocations there will be. */
4527 if (rel_hdr == &elf_section_data (o)->rel_hdr)
4528 reloc_count = elf_section_data (o)->rel_count;
4529 else
4530 reloc_count = elf_section_data (o)->rel_count2;
4531
9317eacc
CM
4532 num_rel_hashes = o->reloc_count;
4533 if (num_rel_hashes < reloc_count)
4534 num_rel_hashes = reloc_count;
dc810e39 4535
b037af20
MM
4536 /* That allows us to calculate the size of the section. */
4537 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
23bc299b
MM
4538
4539 /* The contents field must last into write_object_contents, so we
755cfd29
NC
4540 allocate it with bfd_alloc rather than malloc. Also since we
4541 cannot be sure that the contents will actually be filled in,
4542 we zero the allocated space. */
4543 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
23bc299b
MM
4544 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
4545 return false;
3e932841 4546
b037af20
MM
4547 /* We only allocate one set of hash entries, so we only do it the
4548 first time we are called. */
9317eacc
CM
4549 if (elf_section_data (o)->rel_hashes == NULL
4550 && num_rel_hashes)
b037af20 4551 {
209f668e
NC
4552 struct elf_link_hash_entry **p;
4553
b037af20 4554 p = ((struct elf_link_hash_entry **)
9317eacc 4555 bfd_zmalloc (num_rel_hashes
209f668e 4556 * sizeof (struct elf_link_hash_entry *)));
9317eacc 4557 if (p == NULL)
b037af20 4558 return false;
23bc299b 4559
b037af20 4560 elf_section_data (o)->rel_hashes = p;
b037af20 4561 }
23bc299b
MM
4562
4563 return true;
4564}
4565
31367b81
MM
4566/* When performing a relocateable link, the input relocations are
4567 preserved. But, if they reference global symbols, the indices
4568 referenced must be updated. Update all the relocations in
4569 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
4570
4571static void
4572elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
4573 bfd *abfd;
4574 Elf_Internal_Shdr *rel_hdr;
4575 unsigned int count;
4576 struct elf_link_hash_entry **rel_hash;
4577{
4578 unsigned int i;
32f0787a 4579 struct elf_backend_data *bed = get_elf_backend_data (abfd);
209f668e
NC
4580 Elf_Internal_Rel *irel;
4581 Elf_Internal_Rela *irela;
dc810e39 4582 bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
209f668e 4583
dc810e39 4584 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
209f668e
NC
4585 if (irel == NULL)
4586 {
4587 (*_bfd_error_handler) (_("Error: out of memory"));
4588 abort ();
4589 }
4590
dc810e39
AM
4591 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
4592 irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
209f668e
NC
4593 if (irela == NULL)
4594 {
4595 (*_bfd_error_handler) (_("Error: out of memory"));
4596 abort ();
4597 }
31367b81
MM
4598
4599 for (i = 0; i < count; i++, rel_hash++)
4600 {
4601 if (*rel_hash == NULL)
4602 continue;
4603
4604 BFD_ASSERT ((*rel_hash)->indx >= 0);
4605
4606 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
4607 {
4608 Elf_External_Rel *erel;
209f668e 4609 unsigned int j;
3e932841 4610
31367b81 4611 erel = (Elf_External_Rel *) rel_hdr->contents + i;
32f0787a 4612 if (bed->s->swap_reloc_in)
209f668e 4613 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
32f0787a 4614 else
209f668e
NC
4615 elf_swap_reloc_in (abfd, erel, irel);
4616
4617 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4618 irel[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4619 ELF_R_TYPE (irel[j].r_info));
4620
32f0787a 4621 if (bed->s->swap_reloc_out)
209f668e 4622 (*bed->s->swap_reloc_out) (abfd, irel, (bfd_byte *) erel);
32f0787a 4623 else
209f668e 4624 elf_swap_reloc_out (abfd, irel, erel);
31367b81
MM
4625 }
4626 else
4627 {
4628 Elf_External_Rela *erela;
209f668e 4629 unsigned int j;
3e932841 4630
31367b81
MM
4631 BFD_ASSERT (rel_hdr->sh_entsize
4632 == sizeof (Elf_External_Rela));
3e932841 4633
31367b81 4634 erela = (Elf_External_Rela *) rel_hdr->contents + i;
32f0787a 4635 if (bed->s->swap_reloca_in)
209f668e 4636 (*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
32f0787a 4637 else
209f668e
NC
4638 elf_swap_reloca_in (abfd, erela, irela);
4639
4640 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
4641 irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
4642 ELF_R_TYPE (irela[j].r_info));
4643
32f0787a 4644 if (bed->s->swap_reloca_out)
209f668e 4645 (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
32f0787a 4646 else
209f668e 4647 elf_swap_reloca_out (abfd, irela, erela);
31367b81
MM
4648 }
4649 }
209f668e
NC
4650
4651 free (irel);
4652 free (irela);
31367b81
MM
4653}
4654
db6751f2
JJ
4655struct elf_link_sort_rela {
4656 bfd_vma offset;
4657 enum elf_reloc_type_class type;
4658 union {
4659 Elf_Internal_Rel rel;
4660 Elf_Internal_Rela rela;
4661 } u;
4662};
4663
4664static int
4665elf_link_sort_cmp1 (A, B)
4666 const PTR A;
4667 const PTR B;
4668{
f51e552e
AM
4669 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4670 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4671 int relativea, relativeb;
4672
4673 relativea = a->type == reloc_class_relative;
4674 relativeb = b->type == reloc_class_relative;
4675
4676 if (relativea < relativeb)
db6751f2 4677 return 1;
fcfbdf31
JJ
4678 if (relativea > relativeb)
4679 return -1;
db6751f2
JJ
4680 if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
4681 return -1;
4682 if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
4683 return 1;
4684 if (a->u.rel.r_offset < b->u.rel.r_offset)
4685 return -1;
4686 if (a->u.rel.r_offset > b->u.rel.r_offset)
4687 return 1;
4688 return 0;
4689}
4690
4691static int
4692elf_link_sort_cmp2 (A, B)
4693 const PTR A;
4694 const PTR B;
4695{
f51e552e
AM
4696 struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
4697 struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
db6751f2
JJ
4698 int copya, copyb;
4699
4700 if (a->offset < b->offset)
4701 return -1;
4702 if (a->offset > b->offset)
4703 return 1;
290394d6
JJ
4704 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
4705 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
4706 if (copya < copyb)
4707 return -1;
4708 if (copya > copyb)
4709 return 1;
4710 if (a->u.rel.r_offset < b->u.rel.r_offset)
4711 return -1;
4712 if (a->u.rel.r_offset > b->u.rel.r_offset)
4713 return 1;
4714 return 0;
4715}
4716
4717static size_t
4718elf_link_sort_relocs (abfd, info, psec)
4719 bfd *abfd;
4720 struct bfd_link_info *info;
4721 asection **psec;
4722{
4723 bfd *dynobj = elf_hash_table (info)->dynobj;
4724 asection *reldyn, *o;
4725 boolean rel = false;
f51e552e
AM
4726 bfd_size_type count, size;
4727 size_t i, j, ret;
db6751f2
JJ
4728 struct elf_link_sort_rela *rela;
4729 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4730
4731 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
4732 if (reldyn == NULL || reldyn->_raw_size == 0)
4733 {
4734 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
4735 if (reldyn == NULL || reldyn->_raw_size == 0)
4736 return 0;
4737 rel = true;
4738 count = reldyn->_raw_size / sizeof (Elf_External_Rel);
4739 }
4740 else
4741 count = reldyn->_raw_size / sizeof (Elf_External_Rela);
4742
4743 size = 0;
4744 for (o = dynobj->sections; o != NULL; o = o->next)
4745 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4746 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4747 && o->output_section == reldyn)
4748 size += o->_raw_size;
4749
4750 if (size != reldyn->_raw_size)
4751 return 0;
4752
f51e552e 4753 rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
db6751f2
JJ
4754 if (rela == NULL)
4755 {
4756 (*info->callbacks->warning)
dc810e39
AM
4757 (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
4758 (bfd_vma) 0);
db6751f2
JJ
4759 return 0;
4760 }
4761
4762 for (o = dynobj->sections; o != NULL; o = o->next)
4763 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4764 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4765 && o->output_section == reldyn)
4766 {
4767 if (rel)
4768 {
4769 Elf_External_Rel *erel, *erelend;
4770 struct elf_link_sort_rela *s;
4771
4772 erel = (Elf_External_Rel *) o->contents;
f51e552e 4773 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
db6751f2
JJ
4774 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4775 for (; erel < erelend; erel++, s++)
4776 {
4777 if (bed->s->swap_reloc_in)
4778 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
4779 else
4780 elf_swap_reloc_in (abfd, erel, &s->u.rel);
4781
f51e552e 4782 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
dc810e39 4783 }
db6751f2
JJ
4784 }
4785 else
4786 {
4787 Elf_External_Rela *erela, *erelaend;
4788 struct elf_link_sort_rela *s;
4789
4790 erela = (Elf_External_Rela *) o->contents;
f51e552e 4791 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
db6751f2
JJ
4792 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4793 for (; erela < erelaend; erela++, s++)
4794 {
4795 if (bed->s->swap_reloca_in)
dc810e39
AM
4796 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
4797 &s->u.rela);
db6751f2
JJ
4798 else
4799 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
4800
f51e552e 4801 s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
dc810e39 4802 }
db6751f2
JJ
4803 }
4804 }
4805
973ffd63 4806 qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
fcfbdf31
JJ
4807 for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
4808 ;
4809 for (i = ret, j = ret; i < count; i++)
db6751f2
JJ
4810 {
4811 if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
4812 j = i;
4813 rela[i].offset = rela[j].u.rel.r_offset;
4814 }
973ffd63 4815 qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
dc810e39 4816
db6751f2
JJ
4817 for (o = dynobj->sections; o != NULL; o = o->next)
4818 if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
4819 == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
4820 && o->output_section == reldyn)
4821 {
4822 if (rel)
4823 {
4824 Elf_External_Rel *erel, *erelend;
4825 struct elf_link_sort_rela *s;
4826
4827 erel = (Elf_External_Rel *) o->contents;
df22989b 4828 erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
db6751f2
JJ
4829 s = rela + o->output_offset / sizeof (Elf_External_Rel);
4830 for (; erel < erelend; erel++, s++)
4831 {
4832 if (bed->s->swap_reloc_out)
dc810e39
AM
4833 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
4834 (bfd_byte *) erel);
db6751f2
JJ
4835 else
4836 elf_swap_reloc_out (abfd, &s->u.rel, erel);
4837 }
4838 }
4839 else
4840 {
4841 Elf_External_Rela *erela, *erelaend;
4842 struct elf_link_sort_rela *s;
4843
4844 erela = (Elf_External_Rela *) o->contents;
df22989b 4845 erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
db6751f2
JJ
4846 s = rela + o->output_offset / sizeof (Elf_External_Rela);
4847 for (; erela < erelaend; erela++, s++)
4848 {
4849 if (bed->s->swap_reloca_out)
dc810e39
AM
4850 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
4851 (bfd_byte *) erela);
db6751f2
JJ
4852 else
4853 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
dc810e39 4854 }
db6751f2
JJ
4855 }
4856 }
4857
4858 free (rela);
4859 *psec = reldyn;
4860 return ret;
4861}
4862
252b5132
RH
4863/* Do the final step of an ELF link. */
4864
4865boolean
4866elf_bfd_final_link (abfd, info)
4867 bfd *abfd;
4868 struct bfd_link_info *info;
4869{
4870 boolean dynamic;
9317eacc 4871 boolean emit_relocs;
252b5132
RH
4872 bfd *dynobj;
4873 struct elf_final_link_info finfo;
4874 register asection *o;
4875 register struct bfd_link_order *p;
4876 register bfd *sub;
dc810e39
AM
4877 bfd_size_type max_contents_size;
4878 bfd_size_type max_external_reloc_size;
4879 bfd_size_type max_internal_reloc_count;
4880 bfd_size_type max_sym_count;
9ad5cbcf 4881 bfd_size_type max_sym_shndx_count;
252b5132
RH
4882 file_ptr off;
4883 Elf_Internal_Sym elfsym;
4884 unsigned int i;
4885 Elf_Internal_Shdr *symtab_hdr;
4886 Elf_Internal_Shdr *symstrtab_hdr;
4887 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4888 struct elf_outext_info eoinfo;
f5fa8ca2 4889 boolean merged;
db6751f2
JJ
4890 size_t relativecount = 0;
4891 asection *reldyn = 0;
dc810e39 4892 bfd_size_type amt;
252b5132 4893
8ea2e4bd
NC
4894 if (! is_elf_hash_table (info))
4895 return false;
4896
252b5132
RH
4897 if (info->shared)
4898 abfd->flags |= DYNAMIC;
4899
4900 dynamic = elf_hash_table (info)->dynamic_sections_created;
4901 dynobj = elf_hash_table (info)->dynobj;
4902
9317eacc 4903 emit_relocs = (info->relocateable
c44233aa
AM
4904 || info->emitrelocations
4905 || bed->elf_backend_emit_relocs);
9317eacc 4906
252b5132
RH
4907 finfo.info = info;
4908 finfo.output_bfd = abfd;
4909 finfo.symstrtab = elf_stringtab_init ();
4910 if (finfo.symstrtab == NULL)
4911 return false;
4912
4913 if (! dynamic)
4914 {
4915 finfo.dynsym_sec = NULL;
4916 finfo.hash_sec = NULL;
4917 finfo.symver_sec = NULL;
4918 }
4919 else
4920 {
4921 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
4922 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
4923 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
4924 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
4925 /* Note that it is OK if symver_sec is NULL. */
4926 }
4927
4928 finfo.contents = NULL;
4929 finfo.external_relocs = NULL;
4930 finfo.internal_relocs = NULL;
4931 finfo.external_syms = NULL;
9ad5cbcf 4932 finfo.locsym_shndx = NULL;
252b5132
RH
4933 finfo.internal_syms = NULL;
4934 finfo.indices = NULL;
4935 finfo.sections = NULL;
4936 finfo.symbuf = NULL;
9ad5cbcf 4937 finfo.symshndxbuf = NULL;
252b5132
RH
4938 finfo.symbuf_count = 0;
4939
4940 /* Count up the number of relocations we will output for each output
4941 section, so that we know the sizes of the reloc sections. We
4942 also figure out some maximum sizes. */
4943 max_contents_size = 0;
4944 max_external_reloc_size = 0;
4945 max_internal_reloc_count = 0;
4946 max_sym_count = 0;
9ad5cbcf 4947 max_sym_shndx_count = 0;
f5fa8ca2 4948 merged = false;
252b5132
RH
4949 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4950 {
4951 o->reloc_count = 0;
4952
4953 for (p = o->link_order_head; p != NULL; p = p->next)
4954 {
4955 if (p->type == bfd_section_reloc_link_order
4956 || p->type == bfd_symbol_reloc_link_order)
4957 ++o->reloc_count;
4958 else if (p->type == bfd_indirect_link_order)
4959 {
4960 asection *sec;
4961
4962 sec = p->u.indirect.section;
4963
4964 /* Mark all sections which are to be included in the
4965 link. This will normally be every section. We need
4966 to do this so that we can identify any sections which
4967 the linker has decided to not include. */
4968 sec->linker_mark = true;
4969
f5fa8ca2
JJ
4970 if (sec->flags & SEC_MERGE)
4971 merged = true;
4972
a712da20 4973 if (info->relocateable || info->emitrelocations)
252b5132 4974 o->reloc_count += sec->reloc_count;
c44233aa 4975 else if (bed->elf_backend_count_relocs)
9317eacc
CM
4976 {
4977 Elf_Internal_Rela * relocs;
4978
4979 relocs = (NAME(_bfd_elf,link_read_relocs)
4980 (abfd, sec, (PTR) NULL,
4981 (Elf_Internal_Rela *) NULL, info->keep_memory));
4982
c44233aa
AM
4983 o->reloc_count
4984 += (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc
CM
4985
4986 if (!info->keep_memory)
4987 free (relocs);
4988 }
252b5132
RH
4989
4990 if (sec->_raw_size > max_contents_size)
4991 max_contents_size = sec->_raw_size;
4992 if (sec->_cooked_size > max_contents_size)
4993 max_contents_size = sec->_cooked_size;
4994
4995 /* We are interested in just local symbols, not all
4996 symbols. */
4997 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
4998 && (sec->owner->flags & DYNAMIC) == 0)
4999 {
5000 size_t sym_count;
5001
5002 if (elf_bad_symtab (sec->owner))
5003 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
5004 / sizeof (Elf_External_Sym));
5005 else
5006 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5007
5008 if (sym_count > max_sym_count)
5009 max_sym_count = sym_count;
5010
9ad5cbcf
AM
5011 if (sym_count > max_sym_shndx_count
5012 && elf_symtab_shndx (sec->owner) != 0)
5013 max_sym_shndx_count = sym_count;
5014
252b5132
RH
5015 if ((sec->flags & SEC_RELOC) != 0)
5016 {
5017 size_t ext_size;
5018
5019 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5020 if (ext_size > max_external_reloc_size)
5021 max_external_reloc_size = ext_size;
5022 if (sec->reloc_count > max_internal_reloc_count)
5023 max_internal_reloc_count = sec->reloc_count;
5024 }
5025 }
5026 }
5027 }
5028
5029 if (o->reloc_count > 0)
5030 o->flags |= SEC_RELOC;
5031 else
5032 {
5033 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5034 set it (this is probably a bug) and if it is set
5035 assign_section_numbers will create a reloc section. */
5036 o->flags &=~ SEC_RELOC;
5037 }
5038
5039 /* If the SEC_ALLOC flag is not set, force the section VMA to
5040 zero. This is done in elf_fake_sections as well, but forcing
5041 the VMA to 0 here will ensure that relocs against these
5042 sections are handled correctly. */
5043 if ((o->flags & SEC_ALLOC) == 0
5044 && ! o->user_set_vma)
5045 o->vma = 0;
5046 }
5047
f5fa8ca2
JJ
5048 if (! info->relocateable && merged)
5049 elf_link_hash_traverse (elf_hash_table (info),
5050 elf_link_sec_merge_syms, (PTR) abfd);
5051
252b5132
RH
5052 /* Figure out the file positions for everything but the symbol table
5053 and the relocs. We set symcount to force assign_section_numbers
5054 to create a symbol table. */
5055 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
5056 BFD_ASSERT (! abfd->output_has_begun);
5057 if (! _bfd_elf_compute_section_file_positions (abfd, info))
5058 goto error_return;
5059
b037af20
MM
5060 /* Figure out how many relocations we will have in each section.
5061 Just using RELOC_COUNT isn't good enough since that doesn't
5062 maintain a separate value for REL vs. RELA relocations. */
9317eacc 5063 if (emit_relocs)
b037af20
MM
5064 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5065 for (o = sub->sections; o != NULL; o = o->next)
5066 {
814fe68a 5067 asection *output_section;
b037af20 5068
814fe68a
ILT
5069 if (! o->linker_mark)
5070 {
5071 /* This section was omitted from the link. */
5072 continue;
5073 }
5074
5075 output_section = o->output_section;
5076
5077 if (output_section != NULL
5078 && (o->flags & SEC_RELOC) != 0)
b037af20 5079 {
3e932841 5080 struct bfd_elf_section_data *esdi
b037af20 5081 = elf_section_data (o);
3e932841 5082 struct bfd_elf_section_data *esdo
b037af20 5083 = elf_section_data (output_section);
ce006217
MM
5084 unsigned int *rel_count;
5085 unsigned int *rel_count2;
c119f9b8
AM
5086 bfd_size_type entsize;
5087 bfd_size_type entsize2;
b037af20 5088
c119f9b8 5089 /* We must be careful to add the relocations from the
ce006217 5090 input section to the right output count. */
c119f9b8
AM
5091 entsize = esdi->rel_hdr.sh_entsize;
5092 entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
5093 BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
5094 || entsize == sizeof (Elf_External_Rela))
5095 && entsize2 != entsize
5096 && (entsize2 == 0
5097 || entsize2 == sizeof (Elf_External_Rel)
5098 || entsize2 == sizeof (Elf_External_Rela)));
5099 if (entsize == esdo->rel_hdr.sh_entsize)
ce006217
MM
5100 {
5101 rel_count = &esdo->rel_count;
5102 rel_count2 = &esdo->rel_count2;
5103 }
5104 else
5105 {
5106 rel_count = &esdo->rel_count2;
5107 rel_count2 = &esdo->rel_count;
5108 }
3e932841 5109
d9bc7a44 5110 *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
b037af20 5111 if (esdi->rel_hdr2)
d9bc7a44 5112 *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
9317eacc 5113 output_section->flags |= SEC_RELOC;
b037af20
MM
5114 }
5115 }
5116
252b5132
RH
5117 /* That created the reloc sections. Set their sizes, and assign
5118 them file positions, and allocate some buffers. */
5119 for (o = abfd->sections; o != NULL; o = o->next)
5120 {
5121 if ((o->flags & SEC_RELOC) != 0)
5122 {
23bc299b
MM
5123 if (!elf_link_size_reloc_section (abfd,
5124 &elf_section_data (o)->rel_hdr,
5125 o))
252b5132
RH
5126 goto error_return;
5127
23bc299b
MM
5128 if (elf_section_data (o)->rel_hdr2
5129 && !elf_link_size_reloc_section (abfd,
5130 elf_section_data (o)->rel_hdr2,
5131 o))
252b5132 5132 goto error_return;
252b5132 5133 }
b037af20
MM
5134
5135 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 5136 to count upwards while actually outputting the relocations. */
b037af20
MM
5137 elf_section_data (o)->rel_count = 0;
5138 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
5139 }
5140
5141 _bfd_elf_assign_file_positions_for_relocs (abfd);
5142
5143 /* We have now assigned file positions for all the sections except
5144 .symtab and .strtab. We start the .symtab section at the current
5145 file position, and write directly to it. We build the .strtab
5146 section in memory. */
5147 bfd_get_symcount (abfd) = 0;
5148 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5149 /* sh_name is set in prep_headers. */
5150 symtab_hdr->sh_type = SHT_SYMTAB;
5151 symtab_hdr->sh_flags = 0;
5152 symtab_hdr->sh_addr = 0;
5153 symtab_hdr->sh_size = 0;
5154 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5155 /* sh_link is set in assign_section_numbers. */
5156 /* sh_info is set below. */
5157 /* sh_offset is set just below. */
f0e1d18a 5158 symtab_hdr->sh_addralign = bed->s->file_align;
252b5132
RH
5159
5160 off = elf_tdata (abfd)->next_file_pos;
5161 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
5162
5163 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5164 incorrect. We do not yet know the size of the .symtab section.
5165 We correct next_file_pos below, after we do know the size. */
5166
5167 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5168 continuously seeking to the right position in the file. */
5169 if (! info->keep_memory || max_sym_count < 20)
5170 finfo.symbuf_size = 20;
5171 else
5172 finfo.symbuf_size = max_sym_count;
dc810e39
AM
5173 amt = finfo.symbuf_size;
5174 amt *= sizeof (Elf_External_Sym);
5175 finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
252b5132
RH
5176 if (finfo.symbuf == NULL)
5177 goto error_return;
9ad5cbcf
AM
5178 if (elf_numsections (abfd) > SHN_LORESERVE)
5179 {
5180 amt = finfo.symbuf_size;
5181 amt *= sizeof (Elf_External_Sym_Shndx);
5182 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5183 if (finfo.symshndxbuf == NULL)
5184 goto error_return;
5185 }
252b5132
RH
5186
5187 /* Start writing out the symbol table. The first symbol is always a
5188 dummy symbol. */
9317eacc
CM
5189 if (info->strip != strip_all
5190 || emit_relocs)
252b5132
RH
5191 {
5192 elfsym.st_value = 0;
5193 elfsym.st_size = 0;
5194 elfsym.st_info = 0;
5195 elfsym.st_other = 0;
5196 elfsym.st_shndx = SHN_UNDEF;
5197 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5198 &elfsym, bfd_und_section_ptr))
5199 goto error_return;
5200 }
5201
5202#if 0
5203 /* Some standard ELF linkers do this, but we don't because it causes
5204 bootstrap comparison failures. */
5205 /* Output a file symbol for the output file as the second symbol.
5206 We output this even if we are discarding local symbols, although
5207 I'm not sure if this is correct. */
5208 elfsym.st_value = 0;
5209 elfsym.st_size = 0;
5210 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5211 elfsym.st_other = 0;
5212 elfsym.st_shndx = SHN_ABS;
5213 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
5214 &elfsym, bfd_abs_section_ptr))
5215 goto error_return;
5216#endif
5217
5218 /* Output a symbol for each section. We output these even if we are
5219 discarding local symbols, since they are used for relocs. These
5220 symbols have no names. We store the index of each one in the
5221 index field of the section, so that we can find it again when
5222 outputting relocs. */
9317eacc
CM
5223 if (info->strip != strip_all
5224 || emit_relocs)
252b5132
RH
5225 {
5226 elfsym.st_size = 0;
5227 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5228 elfsym.st_other = 0;
9ad5cbcf 5229 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5230 {
5231 o = section_from_elf_index (abfd, i);
5232 if (o != NULL)
5233 o->target_index = bfd_get_symcount (abfd);
5234 elfsym.st_shndx = i;
7ad34365 5235 if (info->relocateable || o == NULL)
252b5132
RH
5236 elfsym.st_value = 0;
5237 else
5238 elfsym.st_value = o->vma;
5239 if (! elf_link_output_sym (&finfo, (const char *) NULL,
5240 &elfsym, o))
5241 goto error_return;
9ad5cbcf
AM
5242 if (i == SHN_LORESERVE)
5243 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
5244 }
5245 }
5246
5247 /* Allocate some memory to hold information read in from the input
5248 files. */
9ad5cbcf
AM
5249 if (max_contents_size != 0)
5250 {
5251 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5252 if (finfo.contents == NULL)
5253 goto error_return;
5254 }
5255
5256 if (max_external_reloc_size != 0)
5257 {
5258 finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
5259 if (finfo.external_relocs == NULL)
5260 goto error_return;
5261 }
5262
5263 if (max_internal_reloc_count != 0)
5264 {
5265 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
5266 amt *= sizeof (Elf_Internal_Rela);
5267 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
5268 if (finfo.internal_relocs == NULL)
5269 goto error_return;
5270 }
5271
5272 if (max_sym_count != 0)
5273 {
5274 amt = max_sym_count * sizeof (Elf_External_Sym);
5275 finfo.external_syms = (Elf_External_Sym *) bfd_malloc (amt);
5276 if (finfo.external_syms == NULL)
5277 goto error_return;
5278
5279 amt = max_sym_count * sizeof (Elf_Internal_Sym);
5280 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
5281 if (finfo.internal_syms == NULL)
5282 goto error_return;
5283
5284 amt = max_sym_count * sizeof (long);
5285 finfo.indices = (long *) bfd_malloc (amt);
5286 if (finfo.indices == NULL)
5287 goto error_return;
5288
5289 amt = max_sym_count * sizeof (asection *);
5290 finfo.sections = (asection **) bfd_malloc (amt);
5291 if (finfo.sections == NULL)
5292 goto error_return;
5293 }
5294
5295 if (max_sym_shndx_count != 0)
5296 {
5297 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
5298 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
5299 if (finfo.locsym_shndx == NULL)
5300 goto error_return;
5301 }
252b5132
RH
5302
5303 /* Since ELF permits relocations to be against local symbols, we
5304 must have the local symbols available when we do the relocations.
5305 Since we would rather only read the local symbols once, and we
5306 would rather not keep them in memory, we handle all the
5307 relocations for a single input file at the same time.
5308
5309 Unfortunately, there is no way to know the total number of local
5310 symbols until we have seen all of them, and the local symbol
5311 indices precede the global symbol indices. This means that when
5312 we are generating relocateable output, and we see a reloc against
5313 a global symbol, we can not know the symbol index until we have
5314 finished examining all the local symbols to see which ones we are
5315 going to output. To deal with this, we keep the relocations in
5316 memory, and don't output them until the end of the link. This is
5317 an unfortunate waste of memory, but I don't see a good way around
5318 it. Fortunately, it only happens when performing a relocateable
5319 link, which is not the common case. FIXME: If keep_memory is set
5320 we could write the relocs out and then read them again; I don't
5321 know how bad the memory loss will be. */
5322
5323 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5324 sub->output_has_begun = false;
5325 for (o = abfd->sections; o != NULL; o = o->next)
5326 {
5327 for (p = o->link_order_head; p != NULL; p = p->next)
5328 {
c119f9b8
AM
5329 Elf_Internal_Shdr *rhdr;
5330
252b5132 5331 if (p->type == bfd_indirect_link_order
c119f9b8 5332 && (bfd_get_flavour (p->u.indirect.section->owner)
4ddafab0 5333 == bfd_target_elf_flavour)
c119f9b8
AM
5334 && (((rhdr = &elf_section_data (p->u.indirect.section)->rel_hdr)
5335 ->sh_entsize == 0)
5336 || rhdr->sh_entsize == sizeof (Elf_External_Rel)
5337 || rhdr->sh_entsize == sizeof (Elf_External_Rela))
5338 && (((rhdr = elf_section_data (p->u.indirect.section)->rel_hdr2)
5339 == NULL)
5340 || rhdr->sh_entsize == sizeof (Elf_External_Rel)
5341 || rhdr->sh_entsize == sizeof (Elf_External_Rela)))
252b5132 5342 {
c119f9b8 5343 sub = p->u.indirect.section->owner;
252b5132
RH
5344 if (! sub->output_has_begun)
5345 {
5346 if (! elf_link_input_bfd (&finfo, sub))
5347 goto error_return;
5348 sub->output_has_begun = true;
5349 }
5350 }
5351 else if (p->type == bfd_section_reloc_link_order
5352 || p->type == bfd_symbol_reloc_link_order)
5353 {
5354 if (! elf_reloc_link_order (abfd, info, o, p))
5355 goto error_return;
5356 }
5357 else
5358 {
5359 if (! _bfd_default_link_order (abfd, info, o, p))
5360 goto error_return;
5361 }
5362 }
5363 }
5364
c44233aa
AM
5365 /* Output any global symbols that got converted to local in a
5366 version script or due to symbol visibility. We do this in a
5367 separate step since ELF requires all local symbols to appear
5368 prior to any global symbols. FIXME: We should only do this if
5369 some global symbols were, in fact, converted to become local.
5370 FIXME: Will this work correctly with the Irix 5 linker? */
5371 eoinfo.failed = false;
5372 eoinfo.finfo = &finfo;
5373 eoinfo.localsyms = true;
5374 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5375 (PTR) &eoinfo);
5376 if (eoinfo.failed)
5377 return false;
5378
252b5132 5379 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
5380 with the global symbols. Even if we want to strip everything we
5381 can, we still need to deal with those global symbols that got
3e932841 5382 converted to local in a version script. */
252b5132 5383
30b30c21 5384 /* The sh_info field records the index of the first non local symbol. */
252b5132 5385 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 5386
fc8c40a0
AM
5387 if (dynamic
5388 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
5389 {
5390 Elf_Internal_Sym sym;
5391 Elf_External_Sym *dynsym =
a7b97311 5392 (Elf_External_Sym *) finfo.dynsym_sec->contents;
71a40b32 5393 long last_local = 0;
30b30c21
RH
5394
5395 /* Write out the section symbols for the output sections. */
5396 if (info->shared)
5397 {
5398 asection *s;
5399
5400 sym.st_size = 0;
5401 sym.st_name = 0;
5402 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5403 sym.st_other = 0;
5404
5405 for (s = abfd->sections; s != NULL; s = s->next)
5406 {
5407 int indx;
9ad5cbcf
AM
5408 Elf_External_Sym *dest;
5409
30b30c21
RH
5410 indx = elf_section_data (s)->this_idx;
5411 BFD_ASSERT (indx > 0);
5412 sym.st_shndx = indx;
5413 sym.st_value = s->vma;
9ad5cbcf
AM
5414 dest = dynsym + elf_section_data (s)->dynindx;
5415 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5416 }
5417
5418 last_local = bfd_count_sections (abfd);
5419 }
5420
5421 /* Write out the local dynsyms. */
5422 if (elf_hash_table (info)->dynlocal)
5423 {
5424 struct elf_link_local_dynamic_entry *e;
5425 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
5426 {
318da145 5427 asection *s;
9ad5cbcf 5428 Elf_External_Sym *dest;
30b30c21 5429
b037af20
MM
5430 sym.st_size = e->isym.st_size;
5431 sym.st_other = e->isym.st_other;
5432
1fa0ddb3
RH
5433 /* Copy the internal symbol as is.
5434 Note that we saved a word of storage and overwrote
c44233aa
AM
5435 the original st_name with the dynstr_index. */
5436 sym = e->isym;
30b30c21 5437
c8e5ddc8
AM
5438 if (e->isym.st_shndx != SHN_UNDEF
5439 && (e->isym.st_shndx < SHN_LORESERVE
5440 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
5441 {
5442 s = bfd_section_from_elf_index (e->input_bfd,
5443 e->isym.st_shndx);
5444
5445 sym.st_shndx =
5446 elf_section_data (s->output_section)->this_idx;
5447 sym.st_value = (s->output_section->vma
5448 + s->output_offset
5449 + e->isym.st_value);
5450 }
30b30c21
RH
5451
5452 if (last_local < e->dynindx)
5453 last_local = e->dynindx;
5454
9ad5cbcf
AM
5455 dest = dynsym + e->dynindx;
5456 elf_swap_symbol_out (abfd, &sym, (PTR) dest, (PTR) 0);
30b30c21
RH
5457 }
5458 }
5459
71a40b32
ILT
5460 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
5461 last_local + 1;
30b30c21 5462 }
252b5132
RH
5463
5464 /* We get the global symbols from the hash table. */
5465 eoinfo.failed = false;
5466 eoinfo.localsyms = false;
5467 eoinfo.finfo = &finfo;
5468 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5469 (PTR) &eoinfo);
5470 if (eoinfo.failed)
5471 return false;
5472
587ff49e
RH
5473 /* If backend needs to output some symbols not present in the hash
5474 table, do it now. */
5475 if (bed->elf_backend_output_arch_syms)
5476 {
dc810e39
AM
5477 typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
5478 Elf_Internal_Sym *,
5479 asection *));
5480
5481 if (! ((*bed->elf_backend_output_arch_syms)
5482 (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
587ff49e 5483 return false;
3e932841 5484 }
587ff49e 5485
252b5132
RH
5486 /* Flush all symbols to the file. */
5487 if (! elf_link_flush_output_syms (&finfo))
5488 return false;
5489
5490 /* Now we know the size of the symtab section. */
5491 off += symtab_hdr->sh_size;
5492
5493 /* Finish up and write out the symbol string table (.strtab)
5494 section. */
5495 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5496 /* sh_name was set in prep_headers. */
5497 symstrtab_hdr->sh_type = SHT_STRTAB;
5498 symstrtab_hdr->sh_flags = 0;
5499 symstrtab_hdr->sh_addr = 0;
5500 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
5501 symstrtab_hdr->sh_entsize = 0;
5502 symstrtab_hdr->sh_link = 0;
5503 symstrtab_hdr->sh_info = 0;
5504 /* sh_offset is set just below. */
5505 symstrtab_hdr->sh_addralign = 1;
5506
5507 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
5508 elf_tdata (abfd)->next_file_pos = off;
5509
5510 if (bfd_get_symcount (abfd) > 0)
5511 {
5512 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
5513 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
5514 return false;
5515 }
5516
5517 /* Adjust the relocs to have the correct symbol indices. */
5518 for (o = abfd->sections; o != NULL; o = o->next)
5519 {
252b5132
RH
5520 if ((o->flags & SEC_RELOC) == 0)
5521 continue;
5522
3e932841 5523 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
5524 elf_section_data (o)->rel_count,
5525 elf_section_data (o)->rel_hashes);
5526 if (elf_section_data (o)->rel_hdr2 != NULL)
5527 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
5528 elf_section_data (o)->rel_count2,
3e932841 5529 (elf_section_data (o)->rel_hashes
31367b81 5530 + elf_section_data (o)->rel_count));
252b5132
RH
5531
5532 /* Set the reloc_count field to 0 to prevent write_relocs from
5533 trying to swap the relocs out itself. */
5534 o->reloc_count = 0;
5535 }
5536
db6751f2
JJ
5537 if (dynamic && info->combreloc && dynobj != NULL)
5538 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
5539
252b5132
RH
5540 /* If we are linking against a dynamic object, or generating a
5541 shared library, finish up the dynamic linking information. */
5542 if (dynamic)
5543 {
5544 Elf_External_Dyn *dyncon, *dynconend;
5545
5546 /* Fix up .dynamic entries. */
5547 o = bfd_get_section_by_name (dynobj, ".dynamic");
5548 BFD_ASSERT (o != NULL);
5549
5550 dyncon = (Elf_External_Dyn *) o->contents;
5551 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5552 for (; dyncon < dynconend; dyncon++)
5553 {
5554 Elf_Internal_Dyn dyn;
5555 const char *name;
5556 unsigned int type;
5557
5558 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5559
5560 switch (dyn.d_tag)
5561 {
5562 default:
5563 break;
db6751f2
JJ
5564 case DT_NULL:
5565 if (relativecount > 0 && dyncon + 1 < dynconend)
5566 {
5567 switch (elf_section_data (reldyn)->this_hdr.sh_type)
5568 {
5569 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
5570 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
5571 default: break;
5572 }
5573 if (dyn.d_tag != DT_NULL)
5574 {
5575 dyn.d_un.d_val = relativecount;
5576 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5577 relativecount = 0;
5578 }
5579 }
5580 break;
252b5132 5581 case DT_INIT:
f0c2e336 5582 name = info->init_function;
252b5132
RH
5583 goto get_sym;
5584 case DT_FINI:
f0c2e336 5585 name = info->fini_function;
252b5132
RH
5586 get_sym:
5587 {
5588 struct elf_link_hash_entry *h;
5589
5590 h = elf_link_hash_lookup (elf_hash_table (info), name,
5591 false, false, true);
5592 if (h != NULL
5593 && (h->root.type == bfd_link_hash_defined
5594 || h->root.type == bfd_link_hash_defweak))
5595 {
5596 dyn.d_un.d_val = h->root.u.def.value;
5597 o = h->root.u.def.section;
5598 if (o->output_section != NULL)
5599 dyn.d_un.d_val += (o->output_section->vma
5600 + o->output_offset);
5601 else
5602 {
5603 /* The symbol is imported from another shared
5604 library and does not apply to this one. */
5605 dyn.d_un.d_val = 0;
5606 }
5607
5608 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5609 }
5610 }
5611 break;
5612
30831527
RH
5613 case DT_PREINIT_ARRAYSZ:
5614 name = ".preinit_array";
5615 goto get_size;
5616 case DT_INIT_ARRAYSZ:
5617 name = ".init_array";
5618 goto get_size;
5619 case DT_FINI_ARRAYSZ:
5620 name = ".fini_array";
5621 get_size:
5622 o = bfd_get_section_by_name (abfd, name);
5623 BFD_ASSERT (o != NULL);
25e27870
L
5624 if (o->_raw_size == 0)
5625 (*_bfd_error_handler)
5626 (_("warning: %s section has zero size"), name);
30831527
RH
5627 dyn.d_un.d_val = o->_raw_size;
5628 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5629 break;
5630
5631 case DT_PREINIT_ARRAY:
5632 name = ".preinit_array";
5633 goto get_vma;
5634 case DT_INIT_ARRAY:
5635 name = ".init_array";
5636 goto get_vma;
5637 case DT_FINI_ARRAY:
5638 name = ".fini_array";
5639 goto get_vma;
5640
252b5132
RH
5641 case DT_HASH:
5642 name = ".hash";
5643 goto get_vma;
5644 case DT_STRTAB:
5645 name = ".dynstr";
5646 goto get_vma;
5647 case DT_SYMTAB:
5648 name = ".dynsym";
5649 goto get_vma;
5650 case DT_VERDEF:
5651 name = ".gnu.version_d";
5652 goto get_vma;
5653 case DT_VERNEED:
5654 name = ".gnu.version_r";
5655 goto get_vma;
5656 case DT_VERSYM:
5657 name = ".gnu.version";
5658 get_vma:
5659 o = bfd_get_section_by_name (abfd, name);
5660 BFD_ASSERT (o != NULL);
5661 dyn.d_un.d_ptr = o->vma;
5662 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5663 break;
5664
5665 case DT_REL:
5666 case DT_RELA:
5667 case DT_RELSZ:
5668 case DT_RELASZ:
5669 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5670 type = SHT_REL;
5671 else
5672 type = SHT_RELA;
5673 dyn.d_un.d_val = 0;
9ad5cbcf 5674 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
5675 {
5676 Elf_Internal_Shdr *hdr;
5677
5678 hdr = elf_elfsections (abfd)[i];
5679 if (hdr->sh_type == type
5680 && (hdr->sh_flags & SHF_ALLOC) != 0)
5681 {
5682 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5683 dyn.d_un.d_val += hdr->sh_size;
5684 else
5685 {
5686 if (dyn.d_un.d_val == 0
5687 || hdr->sh_addr < dyn.d_un.d_val)
5688 dyn.d_un.d_val = hdr->sh_addr;
5689 }
5690 }
5691 }
5692 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5693 break;
5694 }
5695 }
5696 }
5697
5698 /* If we have created any dynamic sections, then output them. */
5699 if (dynobj != NULL)
5700 {
5701 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5702 goto error_return;
5703
5704 for (o = dynobj->sections; o != NULL; o = o->next)
5705 {
5706 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
5707 || o->_raw_size == 0
5708 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
5709 continue;
5710 if ((o->flags & SEC_LINKER_CREATED) == 0)
5711 {
5712 /* At this point, we are only interested in sections
c44233aa 5713 created by elf_link_create_dynamic_sections. */
252b5132
RH
5714 continue;
5715 }
5716 if ((elf_section_data (o->output_section)->this_hdr.sh_type
5717 != SHT_STRTAB)
5718 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
5719 {
5720 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
5721 o->contents,
5722 (file_ptr) o->output_offset,
252b5132
RH
5723 o->_raw_size))
5724 goto error_return;
5725 }
5726 else
5727 {
252b5132 5728 /* The contents of the .dynstr section are actually in a
c44233aa 5729 stringtab. */
252b5132
RH
5730 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
5731 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
5732 || ! _bfd_elf_strtab_emit (abfd,
5733 elf_hash_table (info)->dynstr))
252b5132
RH
5734 goto error_return;
5735 }
5736 }
5737 }
5738
5739 /* If we have optimized stabs strings, output them. */
5740 if (elf_hash_table (info)->stab_info != NULL)
5741 {
5742 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
5743 goto error_return;
5744 }
5745
57a72197 5746 if (info->eh_frame_hdr && elf_hash_table (info)->dynobj)
65765700
JJ
5747 {
5748 o = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
5749 ".eh_frame_hdr");
5750 if (o
5751 && (elf_section_data (o)->sec_info_type
5752 == ELF_INFO_TYPE_EH_FRAME_HDR))
5753 {
5754 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, o))
5755 goto error_return;
5756 }
5757 }
5758
252b5132
RH
5759 if (finfo.symstrtab != NULL)
5760 _bfd_stringtab_free (finfo.symstrtab);
5761 if (finfo.contents != NULL)
5762 free (finfo.contents);
5763 if (finfo.external_relocs != NULL)
5764 free (finfo.external_relocs);
5765 if (finfo.internal_relocs != NULL)
5766 free (finfo.internal_relocs);
5767 if (finfo.external_syms != NULL)
5768 free (finfo.external_syms);
9ad5cbcf
AM
5769 if (finfo.locsym_shndx != NULL)
5770 free (finfo.locsym_shndx);
252b5132
RH
5771 if (finfo.internal_syms != NULL)
5772 free (finfo.internal_syms);
5773 if (finfo.indices != NULL)
5774 free (finfo.indices);
5775 if (finfo.sections != NULL)
5776 free (finfo.sections);
5777 if (finfo.symbuf != NULL)
5778 free (finfo.symbuf);
9ad5cbcf
AM
5779 if (finfo.symshndxbuf != NULL)
5780 free (finfo.symbuf);
252b5132
RH
5781 for (o = abfd->sections; o != NULL; o = o->next)
5782 {
5783 if ((o->flags & SEC_RELOC) != 0
5784 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 5785 free (elf_section_data (o)->rel_hashes);
252b5132
RH
5786 }
5787
5788 elf_tdata (abfd)->linker = true;
5789
5790 return true;
5791
5792 error_return:
5793 if (finfo.symstrtab != NULL)
5794 _bfd_stringtab_free (finfo.symstrtab);
5795 if (finfo.contents != NULL)
5796 free (finfo.contents);
5797 if (finfo.external_relocs != NULL)
5798 free (finfo.external_relocs);
5799 if (finfo.internal_relocs != NULL)
5800 free (finfo.internal_relocs);
5801 if (finfo.external_syms != NULL)
5802 free (finfo.external_syms);
9ad5cbcf
AM
5803 if (finfo.locsym_shndx != NULL)
5804 free (finfo.locsym_shndx);
252b5132
RH
5805 if (finfo.internal_syms != NULL)
5806 free (finfo.internal_syms);
5807 if (finfo.indices != NULL)
5808 free (finfo.indices);
5809 if (finfo.sections != NULL)
5810 free (finfo.sections);
5811 if (finfo.symbuf != NULL)
5812 free (finfo.symbuf);
9ad5cbcf
AM
5813 if (finfo.symshndxbuf != NULL)
5814 free (finfo.symbuf);
252b5132
RH
5815 for (o = abfd->sections; o != NULL; o = o->next)
5816 {
5817 if ((o->flags & SEC_RELOC) != 0
5818 && elf_section_data (o)->rel_hashes != NULL)
5819 free (elf_section_data (o)->rel_hashes);
5820 }
5821
5822 return false;
5823}
5824
5825/* Add a symbol to the output symbol table. */
5826
5827static boolean
5828elf_link_output_sym (finfo, name, elfsym, input_sec)
5829 struct elf_final_link_info *finfo;
5830 const char *name;
5831 Elf_Internal_Sym *elfsym;
5832 asection *input_sec;
5833{
9ad5cbcf
AM
5834 Elf_External_Sym *dest;
5835 Elf_External_Sym_Shndx *destshndx;
c44233aa 5836
252b5132
RH
5837 boolean (*output_symbol_hook) PARAMS ((bfd *,
5838 struct bfd_link_info *info,
5839 const char *,
5840 Elf_Internal_Sym *,
5841 asection *));
5842
5843 output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
5844 elf_backend_link_output_symbol_hook;
5845 if (output_symbol_hook != NULL)
5846 {
5847 if (! ((*output_symbol_hook)
5848 (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
5849 return false;
5850 }
5851
5852 if (name == (const char *) NULL || *name == '\0')
5853 elfsym->st_name = 0;
5854 else if (input_sec->flags & SEC_EXCLUDE)
5855 elfsym->st_name = 0;
5856 else
5857 {
5858 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
a7b97311 5859 name, true, false);
252b5132
RH
5860 if (elfsym->st_name == (unsigned long) -1)
5861 return false;
5862 }
5863
5864 if (finfo->symbuf_count >= finfo->symbuf_size)
5865 {
5866 if (! elf_link_flush_output_syms (finfo))
5867 return false;
5868 }
5869
9ad5cbcf
AM
5870 dest = finfo->symbuf + finfo->symbuf_count;
5871 destshndx = finfo->symshndxbuf;
5872 if (destshndx != NULL)
5873 destshndx += finfo->symbuf_count;
5874 elf_swap_symbol_out (finfo->output_bfd, elfsym, (PTR) dest, (PTR) destshndx);
252b5132
RH
5875 ++finfo->symbuf_count;
5876
5877 ++ bfd_get_symcount (finfo->output_bfd);
5878
5879 return true;
5880}
5881
5882/* Flush the output symbols to the file. */
5883
5884static boolean
5885elf_link_flush_output_syms (finfo)
5886 struct elf_final_link_info *finfo;
5887{
5888 if (finfo->symbuf_count > 0)
5889 {
9ad5cbcf 5890 Elf_Internal_Shdr *hdr;
dc810e39
AM
5891 file_ptr pos;
5892 bfd_size_type amt;
252b5132 5893
9ad5cbcf
AM
5894 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5895 pos = hdr->sh_offset + hdr->sh_size;
dc810e39
AM
5896 amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
5897 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5898 || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
252b5132
RH
5899 return false;
5900
9ad5cbcf
AM
5901 hdr->sh_size += amt;
5902
5903 if (finfo->symshndxbuf != NULL)
5904 {
5905 hdr = &elf_tdata (finfo->output_bfd)->symtab_shndx_hdr;
5906 pos = hdr->sh_offset + hdr->sh_size;
5907 amt = finfo->symbuf_count * sizeof (Elf_External_Sym_Shndx);
5908 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5909 || (bfd_bwrite ((PTR) finfo->symshndxbuf, amt, finfo->output_bfd)
5910 != amt))
5911 return false;
5912
5913 hdr->sh_size += amt;
5914 }
252b5132
RH
5915
5916 finfo->symbuf_count = 0;
5917 }
5918
5919 return true;
5920}
5921
f5fa8ca2
JJ
5922/* Adjust all external symbols pointing into SEC_MERGE sections
5923 to reflect the object merging within the sections. */
5924
5925static boolean
5926elf_link_sec_merge_syms (h, data)
5927 struct elf_link_hash_entry *h;
5928 PTR data;
5929{
5930 asection *sec;
5931
5932 if ((h->root.type == bfd_link_hash_defined
5933 || h->root.type == bfd_link_hash_defweak)
5934 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
65765700 5935 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f5fa8ca2
JJ
5936 {
5937 bfd *output_bfd = (bfd *) data;
5938
5939 h->root.u.def.value =
5940 _bfd_merged_section_offset (output_bfd,
5941 &h->root.u.def.section,
65765700 5942 elf_section_data (sec)->sec_info,
f5fa8ca2
JJ
5943 h->root.u.def.value, (bfd_vma) 0);
5944 }
5945
5946 return true;
5947}
5948
252b5132
RH
5949/* Add an external symbol to the symbol table. This is called from
5950 the hash table traversal routine. When generating a shared object,
5951 we go through the symbol table twice. The first time we output
5952 anything that might have been forced to local scope in a version
5953 script. The second time we output the symbols that are still
5954 global symbols. */
5955
5956static boolean
5957elf_link_output_extsym (h, data)
5958 struct elf_link_hash_entry *h;
5959 PTR data;
5960{
5961 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
5962 struct elf_final_link_info *finfo = eoinfo->finfo;
5963 boolean strip;
5964 Elf_Internal_Sym sym;
5965 asection *input_sec;
5966
5967 /* Decide whether to output this symbol in this pass. */
5968 if (eoinfo->localsyms)
5969 {
5970 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5971 return true;
5972 }
5973 else
5974 {
5975 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5976 return true;
5977 }
5978
5979 /* If we are not creating a shared library, and this symbol is
5980 referenced by a shared library but is not defined anywhere, then
5981 warn that it is undefined. If we do not do this, the runtime
5982 linker will complain that the symbol is undefined when the
5983 program is run. We don't have to worry about symbols that are
5984 referenced by regular files, because we will already have issued
5985 warnings for them. */
5986 if (! finfo->info->relocateable
b79e8c78 5987 && ! finfo->info->allow_shlib_undefined
e45bf863 5988 && ! finfo->info->shared
252b5132
RH
5989 && h->root.type == bfd_link_hash_undefined
5990 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5991 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5992 {
5993 if (! ((*finfo->info->callbacks->undefined_symbol)
5994 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
dc810e39 5995 (asection *) NULL, (bfd_vma) 0, true)))
252b5132
RH
5996 {
5997 eoinfo->failed = true;
5998 return false;
5999 }
6000 }
6001
6002 /* We don't want to output symbols that have never been mentioned by
6003 a regular file, or that we have been told to strip. However, if
6004 h->indx is set to -2, the symbol is used by a reloc and we must
6005 output it. */
6006 if (h->indx == -2)
6007 strip = false;
6008 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6009 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6010 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6011 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6012 strip = true;
6013 else if (finfo->info->strip == strip_all
6014 || (finfo->info->strip == strip_some
6015 && bfd_hash_lookup (finfo->info->keep_hash,
6016 h->root.root.string,
6017 false, false) == NULL))
6018 strip = true;
6019 else
6020 strip = false;
6021
6022 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
6023 nothing else to do unless it is a forced local symbol. */
6024 if (strip
6025 && h->dynindx == -1
6026 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
6027 return true;
6028
6029 sym.st_value = 0;
6030 sym.st_size = h->size;
6031 sym.st_other = h->other;
6032 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6033 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6034 else if (h->root.type == bfd_link_hash_undefweak
6035 || h->root.type == bfd_link_hash_defweak)
6036 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6037 else
6038 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6039
6040 switch (h->root.type)
6041 {
6042 default:
6043 case bfd_link_hash_new:
6044 abort ();
6045 return false;
6046
6047 case bfd_link_hash_undefined:
252b5132
RH
6048 case bfd_link_hash_undefweak:
6049 input_sec = bfd_und_section_ptr;
6050 sym.st_shndx = SHN_UNDEF;
6051 break;
6052
6053 case bfd_link_hash_defined:
6054 case bfd_link_hash_defweak:
6055 {
6056 input_sec = h->root.u.def.section;
6057 if (input_sec->output_section != NULL)
6058 {
6059 sym.st_shndx =
6060 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6061 input_sec->output_section);
9ad5cbcf 6062 if (sym.st_shndx == SHN_BAD)
252b5132
RH
6063 {
6064 (*_bfd_error_handler)
6065 (_("%s: could not find output section %s for input section %s"),
6066 bfd_get_filename (finfo->output_bfd),
6067 input_sec->output_section->name,
6068 input_sec->name);
6069 eoinfo->failed = true;
6070 return false;
6071 }
6072
6073 /* ELF symbols in relocateable files are section relative,
6074 but in nonrelocateable files they are virtual
6075 addresses. */
6076 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6077 if (! finfo->info->relocateable)
6078 sym.st_value += input_sec->output_section->vma;
6079 }
6080 else
6081 {
6082 BFD_ASSERT (input_sec->owner == NULL
6083 || (input_sec->owner->flags & DYNAMIC) != 0);
6084 sym.st_shndx = SHN_UNDEF;
6085 input_sec = bfd_und_section_ptr;
6086 }
6087 }
6088 break;
6089
6090 case bfd_link_hash_common:
6091 input_sec = h->root.u.c.p->section;
6092 sym.st_shndx = SHN_COMMON;
6093 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6094 break;
6095
6096 case bfd_link_hash_indirect:
6097 /* These symbols are created by symbol versioning. They point
c44233aa
AM
6098 to the decorated version of the name. For example, if the
6099 symbol foo@@GNU_1.2 is the default, which should be used when
6100 foo is used with no version, then we add an indirect symbol
6101 foo which points to foo@@GNU_1.2. We ignore these symbols,
6102 since the indirected symbol is already in the hash table. */
94b6c40a 6103 return true;
252b5132 6104
252b5132
RH
6105 case bfd_link_hash_warning:
6106 /* We can't represent these symbols in ELF, although a warning
c44233aa
AM
6107 symbol may have come from a .gnu.warning.SYMBOL section. We
6108 just put the target symbol in the hash table. If the target
6109 symbol does not really exist, don't do anything. */
252b5132
RH
6110 if (h->root.u.i.link->type == bfd_link_hash_new)
6111 return true;
6112 return (elf_link_output_extsym
6113 ((struct elf_link_hash_entry *) h->root.u.i.link, data));
6114 }
6115
6116 /* Give the processor backend a chance to tweak the symbol value,
6117 and also to finish up anything that needs to be done for this
c44233aa
AM
6118 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6119 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
6120 if ((h->dynindx != -1
6121 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
c44233aa
AM
6122 && (finfo->info->shared
6123 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
6124 && elf_hash_table (finfo->info)->dynamic_sections_created)
6125 {
6126 struct elf_backend_data *bed;
6127
6128 bed = get_elf_backend_data (finfo->output_bfd);
6129 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6130 (finfo->output_bfd, finfo->info, h, &sym)))
6131 {
6132 eoinfo->failed = true;
6133 return false;
6134 }
6135 }
6136
6137 /* If we are marking the symbol as undefined, and there are no
6138 non-weak references to this symbol from a regular object, then
91d3970e
ILT
6139 mark the symbol as weak undefined; if there are non-weak
6140 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
6141 because it might not be marked as undefined until the
6142 finish_dynamic_symbol routine gets through with it. */
6143 if (sym.st_shndx == SHN_UNDEF
252b5132 6144 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
6145 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6146 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
6147 {
6148 int bindtype;
6149
6150 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6151 bindtype = STB_GLOBAL;
6152 else
6153 bindtype = STB_WEAK;
6154 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6155 }
252b5132 6156
32c092c3 6157 /* If a symbol is not defined locally, we clear the visibility
3e932841 6158 field. */
2cd533b7
L
6159 if (! finfo->info->relocateable
6160 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
a7b97311 6161 sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
32c092c3 6162
252b5132
RH
6163 /* If this symbol should be put in the .dynsym section, then put it
6164 there now. We have already know the symbol index. We also fill
6165 in the entry in the .hash section. */
6166 if (h->dynindx != -1
6167 && elf_hash_table (finfo->info)->dynamic_sections_created)
6168 {
6169 size_t bucketcount;
6170 size_t bucket;
c7ac6ff8 6171 size_t hash_entry_size;
252b5132
RH
6172 bfd_byte *bucketpos;
6173 bfd_vma chain;
dc810e39 6174 Elf_External_Sym *esym;
252b5132
RH
6175
6176 sym.st_name = h->dynstr_index;
dc810e39 6177 esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
9ad5cbcf 6178 elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym, (PTR) 0);
252b5132
RH
6179
6180 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6181 bucket = h->elf_hash_value % bucketcount;
3e932841 6182 hash_entry_size
c7ac6ff8 6183 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 6184 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
6185 + (bucket + 2) * hash_entry_size);
6186 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
dc810e39
AM
6187 bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
6188 bucketpos);
c7ac6ff8
MM
6189 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6190 ((bfd_byte *) finfo->hash_sec->contents
6191 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
6192
6193 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6194 {
6195 Elf_Internal_Versym iversym;
dc810e39 6196 Elf_External_Versym *eversym;
252b5132
RH
6197
6198 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6199 {
6200 if (h->verinfo.verdef == NULL)
6201 iversym.vs_vers = 0;
6202 else
6203 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6204 }
6205 else
6206 {
6207 if (h->verinfo.vertree == NULL)
6208 iversym.vs_vers = 1;
6209 else
6210 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6211 }
6212
6213 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6214 iversym.vs_vers |= VERSYM_HIDDEN;
6215
dc810e39
AM
6216 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6217 eversym += h->dynindx;
6218 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
6219 }
6220 }
6221
6222 /* If we're stripping it, then it was just a dynamic symbol, and
6223 there's nothing else to do. */
6224 if (strip)
6225 return true;
6226
6227 h->indx = bfd_get_symcount (finfo->output_bfd);
6228
6229 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
6230 {
6231 eoinfo->failed = true;
6232 return false;
6233 }
6234
6235 return true;
6236}
6237
23bc299b
MM
6238/* Copy the relocations indicated by the INTERNAL_RELOCS (which
6239 originated from the section given by INPUT_REL_HDR) to the
6240 OUTPUT_BFD. */
6241
6242static void
3e932841 6243elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
23bc299b
MM
6244 internal_relocs)
6245 bfd *output_bfd;
6246 asection *input_section;
6247 Elf_Internal_Shdr *input_rel_hdr;
6248 Elf_Internal_Rela *internal_relocs;
6249{
6250 Elf_Internal_Rela *irela;
6251 Elf_Internal_Rela *irelaend;
6252 Elf_Internal_Shdr *output_rel_hdr;
6253 asection *output_section;
7442e600 6254 unsigned int *rel_countp = NULL;
32f0787a 6255 struct elf_backend_data *bed;
dc810e39 6256 bfd_size_type amt;
23bc299b
MM
6257
6258 output_section = input_section->output_section;
6259 output_rel_hdr = NULL;
6260
3e932841 6261 if (elf_section_data (output_section)->rel_hdr.sh_entsize
23bc299b
MM
6262 == input_rel_hdr->sh_entsize)
6263 {
6264 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
6265 rel_countp = &elf_section_data (output_section)->rel_count;
6266 }
6267 else if (elf_section_data (output_section)->rel_hdr2
6268 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
6269 == input_rel_hdr->sh_entsize))
6270 {
6271 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
6272 rel_countp = &elf_section_data (output_section)->rel_count2;
6273 }
6274
6275 BFD_ASSERT (output_rel_hdr != NULL);
32f0787a
UC
6276
6277 bed = get_elf_backend_data (output_bfd);
23bc299b 6278 irela = internal_relocs;
209f668e
NC
6279 irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
6280 * bed->s->int_rels_per_ext_rel;
6281
23bc299b
MM
6282 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6283 {
6284 Elf_External_Rel *erel;
209f668e 6285 Elf_Internal_Rel *irel;
dc810e39
AM
6286
6287 amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
6288 irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
209f668e
NC
6289 if (irel == NULL)
6290 {
6291 (*_bfd_error_handler) (_("Error: out of memory"));
6292 abort ();
6293 }
23bc299b
MM
6294
6295 erel = ((Elf_External_Rel *) output_rel_hdr->contents + *rel_countp);
209f668e 6296 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
23bc299b 6297 {
4e8a9624 6298 unsigned int i;
dc810e39 6299
209f668e
NC
6300 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
6301 {
6302 irel[i].r_offset = irela[i].r_offset;
6303 irel[i].r_info = irela[i].r_info;
6304 BFD_ASSERT (irela[i].r_addend == 0);
6305 }
23bc299b 6306
32f0787a 6307 if (bed->s->swap_reloc_out)
209f668e 6308 (*bed->s->swap_reloc_out) (output_bfd, irel, (PTR) erel);
32f0787a 6309 else
209f668e 6310 elf_swap_reloc_out (output_bfd, irel, erel);
23bc299b 6311 }
209f668e
NC
6312
6313 free (irel);
23bc299b
MM
6314 }
6315 else
6316 {
6317 Elf_External_Rela *erela;
6318
209f668e
NC
6319 BFD_ASSERT (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
6320
23bc299b 6321 erela = ((Elf_External_Rela *) output_rel_hdr->contents + *rel_countp);
209f668e 6322 for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erela++)
32f0787a
UC
6323 if (bed->s->swap_reloca_out)
6324 (*bed->s->swap_reloca_out) (output_bfd, irela, (PTR) erela);
6325 else
6326 elf_swap_reloca_out (output_bfd, irela, erela);
23bc299b
MM
6327 }
6328
6329 /* Bump the counter, so that we know where to add the next set of
6330 relocations. */
d9bc7a44 6331 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
23bc299b
MM
6332}
6333
252b5132
RH
6334/* Link an input file into the linker output file. This function
6335 handles all the sections and relocations of the input file at once.
6336 This is so that we only have to read the local symbols once, and
6337 don't have to keep them in memory. */
6338
6339static boolean
6340elf_link_input_bfd (finfo, input_bfd)
6341 struct elf_final_link_info *finfo;
6342 bfd *input_bfd;
6343{
6344 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
6345 bfd *, asection *, bfd_byte *,
6346 Elf_Internal_Rela *,
6347 Elf_Internal_Sym *, asection **));
6348 bfd *output_bfd;
6349 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 6350 Elf_Internal_Shdr *shndx_hdr;
252b5132
RH
6351 size_t locsymcount;
6352 size_t extsymoff;
6353 Elf_External_Sym *external_syms;
6354 Elf_External_Sym *esym;
6355 Elf_External_Sym *esymend;
9ad5cbcf
AM
6356 Elf_External_Sym_Shndx *shndx_buf;
6357 Elf_External_Sym_Shndx *shndx;
252b5132
RH
6358 Elf_Internal_Sym *isym;
6359 long *pindex;
6360 asection **ppsection;
6361 asection *o;
c7ac6ff8 6362 struct elf_backend_data *bed;
9317eacc 6363 boolean emit_relocs;
f8deed93 6364 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
6365
6366 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
6367 bed = get_elf_backend_data (output_bfd);
6368 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
6369
6370 /* If this is a dynamic object, we don't want to do anything here:
6371 we don't want the local symbols, and we don't want the section
6372 contents. */
6373 if ((input_bfd->flags & DYNAMIC) != 0)
6374 return true;
6375
9317eacc 6376 emit_relocs = (finfo->info->relocateable
c44233aa
AM
6377 || finfo->info->emitrelocations
6378 || bed->elf_backend_emit_relocs);
9317eacc 6379
252b5132
RH
6380 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6381 if (elf_bad_symtab (input_bfd))
6382 {
6383 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
6384 extsymoff = 0;
6385 }
6386 else
6387 {
6388 locsymcount = symtab_hdr->sh_info;
6389 extsymoff = symtab_hdr->sh_info;
6390 }
6391
6392 /* Read the local symbols. */
6393 if (symtab_hdr->contents != NULL)
6394 external_syms = (Elf_External_Sym *) symtab_hdr->contents;
6395 else if (locsymcount == 0)
6396 external_syms = NULL;
6397 else
6398 {
dc810e39 6399 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
252b5132
RH
6400 external_syms = finfo->external_syms;
6401 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
dc810e39 6402 || bfd_bread (external_syms, amt, input_bfd) != amt)
252b5132
RH
6403 return false;
6404 }
6405
9ad5cbcf
AM
6406 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
6407 shndx_buf = NULL;
6408 if (shndx_hdr->sh_size != 0 && locsymcount != 0)
6409 {
6410 bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
6411 shndx_buf = finfo->locsym_shndx;
6412 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
6413 || bfd_bread (shndx_buf, amt, input_bfd) != amt)
6414 return false;
6415 }
6416
252b5132
RH
6417 /* Swap in the local symbols and write out the ones which we know
6418 are going into the output file. */
9ad5cbcf
AM
6419 for (esym = external_syms, esymend = esym + locsymcount,
6420 isym = finfo->internal_syms, pindex = finfo->indices,
6421 ppsection = finfo->sections, shndx = shndx_buf;
6422 esym < esymend;
6423 esym++, isym++, pindex++, ppsection++,
6424 shndx = (shndx != NULL ? shndx + 1 : NULL))
252b5132
RH
6425 {
6426 asection *isec;
6427 const char *name;
6428 Elf_Internal_Sym osym;
6429
9ad5cbcf 6430 elf_swap_symbol_in (input_bfd, esym, shndx, isym);
252b5132
RH
6431 *pindex = -1;
6432
6433 if (elf_bad_symtab (input_bfd))
6434 {
6435 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6436 {
6437 *ppsection = NULL;
6438 continue;
6439 }
6440 }
6441
6442 if (isym->st_shndx == SHN_UNDEF)
862517b6 6443 isec = bfd_und_section_ptr;
9ad5cbcf
AM
6444 else if (isym->st_shndx < SHN_LORESERVE
6445 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2
JJ
6446 {
6447 isec = section_from_elf_index (input_bfd, isym->st_shndx);
65765700
JJ
6448 if (isec
6449 && elf_section_data (isec)->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
6450 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6451 isym->st_value =
6452 _bfd_merged_section_offset (output_bfd, &isec,
65765700 6453 elf_section_data (isec)->sec_info,
f5fa8ca2
JJ
6454 isym->st_value, (bfd_vma) 0);
6455 }
252b5132 6456 else if (isym->st_shndx == SHN_ABS)
862517b6 6457 isec = bfd_abs_section_ptr;
252b5132 6458 else if (isym->st_shndx == SHN_COMMON)
862517b6 6459 isec = bfd_com_section_ptr;
252b5132
RH
6460 else
6461 {
6462 /* Who knows? */
6463 isec = NULL;
6464 }
6465
6466 *ppsection = isec;
6467
6468 /* Don't output the first, undefined, symbol. */
6469 if (esym == external_syms)
6470 continue;
6471
24376d1b
AM
6472 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6473 {
24376d1b
AM
6474 /* We never output section symbols. Instead, we use the
6475 section symbol of the corresponding section in the output
6476 file. */
6477 continue;
6478 }
6479
252b5132
RH
6480 /* If we are stripping all symbols, we don't want to output this
6481 one. */
6482 if (finfo->info->strip == strip_all)
6483 continue;
6484
252b5132
RH
6485 /* If we are discarding all local symbols, we don't want to
6486 output this one. If we are generating a relocateable output
6487 file, then some of the local symbols may be required by
6488 relocs; we output them below as we discover that they are
6489 needed. */
6490 if (finfo->info->discard == discard_all)
6491 continue;
6492
6493 /* If this symbol is defined in a section which we are
c44233aa
AM
6494 discarding, we don't need to keep it, but note that
6495 linker_mark is only reliable for sections that have contents.
6496 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6497 as well as linker_mark. */
9ad5cbcf 6498 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
6499 && isec != NULL
6500 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6501 || (! finfo->info->relocateable
6502 && (isec->flags & SEC_EXCLUDE) != 0)))
6503 continue;
6504
6505 /* Get the name of the symbol. */
6506 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6507 isym->st_name);
6508 if (name == NULL)
6509 return false;
6510
6511 /* See if we are discarding symbols with this name. */
6512 if ((finfo->info->strip == strip_some
6513 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
6514 == NULL))
f5fa8ca2
JJ
6515 || (((finfo->info->discard == discard_sec_merge
6516 && (isec->flags & SEC_MERGE) && ! finfo->info->relocateable)
6517 || finfo->info->discard == discard_l)
252b5132
RH
6518 && bfd_is_local_label_name (input_bfd, name)))
6519 continue;
6520
6521 /* If we get here, we are going to output this symbol. */
6522
6523 osym = *isym;
6524
6525 /* Adjust the section index for the output file. */
6526 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6527 isec->output_section);
9ad5cbcf 6528 if (osym.st_shndx == SHN_BAD)
252b5132
RH
6529 return false;
6530
6531 *pindex = bfd_get_symcount (output_bfd);
6532
6533 /* ELF symbols in relocateable files are section relative, but
6534 in executable files they are virtual addresses. Note that
6535 this code assumes that all ELF sections have an associated
6536 BFD section with a reasonable value for output_offset; below
6537 we assume that they also have a reasonable value for
6538 output_section. Any special sections must be set up to meet
6539 these requirements. */
6540 osym.st_value += isec->output_offset;
6541 if (! finfo->info->relocateable)
6542 osym.st_value += isec->output_section->vma;
6543
6544 if (! elf_link_output_sym (finfo, name, &osym, isec))
6545 return false;
6546 }
6547
6548 /* Relocate the contents of each section. */
f8deed93 6549 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
6550 for (o = input_bfd->sections; o != NULL; o = o->next)
6551 {
6552 bfd_byte *contents;
6553
6554 if (! o->linker_mark)
6555 {
6556 /* This section was omitted from the link. */
6557 continue;
6558 }
6559
6560 if ((o->flags & SEC_HAS_CONTENTS) == 0
6561 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
6562 continue;
6563
6564 if ((o->flags & SEC_LINKER_CREATED) != 0)
6565 {
6566 /* Section was created by elf_link_create_dynamic_sections
6567 or somesuch. */
6568 continue;
6569 }
6570
6571 /* Get the contents of the section. They have been cached by a
c44233aa
AM
6572 relaxation routine. Note that o is a section in an input
6573 file, so the contents field will not have been set by any of
6574 the routines which work on output files. */
252b5132
RH
6575 if (elf_section_data (o)->this_hdr.contents != NULL)
6576 contents = elf_section_data (o)->this_hdr.contents;
6577 else
6578 {
6579 contents = finfo->contents;
6580 if (! bfd_get_section_contents (input_bfd, o, contents,
6581 (file_ptr) 0, o->_raw_size))
6582 return false;
6583 }
6584
6585 if ((o->flags & SEC_RELOC) != 0)
6586 {
6587 Elf_Internal_Rela *internal_relocs;
6588
6589 /* Get the swapped relocs. */
6590 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
6591 (input_bfd, o, finfo->external_relocs,
6592 finfo->internal_relocs, false));
6593 if (internal_relocs == NULL
6594 && o->reloc_count > 0)
6595 return false;
6596
ec338859
AM
6597 /* Run through the relocs looking for any against symbols
6598 from discarded sections and section symbols from
6599 removed link-once sections. Complain about relocs
6600 against discarded sections. Zero relocs against removed
6601 link-once sections. We should really complain if
6602 anything in the final link tries to use it, but
6603 DWARF-based exception handling might have an entry in
6604 .eh_frame to describe a routine in the linkonce section,
6605 and it turns out to be hard to remove the .eh_frame
6606 entry too. FIXME. */
73d074b4
DJ
6607 if (!finfo->info->relocateable
6608 && !elf_section_ignore_discarded_relocs (o))
ec338859
AM
6609 {
6610 Elf_Internal_Rela *rel, *relend;
50b4d486 6611
ec338859
AM
6612 rel = internal_relocs;
6613 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6614 for ( ; rel < relend; rel++)
6615 {
6616 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
6617
6618 if (r_symndx >= locsymcount
6619 || (elf_bad_symtab (input_bfd)
6620 && finfo->sections[r_symndx] == NULL))
6621 {
6622 struct elf_link_hash_entry *h;
6623
6624 h = sym_hashes[r_symndx - extsymoff];
6625 while (h->root.type == bfd_link_hash_indirect
6626 || h->root.type == bfd_link_hash_warning)
6627 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6628
6629 /* Complain if the definition comes from a
6630 discarded section. */
6631 if ((h->root.type == bfd_link_hash_defined
6632 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 6633 && elf_discarded_section (h->root.u.def.section))
ec338859 6634 {
f8deed93 6635#if BFD_VERSION_DATE < 20031005
ec338859
AM
6636 if ((o->flags & SEC_DEBUGGING) != 0)
6637 {
f8deed93 6638#if BFD_VERSION_DATE > 20021005
ec338859
AM
6639 (*finfo->info->callbacks->warning)
6640 (finfo->info,
6641 _("warning: relocation against removed section; zeroing"),
6642 NULL, input_bfd, o, rel->r_offset);
f8deed93 6643#endif
45e9217a 6644 BFD_ASSERT (r_symndx != 0);
f8deed93 6645 memset (rel, 0, sizeof (*rel));
ec338859
AM
6646 }
6647 else
f8deed93 6648#endif
ec338859
AM
6649 {
6650 if (! ((*finfo->info->callbacks->undefined_symbol)
6651 (finfo->info, h->root.root.string,
6652 input_bfd, o, rel->r_offset,
6653 true)))
6654 return false;
6655 }
6656 }
6657 }
6658 else
6659 {
f9f32305 6660 asection *sec = finfo->sections[r_symndx];
50b4d486 6661
ed4de5e2 6662 if (sec != NULL && elf_discarded_section (sec))
f9f32305 6663 {
f8deed93 6664#if BFD_VERSION_DATE < 20031005
f9f32305
AM
6665 if ((o->flags & SEC_DEBUGGING) != 0
6666 || (sec->flags & SEC_LINK_ONCE) != 0)
6667 {
50b4d486 6668#if BFD_VERSION_DATE > 20021005
f9f32305
AM
6669 (*finfo->info->callbacks->warning)
6670 (finfo->info,
6671 _("warning: relocation against removed section"),
6672 NULL, input_bfd, o, rel->r_offset);
50b4d486 6673#endif
45e9217a 6674 BFD_ASSERT (r_symndx != 0);
f9f32305
AM
6675 rel->r_info
6676 = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
6677 rel->r_addend = 0;
6678 }
6679 else
f8deed93 6680#endif
f9f32305
AM
6681 {
6682 boolean ok;
6683 const char *msg
6684 = _("local symbols in discarded section %s");
6685 bfd_size_type amt
6686 = strlen (sec->name) + strlen (msg) - 1;
6687 char *buf = (char *) bfd_malloc (amt);
6688
6689 if (buf != NULL)
6690 sprintf (buf, msg, sec->name);
6691 else
6692 buf = (char *) sec->name;
6693 ok = (*finfo->info->callbacks
6694 ->undefined_symbol) (finfo->info, buf,
6695 input_bfd, o,
6696 rel->r_offset,
6697 true);
6698 if (buf != sec->name)
6699 free (buf);
6700 if (!ok)
6701 return false;
ec338859
AM
6702 }
6703 }
6704 }
6705 }
6706 }
50b4d486 6707
252b5132
RH
6708 /* Relocate the section by invoking a back end routine.
6709
6710 The back end routine is responsible for adjusting the
6711 section contents as necessary, and (if using Rela relocs
6712 and generating a relocateable output file) adjusting the
6713 reloc addend as necessary.
6714
6715 The back end routine does not have to worry about setting
6716 the reloc address or the reloc symbol index.
6717
6718 The back end routine is given a pointer to the swapped in
6719 internal symbols, and can access the hash table entries
6720 for the external symbols via elf_sym_hashes (input_bfd).
6721
6722 When generating relocateable output, the back end routine
6723 must handle STB_LOCAL/STT_SECTION symbols specially. The
6724 output symbol is going to be a section symbol
6725 corresponding to the output section, which will require
6726 the addend to be adjusted. */
6727
6728 if (! (*relocate_section) (output_bfd, finfo->info,
6729 input_bfd, o, contents,
6730 internal_relocs,
6731 finfo->internal_syms,
6732 finfo->sections))
6733 return false;
6734
9317eacc 6735 if (emit_relocs)
252b5132
RH
6736 {
6737 Elf_Internal_Rela *irela;
6738 Elf_Internal_Rela *irelaend;
6739 struct elf_link_hash_entry **rel_hash;
6740 Elf_Internal_Shdr *input_rel_hdr;
4e8a9624 6741 unsigned int next_erel;
dc810e39
AM
6742 void (*reloc_emitter) PARAMS ((bfd *, asection *,
6743 Elf_Internal_Shdr *,
6744 Elf_Internal_Rela *));
252b5132
RH
6745
6746 /* Adjust the reloc addresses and symbol indices. */
6747
6748 irela = internal_relocs;
dc810e39 6749 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 6750 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
6751 + elf_section_data (o->output_section)->rel_count
6752 + elf_section_data (o->output_section)->rel_count2);
209f668e 6753 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
6754 {
6755 unsigned long r_symndx;
252b5132
RH
6756 asection *sec;
6757
209f668e
NC
6758 if (next_erel == bed->s->int_rels_per_ext_rel)
6759 {
6760 rel_hash++;
6761 next_erel = 0;
6762 }
6763
252b5132
RH
6764 irela->r_offset += o->output_offset;
6765
7ad34365
NC
6766 /* Relocs in an executable have to be virtual addresses. */
6767 if (finfo->info->emitrelocations)
6768 irela->r_offset += o->output_section->vma;
6769
252b5132
RH
6770 r_symndx = ELF_R_SYM (irela->r_info);
6771
6772 if (r_symndx == 0)
6773 continue;
6774
6775 if (r_symndx >= locsymcount
6776 || (elf_bad_symtab (input_bfd)
6777 && finfo->sections[r_symndx] == NULL))
6778 {
6779 struct elf_link_hash_entry *rh;
209f668e 6780 unsigned long indx;
252b5132
RH
6781
6782 /* This is a reloc against a global symbol. We
6783 have not yet output all the local symbols, so
6784 we do not know the symbol index of any global
6785 symbol. We set the rel_hash entry for this
6786 reloc to point to the global hash table entry
6787 for this symbol. The symbol index is then
6788 set at the end of elf_bfd_final_link. */
6789 indx = r_symndx - extsymoff;
6790 rh = elf_sym_hashes (input_bfd)[indx];
6791 while (rh->root.type == bfd_link_hash_indirect
6792 || rh->root.type == bfd_link_hash_warning)
6793 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6794
6795 /* Setting the index to -2 tells
6796 elf_link_output_extsym that this symbol is
6797 used by a reloc. */
6798 BFD_ASSERT (rh->indx < 0);
6799 rh->indx = -2;
6800
6801 *rel_hash = rh;
6802
6803 continue;
6804 }
6805
3e932841 6806 /* This is a reloc against a local symbol. */
252b5132
RH
6807
6808 *rel_hash = NULL;
6809 isym = finfo->internal_syms + r_symndx;
6810 sec = finfo->sections[r_symndx];
6811 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6812 {
6813 /* I suppose the backend ought to fill in the
6814 section of any STT_SECTION symbol against a
6815 processor specific section. If we have
6816 discarded a section, the output_section will
6817 be the absolute section. */
6818 if (sec != NULL
6819 && (bfd_is_abs_section (sec)
6820 || (sec->output_section != NULL
6821 && bfd_is_abs_section (sec->output_section))))
6822 r_symndx = 0;
6823 else if (sec == NULL || sec->owner == NULL)
6824 {
6825 bfd_set_error (bfd_error_bad_value);
6826 return false;
6827 }
6828 else
6829 {
6830 r_symndx = sec->output_section->target_index;
6831 BFD_ASSERT (r_symndx != 0);
6832 }
6833 }
6834 else
6835 {
6836 if (finfo->indices[r_symndx] == -1)
6837 {
dc810e39 6838 unsigned long shlink;
252b5132
RH
6839 const char *name;
6840 asection *osec;
6841
6842 if (finfo->info->strip == strip_all)
6843 {
6844 /* You can't do ld -r -s. */
6845 bfd_set_error (bfd_error_invalid_operation);
6846 return false;
6847 }
6848
6849 /* This symbol was skipped earlier, but
6850 since it is needed by a reloc, we
6851 must output it now. */
dc810e39 6852 shlink = symtab_hdr->sh_link;
a7b97311 6853 name = (bfd_elf_string_from_elf_section
dc810e39 6854 (input_bfd, shlink, isym->st_name));
252b5132
RH
6855 if (name == NULL)
6856 return false;
6857
6858 osec = sec->output_section;
6859 isym->st_shndx =
6860 _bfd_elf_section_from_bfd_section (output_bfd,
6861 osec);
9ad5cbcf 6862 if (isym->st_shndx == SHN_BAD)
252b5132
RH
6863 return false;
6864
6865 isym->st_value += sec->output_offset;
6866 if (! finfo->info->relocateable)
6867 isym->st_value += osec->vma;
6868
a7b97311
AM
6869 finfo->indices[r_symndx]
6870 = bfd_get_symcount (output_bfd);
252b5132
RH
6871
6872 if (! elf_link_output_sym (finfo, name, isym, sec))
6873 return false;
6874 }
6875
6876 r_symndx = finfo->indices[r_symndx];
6877 }
6878
6879 irela->r_info = ELF_R_INFO (r_symndx,
6880 ELF_R_TYPE (irela->r_info));
6881 }
6882
6883 /* Swap out the relocs. */
c44233aa
AM
6884 if (bed->elf_backend_emit_relocs
6885 && !(finfo->info->relocateable
a7b97311 6886 || finfo->info->emitrelocations))
c44233aa
AM
6887 reloc_emitter = bed->elf_backend_emit_relocs;
6888 else
6889 reloc_emitter = elf_link_output_relocs;
9317eacc 6890
252b5132 6891 input_rel_hdr = &elf_section_data (o)->rel_hdr;
c44233aa 6892 (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
9317eacc 6893
23bc299b 6894 input_rel_hdr = elf_section_data (o)->rel_hdr2;
c44233aa
AM
6895 if (input_rel_hdr)
6896 {
6897 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 6898 * bed->s->int_rels_per_ext_rel);
c44233aa
AM
6899 reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
6900 }
9317eacc 6901
252b5132
RH
6902 }
6903 }
6904
6905 /* Write out the modified section contents. */
73d074b4 6906 if (bed->elf_backend_write_section
f9f32305 6907 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
6908 {
6909 /* Section written out. */
6910 }
65765700 6911 else switch (elf_section_data (o)->sec_info_type)
f5fa8ca2 6912 {
65765700 6913 case ELF_INFO_TYPE_STABS:
f5fa8ca2 6914 if (! (_bfd_write_section_stabs
65765700
JJ
6915 (output_bfd,
6916 &elf_hash_table (finfo->info)->stab_info,
6917 o, &elf_section_data (o)->sec_info, contents)))
f5fa8ca2 6918 return false;
65765700
JJ
6919 break;
6920 case ELF_INFO_TYPE_MERGE:
f5fa8ca2 6921 if (! (_bfd_write_merged_section
65765700 6922 (output_bfd, o, elf_section_data (o)->sec_info)))
252b5132 6923 return false;
65765700
JJ
6924 break;
6925 case ELF_INFO_TYPE_EH_FRAME:
6926 {
6927 asection *ehdrsec;
6928
6929 ehdrsec
6930 = bfd_get_section_by_name (elf_hash_table (finfo->info)->dynobj,
6931 ".eh_frame_hdr");
6932 if (! (_bfd_elf_write_section_eh_frame (output_bfd, o, ehdrsec,
6933 contents)))
6934 return false;
6935 }
6936 break;
6937 default:
6938 {
6939 bfd_size_type sec_size;
6940
6941 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
6942 if (! (o->flags & SEC_EXCLUDE)
6943 && ! bfd_set_section_contents (output_bfd, o->output_section,
6944 contents,
6945 (file_ptr) o->output_offset,
6946 sec_size))
6947 return false;
6948 }
6949 break;
252b5132
RH
6950 }
6951 }
6952
6953 return true;
6954}
6955
6956/* Generate a reloc when linking an ELF file. This is a reloc
6957 requested by the linker, and does come from any input file. This
6958 is used to build constructor and destructor tables when linking
6959 with -Ur. */
6960
6961static boolean
6962elf_reloc_link_order (output_bfd, info, output_section, link_order)
6963 bfd *output_bfd;
6964 struct bfd_link_info *info;
6965 asection *output_section;
6966 struct bfd_link_order *link_order;
6967{
6968 reloc_howto_type *howto;
6969 long indx;
6970 bfd_vma offset;
6971 bfd_vma addend;
6972 struct elf_link_hash_entry **rel_hash_ptr;
6973 Elf_Internal_Shdr *rel_hdr;
32f0787a 6974 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
252b5132
RH
6975
6976 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6977 if (howto == NULL)
6978 {
6979 bfd_set_error (bfd_error_bad_value);
6980 return false;
6981 }
6982
6983 addend = link_order->u.reloc.p->addend;
6984
6985 /* Figure out the symbol index. */
6986 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
6987 + elf_section_data (output_section)->rel_count
6988 + elf_section_data (output_section)->rel_count2);
252b5132
RH
6989 if (link_order->type == bfd_section_reloc_link_order)
6990 {
6991 indx = link_order->u.reloc.p->u.section->target_index;
6992 BFD_ASSERT (indx != 0);
6993 *rel_hash_ptr = NULL;
6994 }
6995 else
6996 {
6997 struct elf_link_hash_entry *h;
6998
6999 /* Treat a reloc against a defined symbol as though it were
c44233aa 7000 actually against the section. */
252b5132
RH
7001 h = ((struct elf_link_hash_entry *)
7002 bfd_wrapped_link_hash_lookup (output_bfd, info,
7003 link_order->u.reloc.p->u.name,
7004 false, false, true));
7005 if (h != NULL
7006 && (h->root.type == bfd_link_hash_defined
7007 || h->root.type == bfd_link_hash_defweak))
7008 {
7009 asection *section;
7010
7011 section = h->root.u.def.section;
7012 indx = section->output_section->target_index;
7013 *rel_hash_ptr = NULL;
7014 /* It seems that we ought to add the symbol value to the
c44233aa
AM
7015 addend here, but in practice it has already been added
7016 because it was passed to constructor_callback. */
252b5132
RH
7017 addend += section->output_section->vma + section->output_offset;
7018 }
7019 else if (h != NULL)
7020 {
7021 /* Setting the index to -2 tells elf_link_output_extsym that
7022 this symbol is used by a reloc. */
7023 h->indx = -2;
7024 *rel_hash_ptr = h;
7025 indx = 0;
7026 }
7027 else
7028 {
7029 if (! ((*info->callbacks->unattached_reloc)
7030 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
7031 (asection *) NULL, (bfd_vma) 0)))
7032 return false;
7033 indx = 0;
7034 }
7035 }
7036
7037 /* If this is an inplace reloc, we must write the addend into the
7038 object file. */
7039 if (howto->partial_inplace && addend != 0)
7040 {
7041 bfd_size_type size;
7042 bfd_reloc_status_type rstat;
7043 bfd_byte *buf;
7044 boolean ok;
dc810e39 7045 const char *sym_name;
252b5132
RH
7046
7047 size = bfd_get_reloc_size (howto);
7048 buf = (bfd_byte *) bfd_zmalloc (size);
7049 if (buf == (bfd_byte *) NULL)
7050 return false;
dc810e39 7051 rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
252b5132
RH
7052 switch (rstat)
7053 {
7054 case bfd_reloc_ok:
7055 break;
dc810e39 7056
252b5132
RH
7057 default:
7058 case bfd_reloc_outofrange:
7059 abort ();
dc810e39 7060
252b5132 7061 case bfd_reloc_overflow:
dc810e39
AM
7062 if (link_order->type == bfd_section_reloc_link_order)
7063 sym_name = bfd_section_name (output_bfd,
7064 link_order->u.reloc.p->u.section);
7065 else
7066 sym_name = link_order->u.reloc.p->u.name;
252b5132 7067 if (! ((*info->callbacks->reloc_overflow)
dc810e39
AM
7068 (info, sym_name, howto->name, addend,
7069 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
252b5132
RH
7070 {
7071 free (buf);
7072 return false;
7073 }
7074 break;
7075 }
7076 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
7077 (file_ptr) link_order->offset, size);
7078 free (buf);
7079 if (! ok)
7080 return false;
7081 }
7082
7083 /* The address of a reloc is relative to the section in a
7084 relocateable file, and is a virtual address in an executable
7085 file. */
7086 offset = link_order->offset;
7087 if (! info->relocateable)
7088 offset += output_section->vma;
7089
7090 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7091
7092 if (rel_hdr->sh_type == SHT_REL)
7093 {
dc810e39 7094 bfd_size_type size;
209f668e 7095 Elf_Internal_Rel *irel;
252b5132 7096 Elf_External_Rel *erel;
4e8a9624 7097 unsigned int i;
dc810e39
AM
7098
7099 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
7100 irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
209f668e
NC
7101 if (irel == NULL)
7102 return false;
dc810e39 7103
209f668e
NC
7104 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7105 irel[i].r_offset = offset;
7106 irel[0].r_info = ELF_R_INFO (indx, howto->type);
252b5132 7107
252b5132 7108 erel = ((Elf_External_Rel *) rel_hdr->contents
0525d26e 7109 + elf_section_data (output_section)->rel_count);
209f668e 7110
32f0787a 7111 if (bed->s->swap_reloc_out)
209f668e 7112 (*bed->s->swap_reloc_out) (output_bfd, irel, (bfd_byte *) erel);
32f0787a 7113 else
209f668e
NC
7114 elf_swap_reloc_out (output_bfd, irel, erel);
7115
7116 free (irel);
252b5132
RH
7117 }
7118 else
7119 {
dc810e39 7120 bfd_size_type size;
209f668e 7121 Elf_Internal_Rela *irela;
252b5132 7122 Elf_External_Rela *erela;
4e8a9624 7123 unsigned int i;
dc810e39
AM
7124
7125 size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
7126 irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
209f668e
NC
7127 if (irela == NULL)
7128 return false;
7129
7130 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7131 irela[i].r_offset = offset;
7132 irela[0].r_info = ELF_R_INFO (indx, howto->type);
7133 irela[0].r_addend = addend;
252b5132 7134
252b5132 7135 erela = ((Elf_External_Rela *) rel_hdr->contents
0525d26e 7136 + elf_section_data (output_section)->rel_count);
209f668e 7137
32f0787a 7138 if (bed->s->swap_reloca_out)
209f668e 7139 (*bed->s->swap_reloca_out) (output_bfd, irela, (bfd_byte *) erela);
32f0787a 7140 else
209f668e 7141 elf_swap_reloca_out (output_bfd, irela, erela);
252b5132
RH
7142 }
7143
0525d26e 7144 ++elf_section_data (output_section)->rel_count;
252b5132
RH
7145
7146 return true;
7147}
252b5132
RH
7148\f
7149/* Allocate a pointer to live in a linker created section. */
7150
7151boolean
7152elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
7153 bfd *abfd;
7154 struct bfd_link_info *info;
7155 elf_linker_section_t *lsect;
7156 struct elf_link_hash_entry *h;
7157 const Elf_Internal_Rela *rel;
7158{
7159 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
7160 elf_linker_section_pointers_t *linker_section_ptr;
dc810e39
AM
7161 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7162 bfd_size_type amt;
252b5132
RH
7163
7164 BFD_ASSERT (lsect != NULL);
7165
a7b97311 7166 /* Is this a global symbol? */
252b5132
RH
7167 if (h != NULL)
7168 {
a7b97311 7169 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7170 if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
7171 rel->r_addend,
7172 lsect->which))
7173 return true;
7174
7175 ptr_linker_section_ptr = &h->linker_section_pointer;
7176 /* Make sure this symbol is output as a dynamic symbol. */
7177 if (h->dynindx == -1)
7178 {
7179 if (! elf_link_record_dynamic_symbol (info, h))
7180 return false;
7181 }
7182
7183 if (lsect->rel_section)
7184 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7185 }
a7b97311 7186 else
252b5132 7187 {
a7b97311 7188 /* Allocation of a pointer to a local symbol. */
252b5132
RH
7189 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
7190
a7b97311 7191 /* Allocate a table to hold the local symbols if first time. */
252b5132
RH
7192 if (!ptr)
7193 {
7194 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
7195 register unsigned int i;
7196
dc810e39
AM
7197 amt = num_symbols;
7198 amt *= sizeof (elf_linker_section_pointers_t *);
7199 ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
252b5132
RH
7200
7201 if (!ptr)
7202 return false;
7203
7204 elf_local_ptr_offsets (abfd) = ptr;
7205 for (i = 0; i < num_symbols; i++)
a7b97311 7206 ptr[i] = (elf_linker_section_pointers_t *) 0;
252b5132
RH
7207 }
7208
a7b97311 7209 /* Has this symbol already been allocated? If so, our work is done. */
252b5132
RH
7210 if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
7211 rel->r_addend,
7212 lsect->which))
7213 return true;
7214
7215 ptr_linker_section_ptr = &ptr[r_symndx];
7216
7217 if (info->shared)
7218 {
7219 /* If we are generating a shared object, we need to
7220 output a R_<xxx>_RELATIVE reloc so that the
7221 dynamic linker can adjust this GOT entry. */
7222 BFD_ASSERT (lsect->rel_section != NULL);
7223 lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
7224 }
7225 }
7226
a7b97311
AM
7227 /* Allocate space for a pointer in the linker section, and allocate
7228 a new pointer record from internal memory. */
252b5132 7229 BFD_ASSERT (ptr_linker_section_ptr != NULL);
dc810e39
AM
7230 amt = sizeof (elf_linker_section_pointers_t);
7231 linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
252b5132
RH
7232
7233 if (!linker_section_ptr)
7234 return false;
7235
7236 linker_section_ptr->next = *ptr_linker_section_ptr;
7237 linker_section_ptr->addend = rel->r_addend;
7238 linker_section_ptr->which = lsect->which;
7239 linker_section_ptr->written_address_p = false;
7240 *ptr_linker_section_ptr = linker_section_ptr;
7241
7242#if 0
7243 if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
7244 {
a7b97311
AM
7245 linker_section_ptr->offset = (lsect->section->_raw_size
7246 - lsect->hole_size + (ARCH_SIZE / 8));
252b5132
RH
7247 lsect->hole_offset += ARCH_SIZE / 8;
7248 lsect->sym_offset += ARCH_SIZE / 8;
a7b97311 7249 if (lsect->sym_hash)
252b5132 7250 {
a7b97311 7251 /* Bump up symbol value if needed. */
252b5132
RH
7252 lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
7253#ifdef DEBUG
7254 fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
7255 lsect->sym_hash->root.root.string,
a7b97311
AM
7256 (long) ARCH_SIZE / 8,
7257 (long) lsect->sym_hash->root.u.def.value);
252b5132
RH
7258#endif
7259 }
7260 }
7261 else
7262#endif
7263 linker_section_ptr->offset = lsect->section->_raw_size;
7264
7265 lsect->section->_raw_size += ARCH_SIZE / 8;
7266
7267#ifdef DEBUG
a7b97311
AM
7268 fprintf (stderr,
7269 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
7270 lsect->name, (long) linker_section_ptr->offset,
7271 (long) lsect->section->_raw_size);
252b5132
RH
7272#endif
7273
7274 return true;
7275}
252b5132
RH
7276\f
7277#if ARCH_SIZE==64
7278#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_64 (BFD, VAL, ADDR)
7279#endif
7280#if ARCH_SIZE==32
7281#define bfd_put_ptr(BFD,VAL,ADDR) bfd_put_32 (BFD, VAL, ADDR)
7282#endif
7283
209f668e 7284/* Fill in the address for a pointer generated in a linker section. */
252b5132
RH
7285
7286bfd_vma
a7b97311
AM
7287elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
7288 relocation, rel, relative_reloc)
252b5132
RH
7289 bfd *output_bfd;
7290 bfd *input_bfd;
7291 struct bfd_link_info *info;
7292 elf_linker_section_t *lsect;
7293 struct elf_link_hash_entry *h;
7294 bfd_vma relocation;
7295 const Elf_Internal_Rela *rel;
7296 int relative_reloc;
7297{
7298 elf_linker_section_pointers_t *linker_section_ptr;
7299
7300 BFD_ASSERT (lsect != NULL);
7301
a7b97311 7302 if (h != NULL)
252b5132 7303 {
a7b97311
AM
7304 /* Handle global symbol. */
7305 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7306 (h->linker_section_pointer,
7307 rel->r_addend,
7308 lsect->which));
252b5132
RH
7309
7310 BFD_ASSERT (linker_section_ptr != NULL);
7311
7312 if (! elf_hash_table (info)->dynamic_sections_created
7313 || (info->shared
7314 && info->symbolic
7315 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
7316 {
7317 /* This is actually a static link, or it is a
7318 -Bsymbolic link and the symbol is defined
7319 locally. We must initialize this entry in the
7320 global section.
7321
7322 When doing a dynamic link, we create a .rela.<xxx>
7323 relocation entry to initialize the value. This
7324 is done in the finish_dynamic_symbol routine. */
7325 if (!linker_section_ptr->written_address_p)
7326 {
7327 linker_section_ptr->written_address_p = true;
a7b97311
AM
7328 bfd_put_ptr (output_bfd,
7329 relocation + linker_section_ptr->addend,
7330 (lsect->section->contents
7331 + linker_section_ptr->offset));
252b5132
RH
7332 }
7333 }
7334 }
a7b97311 7335 else
252b5132 7336 {
a7b97311 7337 /* Handle local symbol. */
252b5132
RH
7338 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
7339 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
7340 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
a7b97311
AM
7341 linker_section_ptr = (_bfd_elf_find_pointer_linker_section
7342 (elf_local_ptr_offsets (input_bfd)[r_symndx],
7343 rel->r_addend,
7344 lsect->which));
252b5132
RH
7345
7346 BFD_ASSERT (linker_section_ptr != NULL);
7347
a7b97311 7348 /* Write out pointer if it hasn't been rewritten out before. */
252b5132
RH
7349 if (!linker_section_ptr->written_address_p)
7350 {
7351 linker_section_ptr->written_address_p = true;
7352 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
7353 lsect->section->contents + linker_section_ptr->offset);
7354
7355 if (info->shared)
7356 {
7357 asection *srel = lsect->rel_section;
209f668e 7358 Elf_Internal_Rela *outrel;
dc810e39 7359 Elf_External_Rela *erel;
209f668e
NC
7360 struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7361 unsigned int i;
dc810e39 7362 bfd_size_type amt;
209f668e 7363
dc810e39
AM
7364 amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
7365 outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
209f668e
NC
7366 if (outrel == NULL)
7367 {
7368 (*_bfd_error_handler) (_("Error: out of memory"));
7369 return 0;
7370 }
252b5132 7371
a7b97311
AM
7372 /* We need to generate a relative reloc for the dynamic
7373 linker. */
252b5132 7374 if (!srel)
a7b97311
AM
7375 {
7376 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
7377 lsect->rel_name);
7378 lsect->rel_section = srel;
7379 }
252b5132
RH
7380
7381 BFD_ASSERT (srel != NULL);
7382
209f668e
NC
7383 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7384 outrel[i].r_offset = (lsect->section->output_section->vma
7385 + lsect->section->output_offset
7386 + linker_section_ptr->offset);
7387 outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
7388 outrel[0].r_addend = 0;
dc810e39
AM
7389 erel = (Elf_External_Rela *) lsect->section->contents;
7390 erel += elf_section_data (lsect->section)->rel_count;
7391 elf_swap_reloca_out (output_bfd, outrel, erel);
0525d26e 7392 ++elf_section_data (lsect->section)->rel_count;
dc810e39 7393
209f668e 7394 free (outrel);
252b5132
RH
7395 }
7396 }
7397 }
7398
7399 relocation = (lsect->section->output_offset
7400 + linker_section_ptr->offset
7401 - lsect->hole_offset
7402 - lsect->sym_offset);
7403
7404#ifdef DEBUG
a7b97311
AM
7405 fprintf (stderr,
7406 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
7407 lsect->name, (long) relocation, (long) relocation);
252b5132
RH
7408#endif
7409
7410 /* Subtract out the addend, because it will get added back in by the normal
7411 processing. */
7412 return relocation - linker_section_ptr->addend;
7413}
7414\f
7415/* Garbage collect unused sections. */
7416
7417static boolean elf_gc_mark
7418 PARAMS ((struct bfd_link_info *info, asection *sec,
7419 asection * (*gc_mark_hook)
7420 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7421 struct elf_link_hash_entry *, Elf_Internal_Sym *))));
7422
7423static boolean elf_gc_sweep
7424 PARAMS ((struct bfd_link_info *info,
7425 boolean (*gc_sweep_hook)
7426 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7427 const Elf_Internal_Rela *relocs))));
7428
7429static boolean elf_gc_sweep_symbol
7430 PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
7431
7432static boolean elf_gc_allocate_got_offsets
7433 PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
7434
7435static boolean elf_gc_propagate_vtable_entries_used
7436 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7437
7438static boolean elf_gc_smash_unused_vtentry_relocs
7439 PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
7440
7441/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
7442 it and any sections in this section's group, and all the sections
7443 which define symbols to which it refers. */
252b5132
RH
7444
7445static boolean
7446elf_gc_mark (info, sec, gc_mark_hook)
7447 struct bfd_link_info *info;
7448 asection *sec;
7449 asection * (*gc_mark_hook)
7450 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
7451 struct elf_link_hash_entry *, Elf_Internal_Sym *));
7452{
dbb410c3
AM
7453 boolean ret;
7454 asection *group_sec;
252b5132
RH
7455
7456 sec->gc_mark = 1;
7457
dbb410c3
AM
7458 /* Mark all the sections in the group. */
7459 group_sec = elf_section_data (sec)->next_in_group;
7460 if (group_sec && !group_sec->gc_mark)
7461 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
7462 return false;
252b5132 7463
dbb410c3
AM
7464 /* Look through the section relocs. */
7465 ret = true;
252b5132
RH
7466 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
7467 {
7468 Elf_Internal_Rela *relstart, *rel, *relend;
7469 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7470 Elf_Internal_Shdr *shndx_hdr;
252b5132
RH
7471 struct elf_link_hash_entry **sym_hashes;
7472 size_t nlocsyms;
7473 size_t extsymoff;
7474 Elf_External_Sym *locsyms, *freesyms = NULL;
9ad5cbcf 7475 Elf_External_Sym_Shndx *locsym_shndx;
252b5132 7476 bfd *input_bfd = sec->owner;
c7ac6ff8 7477 struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
252b5132
RH
7478
7479 /* GCFIXME: how to arrange so that relocs and symbols are not
7480 reread continually? */
7481
7482 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7483 sym_hashes = elf_sym_hashes (input_bfd);
7484
7485 /* Read the local symbols. */
7486 if (elf_bad_symtab (input_bfd))
7487 {
7488 nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
c44233aa 7489 extsymoff = 0;
252b5132
RH
7490 }
7491 else
7492 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 7493
252b5132
RH
7494 if (symtab_hdr->contents)
7495 locsyms = (Elf_External_Sym *) symtab_hdr->contents;
7496 else if (nlocsyms == 0)
7497 locsyms = NULL;
7498 else
7499 {
dc810e39
AM
7500 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
7501 locsyms = freesyms = bfd_malloc (amt);
252b5132
RH
7502 if (freesyms == NULL
7503 || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
dc810e39 7504 || bfd_bread (locsyms, amt, input_bfd) != amt)
252b5132
RH
7505 {
7506 ret = false;
7507 goto out1;
7508 }
7509 }
7510
9ad5cbcf
AM
7511 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
7512 locsym_shndx = NULL;
7513 if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
7514 {
7515 bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
7516 locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
7517 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
7518 || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
7519 return false;
7520 }
7521
252b5132
RH
7522 /* Read the relocations. */
7523 relstart = (NAME(_bfd_elf,link_read_relocs)
7524 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
7525 info->keep_memory));
7526 if (relstart == NULL)
7527 {
7528 ret = false;
7529 goto out1;
7530 }
c7ac6ff8 7531 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7532
7533 for (rel = relstart; rel < relend; rel++)
7534 {
7535 unsigned long r_symndx;
7536 asection *rsec;
7537 struct elf_link_hash_entry *h;
7538 Elf_Internal_Sym s;
7539
7540 r_symndx = ELF_R_SYM (rel->r_info);
7541 if (r_symndx == 0)
7542 continue;
7543
7544 if (elf_bad_symtab (sec->owner))
7545 {
9ad5cbcf
AM
7546 elf_swap_symbol_in (input_bfd,
7547 locsyms + r_symndx,
7548 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7549 &s);
252b5132 7550 if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
c44233aa 7551 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
252b5132
RH
7552 else
7553 {
c44233aa
AM
7554 h = sym_hashes[r_symndx - extsymoff];
7555 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
252b5132
RH
7556 }
7557 }
7558 else if (r_symndx >= nlocsyms)
7559 {
7560 h = sym_hashes[r_symndx - extsymoff];
3e932841 7561 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
252b5132
RH
7562 }
7563 else
7564 {
9ad5cbcf
AM
7565 elf_swap_symbol_in (input_bfd,
7566 locsyms + r_symndx,
7567 locsym_shndx + (locsym_shndx ? r_symndx : 0),
7568 &s);
3e932841 7569 rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
252b5132
RH
7570 }
7571
7572 if (rsec && !rsec->gc_mark)
7573 if (!elf_gc_mark (info, rsec, gc_mark_hook))
7574 {
7575 ret = false;
7576 goto out2;
7577 }
7578 }
7579
7580 out2:
7581 if (!info->keep_memory)
7582 free (relstart);
7583 out1:
7584 if (freesyms)
7585 free (freesyms);
7586 }
7587
7588 return ret;
7589}
7590
7591/* The sweep phase of garbage collection. Remove all garbage sections. */
7592
7593static boolean
7594elf_gc_sweep (info, gc_sweep_hook)
7595 struct bfd_link_info *info;
7596 boolean (*gc_sweep_hook)
7597 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
7598 const Elf_Internal_Rela *relocs));
7599{
7600 bfd *sub;
7601
7602 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7603 {
7604 asection *o;
7605
f6af82bd
AM
7606 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7607 continue;
7608
252b5132
RH
7609 for (o = sub->sections; o != NULL; o = o->next)
7610 {
7611 /* Keep special sections. Keep .debug sections. */
7612 if ((o->flags & SEC_LINKER_CREATED)
7613 || (o->flags & SEC_DEBUGGING))
7614 o->gc_mark = 1;
7615
7616 if (o->gc_mark)
7617 continue;
7618
7619 /* Skip sweeping sections already excluded. */
7620 if (o->flags & SEC_EXCLUDE)
7621 continue;
7622
7623 /* Since this is early in the link process, it is simple
7624 to remove a section from the output. */
7625 o->flags |= SEC_EXCLUDE;
7626
7627 /* But we also have to update some of the relocation
7628 info we collected before. */
7629 if (gc_sweep_hook
7630 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
7631 {
7632 Elf_Internal_Rela *internal_relocs;
7633 boolean r;
7634
7635 internal_relocs = (NAME(_bfd_elf,link_read_relocs)
7636 (o->owner, o, NULL, NULL, info->keep_memory));
7637 if (internal_relocs == NULL)
7638 return false;
7639
3e932841 7640 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132
RH
7641
7642 if (!info->keep_memory)
7643 free (internal_relocs);
7644
7645 if (!r)
7646 return false;
7647 }
7648 }
7649 }
7650
7651 /* Remove the symbols that were in the swept sections from the dynamic
7652 symbol table. GCFIXME: Anyone know how to get them out of the
7653 static symbol table as well? */
7654 {
7655 int i = 0;
7656
7657 elf_link_hash_traverse (elf_hash_table (info),
7658 elf_gc_sweep_symbol,
7659 (PTR) &i);
7660
7661 elf_hash_table (info)->dynsymcount = i;
7662 }
7663
7664 return true;
7665}
7666
7667/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
7668
7669static boolean
7670elf_gc_sweep_symbol (h, idxptr)
7671 struct elf_link_hash_entry *h;
7672 PTR idxptr;
7673{
7674 int *idx = (int *) idxptr;
7675
7676 if (h->dynindx != -1
7677 && ((h->root.type != bfd_link_hash_defined
7678 && h->root.type != bfd_link_hash_defweak)
7679 || h->root.u.def.section->gc_mark))
7680 h->dynindx = (*idx)++;
7681
7682 return true;
7683}
7684
7685/* Propogate collected vtable information. This is called through
7686 elf_link_hash_traverse. */
7687
7688static boolean
7689elf_gc_propagate_vtable_entries_used (h, okp)
7690 struct elf_link_hash_entry *h;
7691 PTR okp;
7692{
3e932841 7693 /* Those that are not vtables. */
252b5132
RH
7694 if (h->vtable_parent == NULL)
7695 return true;
7696
7697 /* Those vtables that do not have parents, we cannot merge. */
7698 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
7699 return true;
7700
7701 /* If we've already been done, exit. */
7702 if (h->vtable_entries_used && h->vtable_entries_used[-1])
7703 return true;
7704
7705 /* Make sure the parent's table is up to date. */
7706 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
7707
7708 if (h->vtable_entries_used == NULL)
7709 {
7710 /* None of this table's entries were referenced. Re-use the
7711 parent's table. */
7712 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
7713 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
7714 }
7715 else
7716 {
7717 size_t n;
7718 boolean *cu, *pu;
7719
7720 /* Or the parent's entries into ours. */
7721 cu = h->vtable_entries_used;
7722 cu[-1] = true;
7723 pu = h->vtable_parent->vtable_entries_used;
7724 if (pu != NULL)
7725 {
c44233aa
AM
7726 asection *sec = h->root.u.def.section;
7727 struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
7728 int file_align = bed->s->file_align;
0d1ea5c0
CM
7729
7730 n = h->vtable_parent->vtable_entries_size / file_align;
374b596d 7731 while (n--)
252b5132 7732 {
374b596d
NC
7733 if (*pu)
7734 *cu = true;
7735 pu++;
7736 cu++;
252b5132
RH
7737 }
7738 }
7739 }
7740
7741 return true;
7742}
7743
7744static boolean
7745elf_gc_smash_unused_vtentry_relocs (h, okp)
7746 struct elf_link_hash_entry *h;
7747 PTR okp;
7748{
7749 asection *sec;
7750 bfd_vma hstart, hend;
7751 Elf_Internal_Rela *relstart, *relend, *rel;
c7ac6ff8 7752 struct elf_backend_data *bed;
0d1ea5c0 7753 int file_align;
252b5132
RH
7754
7755 /* Take care of both those symbols that do not describe vtables as
7756 well as those that are not loaded. */
7757 if (h->vtable_parent == NULL)
7758 return true;
7759
7760 BFD_ASSERT (h->root.type == bfd_link_hash_defined
7761 || h->root.type == bfd_link_hash_defweak);
7762
7763 sec = h->root.u.def.section;
7764 hstart = h->root.u.def.value;
7765 hend = hstart + h->size;
7766
7767 relstart = (NAME(_bfd_elf,link_read_relocs)
7768 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
7769 if (!relstart)
a7b97311 7770 return *(boolean *) okp = false;
c7ac6ff8 7771 bed = get_elf_backend_data (sec->owner);
0d1ea5c0
CM
7772 file_align = bed->s->file_align;
7773
c7ac6ff8 7774 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
7775
7776 for (rel = relstart; rel < relend; ++rel)
7777 if (rel->r_offset >= hstart && rel->r_offset < hend)
7778 {
7779 /* If the entry is in use, do nothing. */
7780 if (h->vtable_entries_used
7781 && (rel->r_offset - hstart) < h->vtable_entries_size)
7782 {
0d1ea5c0 7783 bfd_vma entry = (rel->r_offset - hstart) / file_align;
252b5132
RH
7784 if (h->vtable_entries_used[entry])
7785 continue;
7786 }
7787 /* Otherwise, kill it. */
7788 rel->r_offset = rel->r_info = rel->r_addend = 0;
7789 }
7790
7791 return true;
7792}
7793
7794/* Do mark and sweep of unused sections. */
7795
7796boolean
7797elf_gc_sections (abfd, info)
7798 bfd *abfd;
7799 struct bfd_link_info *info;
7800{
7801 boolean ok = true;
7802 bfd *sub;
7803 asection * (*gc_mark_hook)
dc810e39 7804 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
c44233aa 7805 struct elf_link_hash_entry *h, Elf_Internal_Sym *));
252b5132
RH
7806
7807 if (!get_elf_backend_data (abfd)->can_gc_sections
6d3e950b 7808 || info->relocateable || info->emitrelocations
252b5132
RH
7809 || elf_hash_table (info)->dynamic_sections_created)
7810 return true;
7811
7812 /* Apply transitive closure to the vtable entry usage info. */
7813 elf_link_hash_traverse (elf_hash_table (info),
7814 elf_gc_propagate_vtable_entries_used,
7815 (PTR) &ok);
7816 if (!ok)
7817 return false;
7818
7819 /* Kill the vtable relocations that were not used. */
7820 elf_link_hash_traverse (elf_hash_table (info),
7821 elf_gc_smash_unused_vtentry_relocs,
7822 (PTR) &ok);
7823 if (!ok)
7824 return false;
7825
7826 /* Grovel through relocs to find out who stays ... */
7827
7828 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
7829 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7830 {
7831 asection *o;
f6af82bd
AM
7832
7833 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
7834 continue;
7835
252b5132
RH
7836 for (o = sub->sections; o != NULL; o = o->next)
7837 {
7838 if (o->flags & SEC_KEEP)
c44233aa 7839 if (!elf_gc_mark (info, o, gc_mark_hook))
252b5132
RH
7840 return false;
7841 }
7842 }
7843
7844 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 7845 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
252b5132
RH
7846 return false;
7847
7848 return true;
7849}
7850\f
7851/* Called from check_relocs to record the existance of a VTINHERIT reloc. */
7852
7853boolean
7854elf_gc_record_vtinherit (abfd, sec, h, offset)
7855 bfd *abfd;
7856 asection *sec;
7857 struct elf_link_hash_entry *h;
7858 bfd_vma offset;
7859{
7860 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7861 struct elf_link_hash_entry **search, *child;
7862 bfd_size_type extsymcount;
7863
7864 /* The sh_info field of the symtab header tells us where the
7865 external symbols start. We don't care about the local symbols at
7866 this point. */
7867 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size/sizeof (Elf_External_Sym);
7868 if (!elf_bad_symtab (abfd))
7869 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
7870
7871 sym_hashes = elf_sym_hashes (abfd);
7872 sym_hashes_end = sym_hashes + extsymcount;
7873
7874 /* Hunt down the child symbol, which is in this section at the same
7875 offset as the relocation. */
7876 for (search = sym_hashes; search != sym_hashes_end; ++search)
7877 {
7878 if ((child = *search) != NULL
7879 && (child->root.type == bfd_link_hash_defined
7880 || child->root.type == bfd_link_hash_defweak)
7881 && child->root.u.def.section == sec
7882 && child->root.u.def.value == offset)
7883 goto win;
7884 }
7885
7886 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 7887 bfd_archive_filename (abfd), sec->name,
a7b97311 7888 (unsigned long) offset);
252b5132
RH
7889 bfd_set_error (bfd_error_invalid_operation);
7890 return false;
7891
dc810e39 7892 win:
252b5132
RH
7893 if (!h)
7894 {
7895 /* This *should* only be the absolute section. It could potentially
7896 be that someone has defined a non-global vtable though, which
7897 would be bad. It isn't worth paging in the local symbols to be
7898 sure though; that case should simply be handled by the assembler. */
7899
7900 child->vtable_parent = (struct elf_link_hash_entry *) -1;
7901 }
7902 else
7903 child->vtable_parent = h;
7904
7905 return true;
7906}
7907
7908/* Called from check_relocs to record the existance of a VTENTRY reloc. */
7909
7910boolean
7911elf_gc_record_vtentry (abfd, sec, h, addend)
7442e600
ILT
7912 bfd *abfd ATTRIBUTE_UNUSED;
7913 asection *sec ATTRIBUTE_UNUSED;
252b5132
RH
7914 struct elf_link_hash_entry *h;
7915 bfd_vma addend;
7916{
0d1ea5c0
CM
7917 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7918 int file_align = bed->s->file_align;
7919
252b5132
RH
7920 if (addend >= h->vtable_entries_size)
7921 {
7922 size_t size, bytes;
7923 boolean *ptr = h->vtable_entries_used;
7924
7925 /* While the symbol is undefined, we have to be prepared to handle
7926 a zero size. */
7927 if (h->root.type == bfd_link_hash_undefined)
7928 size = addend;
7929 else
7930 {
7931 size = h->size;
7932 if (size < addend)
7933 {
7934 /* Oops! We've got a reference past the defined end of
7935 the table. This is probably a bug -- shall we warn? */
7936 size = addend;
7937 }
7938 }
7939
7940 /* Allocate one extra entry for use as a "done" flag for the
7941 consolidation pass. */
0d1ea5c0 7942 bytes = (size / file_align + 1) * sizeof (boolean);
252b5132
RH
7943
7944 if (ptr)
7945 {
dc810e39 7946 ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
3e932841 7947
fed79cc6
NC
7948 if (ptr != NULL)
7949 {
7950 size_t oldbytes;
252b5132 7951
a7b97311
AM
7952 oldbytes = ((h->vtable_entries_size / file_align + 1)
7953 * sizeof (boolean));
7954 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 7955 }
252b5132
RH
7956 }
7957 else
dc810e39 7958 ptr = bfd_zmalloc ((bfd_size_type) bytes);
252b5132 7959
fed79cc6
NC
7960 if (ptr == NULL)
7961 return false;
3e932841 7962
252b5132 7963 /* And arrange for that done flag to be at index -1. */
fed79cc6 7964 h->vtable_entries_used = ptr + 1;
252b5132
RH
7965 h->vtable_entries_size = size;
7966 }
3e932841 7967
0d1ea5c0 7968 h->vtable_entries_used[addend / file_align] = true;
252b5132
RH
7969
7970 return true;
7971}
7972
7973/* And an accompanying bit to work out final got entry offsets once
7974 we're done. Should be called from final_link. */
7975
7976boolean
7977elf_gc_common_finalize_got_offsets (abfd, info)
7978 bfd *abfd;
7979 struct bfd_link_info *info;
7980{
7981 bfd *i;
7982 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7983 bfd_vma gotoff;
7984
7985 /* The GOT offset is relative to the .got section, but the GOT header is
7986 put into the .got.plt section, if the backend uses it. */
7987 if (bed->want_got_plt)
7988 gotoff = 0;
7989 else
7990 gotoff = bed->got_header_size;
7991
7992 /* Do the local .got entries first. */
7993 for (i = info->input_bfds; i; i = i->link_next)
7994 {
f6af82bd 7995 bfd_signed_vma *local_got;
252b5132
RH
7996 bfd_size_type j, locsymcount;
7997 Elf_Internal_Shdr *symtab_hdr;
7998
f6af82bd
AM
7999 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8000 continue;
8001
8002 local_got = elf_local_got_refcounts (i);
252b5132
RH
8003 if (!local_got)
8004 continue;
8005
8006 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8007 if (elf_bad_symtab (i))
8008 locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8009 else
8010 locsymcount = symtab_hdr->sh_info;
8011
8012 for (j = 0; j < locsymcount; ++j)
8013 {
8014 if (local_got[j] > 0)
8015 {
8016 local_got[j] = gotoff;
8017 gotoff += ARCH_SIZE / 8;
8018 }
8019 else
8020 local_got[j] = (bfd_vma) -1;
8021 }
8022 }
8023
dd5724d5
AM
8024 /* Then the global .got entries. .plt refcounts are handled by
8025 adjust_dynamic_symbol */
252b5132
RH
8026 elf_link_hash_traverse (elf_hash_table (info),
8027 elf_gc_allocate_got_offsets,
8028 (PTR) &gotoff);
8029 return true;
8030}
8031
8032/* We need a special top-level link routine to convert got reference counts
8033 to real got offsets. */
8034
8035static boolean
8036elf_gc_allocate_got_offsets (h, offarg)
8037 struct elf_link_hash_entry *h;
8038 PTR offarg;
8039{
8040 bfd_vma *off = (bfd_vma *) offarg;
8041
8042 if (h->got.refcount > 0)
8043 {
8044 h->got.offset = off[0];
8045 off[0] += ARCH_SIZE / 8;
8046 }
8047 else
8048 h->got.offset = (bfd_vma) -1;
8049
8050 return true;
8051}
8052
8053/* Many folk need no more in the way of final link than this, once
8054 got entry reference counting is enabled. */
8055
8056boolean
8057elf_gc_common_final_link (abfd, info)
8058 bfd *abfd;
8059 struct bfd_link_info *info;
8060{
8061 if (!elf_gc_common_finalize_got_offsets (abfd, info))
8062 return false;
8063
8064 /* Invoke the regular ELF backend linker to do all the work. */
8065 return elf_bfd_final_link (abfd, info);
8066}
8067
8068/* This function will be called though elf_link_hash_traverse to store
8069 all hash value of the exported symbols in an array. */
8070
8071static boolean
8072elf_collect_hash_codes (h, data)
8073 struct elf_link_hash_entry *h;
8074 PTR data;
8075{
8076 unsigned long **valuep = (unsigned long **) data;
8077 const char *name;
8078 char *p;
8079 unsigned long ha;
8080 char *alc = NULL;
8081
8082 /* Ignore indirect symbols. These are added by the versioning code. */
8083 if (h->dynindx == -1)
8084 return true;
8085
8086 name = h->root.root.string;
8087 p = strchr (name, ELF_VER_CHR);
8088 if (p != NULL)
8089 {
dc810e39
AM
8090 alc = bfd_malloc ((bfd_size_type) (p - name + 1));
8091 memcpy (alc, name, (size_t) (p - name));
252b5132
RH
8092 alc[p - name] = '\0';
8093 name = alc;
8094 }
8095
8096 /* Compute the hash value. */
8097 ha = bfd_elf_hash (name);
8098
8099 /* Store the found hash value in the array given as the argument. */
8100 *(*valuep)++ = ha;
8101
8102 /* And store it in the struct so that we can put it in the hash table
8103 later. */
8104 h->elf_hash_value = ha;
8105
8106 if (alc != NULL)
8107 free (alc);
8108
8109 return true;
8110}
73d074b4
DJ
8111
8112boolean
8113elf_reloc_symbol_deleted_p (offset, cookie)
8114 bfd_vma offset;
8115 PTR cookie;
8116{
9ad5cbcf 8117 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
73d074b4
DJ
8118
8119 if (rcookie->bad_symtab)
8120 rcookie->rel = rcookie->rels;
8121
8122 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8123 {
8124 unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
8125 Elf_Internal_Sym isym;
8126
8127 if (! rcookie->bad_symtab)
8128 if (rcookie->rel->r_offset > offset)
8129 return false;
8130 if (rcookie->rel->r_offset != offset)
8131 continue;
8132
f9f32305 8133 if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
9ad5cbcf
AM
8134 {
8135 Elf_External_Sym *lsym;
8136 Elf_External_Sym_Shndx *lshndx;
8137
8138 lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
8139 lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
8140 if (lshndx != NULL)
8141 lshndx += r_symndx;
8142 elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
8143 }
73d074b4
DJ
8144
8145 if (r_symndx >= rcookie->locsymcount
8146 || (rcookie->locsyms
8147 && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
8148 {
8149 struct elf_link_hash_entry *h;
8150
8151 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8152
8153 while (h->root.type == bfd_link_hash_indirect
8154 || h->root.type == bfd_link_hash_warning)
8155 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8156
8157 if ((h->root.type == bfd_link_hash_defined
8158 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 8159 && elf_discarded_section (h->root.u.def.section))
73d074b4
DJ
8160 return true;
8161 else
8162 return false;
8163 }
8164 else if (rcookie->locsyms)
8165 {
8166 /* It's not a relocation against a global symbol,
44421011 8167 but it could be a relocation against a local
73d074b4
DJ
8168 symbol for a discarded section. */
8169 asection *isec;
8170
8171 /* Need to: get the symbol; get the section. */
9ad5cbcf 8172 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
73d074b4
DJ
8173 {
8174 isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
ed4de5e2 8175 if (isec != NULL && elf_discarded_section (isec))
73d074b4
DJ
8176 return true;
8177 }
8178 }
8179 return false;
8180 }
8181 return false;
8182}
8183
8184/* Discard unneeded references to discarded sections.
8185 Returns true if any section's size was changed. */
8186/* This function assumes that the relocations are in sorted order,
8187 which is true for all known assemblers. */
8188
8189boolean
65765700
JJ
8190elf_bfd_discard_info (output_bfd, info)
8191 bfd *output_bfd;
73d074b4
DJ
8192 struct bfd_link_info *info;
8193{
8194 struct elf_reloc_cookie cookie;
65765700 8195 asection *stab, *eh, *ehdr;
73d074b4 8196 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 8197 Elf_Internal_Shdr *shndx_hdr;
73d074b4
DJ
8198 Elf_External_Sym *freesyms;
8199 struct elf_backend_data *bed;
8200 bfd *abfd;
8201 boolean ret = false;
65765700 8202 boolean strip = info->strip == strip_all || info->strip == strip_debugger;
73d074b4
DJ
8203
8204 if (info->relocateable
8205 || info->traditional_format
8206 || info->hash->creator->flavour != bfd_target_elf_flavour
65765700 8207 || ! is_elf_hash_table (info))
73d074b4 8208 return false;
65765700 8209
72dd6331
AM
8210 ehdr = NULL;
8211 if (elf_hash_table (info)->dynobj != NULL)
8212 ehdr = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
8213 ".eh_frame_hdr");
65765700 8214
73d074b4
DJ
8215 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8216 {
163c1c30
L
8217 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8218 continue;
8219
73d074b4
DJ
8220 bed = get_elf_backend_data (abfd);
8221
8222 if ((abfd->flags & DYNAMIC) != 0)
8223 continue;
8224
65765700
JJ
8225 eh = NULL;
8226 if (ehdr)
8227 {
8228 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8229 if (eh && eh->_raw_size == 0)
8230 eh = NULL;
8231 }
8232
8233 stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
40b829d4
AM
8234 if ((! stab
8235 || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
65765700
JJ
8236 && ! eh
8237 && (strip || ! bed->elf_backend_discard_info))
73d074b4
DJ
8238 continue;
8239
8240 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9ad5cbcf 8241 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
73d074b4
DJ
8242
8243 cookie.abfd = abfd;
8244 cookie.sym_hashes = elf_sym_hashes (abfd);
8245 cookie.bad_symtab = elf_bad_symtab (abfd);
8246 if (cookie.bad_symtab)
8247 {
8248 cookie.locsymcount =
8249 symtab_hdr->sh_size / sizeof (Elf_External_Sym);
8250 cookie.extsymoff = 0;
8251 }
8252 else
8253 {
8254 cookie.locsymcount = symtab_hdr->sh_info;
8255 cookie.extsymoff = symtab_hdr->sh_info;
8256 }
8257
8258 freesyms = NULL;
8259 if (symtab_hdr->contents)
c44233aa 8260 cookie.locsyms = (void *) symtab_hdr->contents;
73d074b4 8261 else if (cookie.locsymcount == 0)
c44233aa 8262 cookie.locsyms = NULL;
73d074b4 8263 else
c44233aa
AM
8264 {
8265 bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
8266 cookie.locsyms = bfd_malloc (amt);
8267 if (cookie.locsyms == NULL)
9ad5cbcf
AM
8268 return false;
8269 freesyms = cookie.locsyms;
8270 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
c44233aa 8271 || bfd_bread (cookie.locsyms, amt, abfd) != amt)
73d074b4 8272 {
9ad5cbcf
AM
8273 error_ret_free_loc:
8274 free (cookie.locsyms);
8275 return false;
c44233aa
AM
8276 }
8277 }
9ad5cbcf
AM
8278
8279 cookie.locsym_shndx = NULL;
8280 if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
8281 {
8282 bfd_size_type amt;
8283 amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
8284 cookie.locsym_shndx = bfd_malloc (amt);
8285 if (cookie.locsym_shndx == NULL)
8286 goto error_ret_free_loc;
8287 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
8288 || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
73d074b4 8289 {
9ad5cbcf
AM
8290 free (cookie.locsym_shndx);
8291 goto error_ret_free_loc;
73d074b4 8292 }
9ad5cbcf 8293 }
73d074b4 8294
65765700 8295 if (stab)
73d074b4
DJ
8296 {
8297 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
65765700 8298 (abfd, stab, (PTR) NULL,
73d074b4
DJ
8299 (Elf_Internal_Rela *) NULL,
8300 info->keep_memory));
8301 if (cookie.rels)
8302 {
8303 cookie.rel = cookie.rels;
8304 cookie.relend =
65765700
JJ
8305 cookie.rels + stab->reloc_count * bed->s->int_rels_per_ext_rel;
8306 if (_bfd_discard_section_stabs (abfd, stab,
8307 elf_section_data (stab)->sec_info,
73d074b4
DJ
8308 elf_reloc_symbol_deleted_p,
8309 &cookie))
8310 ret = true;
8311 if (! info->keep_memory)
8312 free (cookie.rels);
8313 }
8314 }
8315
65765700
JJ
8316 if (eh)
8317 {
8318 cookie.rels = NULL;
8319 cookie.rel = NULL;
8320 cookie.relend = NULL;
8321 if (eh->reloc_count)
8322 cookie.rels = (NAME(_bfd_elf,link_read_relocs)
40b829d4 8323 (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
65765700
JJ
8324 info->keep_memory));
8325 if (cookie.rels)
8326 {
8327 cookie.rel = cookie.rels;
8328 cookie.relend =
8329 cookie.rels + eh->reloc_count * bed->s->int_rels_per_ext_rel;
8330 }
8331 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
8332 elf_reloc_symbol_deleted_p,
8333 &cookie))
8334 ret = true;
8335 if (! info->keep_memory)
8336 free (cookie.rels);
8337 }
8338
73d074b4
DJ
8339 if (bed->elf_backend_discard_info)
8340 {
8341 if (bed->elf_backend_discard_info (abfd, &cookie, info))
8342 ret = true;
8343 }
8344
9ad5cbcf
AM
8345 if (cookie.locsym_shndx != NULL)
8346 free (cookie.locsym_shndx);
8347
8348 if (freesyms != NULL)
73d074b4
DJ
8349 free (freesyms);
8350 }
65765700 8351
40b829d4 8352 if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
65765700 8353 ret = true;
73d074b4
DJ
8354 return ret;
8355}
8356
8357static boolean
8358elf_section_ignore_discarded_relocs (sec)
8359 asection *sec;
8360{
40b829d4
AM
8361 struct elf_backend_data *bed;
8362
65765700
JJ
8363 switch (elf_section_data (sec)->sec_info_type)
8364 {
8365 case ELF_INFO_TYPE_STABS:
8366 case ELF_INFO_TYPE_EH_FRAME:
8367 return true;
8368 default:
8369 break;
8370 }
40b829d4
AM
8371
8372 bed = get_elf_backend_data (sec->owner);
8373 if (bed->elf_backend_ignore_discarded_relocs != NULL
8374 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
73d074b4 8375 return true;
65765700
JJ
8376
8377 return false;
73d074b4 8378}
This page took 0.574183 seconds and 4 git commands to generate.