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