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