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