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