* gas/i860/dir-intel03-err.l: Update for junk at end line becoming
[deliverable/binutils-gdb.git] / bfd / elflink.h
CommitLineData
252b5132 1/* ELF linker support.
79349b09 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
7898deda 3 Free Software Foundation, Inc.
252b5132 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132
RH
20
21/* ELF linker code. */
22
268b6b39 23static bfd_boolean elf_section_ignore_discarded_relocs (asection *);
252b5132 24
252b5132
RH
25/* Final phase of ELF linker. */
26
27/* A structure we use to avoid passing large numbers of arguments. */
28
29struct elf_final_link_info
30{
31 /* General link information. */
32 struct bfd_link_info *info;
33 /* Output BFD. */
34 bfd *output_bfd;
35 /* Symbol string table. */
36 struct bfd_strtab_hash *symstrtab;
37 /* .dynsym section. */
38 asection *dynsym_sec;
39 /* .hash section. */
40 asection *hash_sec;
41 /* symbol version section (.gnu.version). */
42 asection *symver_sec;
43 /* Buffer large enough to hold contents of any section. */
44 bfd_byte *contents;
45 /* Buffer large enough to hold external relocs of any section. */
268b6b39 46 void *external_relocs;
252b5132
RH
47 /* Buffer large enough to hold internal relocs of any section. */
48 Elf_Internal_Rela *internal_relocs;
49 /* Buffer large enough to hold external local symbols of any input
50 BFD. */
140f6c8e 51 bfd_byte *external_syms;
9ad5cbcf
AM
52 /* And a buffer for symbol section indices. */
53 Elf_External_Sym_Shndx *locsym_shndx;
252b5132
RH
54 /* Buffer large enough to hold internal local symbols of any input
55 BFD. */
56 Elf_Internal_Sym *internal_syms;
57 /* Array large enough to hold a symbol index for each local symbol
58 of any input BFD. */
59 long *indices;
60 /* Array large enough to hold a section pointer for each local
61 symbol of any input BFD. */
62 asection **sections;
63 /* Buffer to hold swapped out symbols. */
140f6c8e 64 bfd_byte *symbuf;
9ad5cbcf
AM
65 /* And one for symbol section indices. */
66 Elf_External_Sym_Shndx *symshndxbuf;
252b5132
RH
67 /* Number of swapped out symbols in buffer. */
68 size_t symbuf_count;
69 /* Number of symbols which fit in symbuf. */
70 size_t symbuf_size;
c97e73dd
AM
71 /* And same for symshndxbuf. */
72 size_t shndxbuf_size;
252b5132
RH
73};
74
b34976b6 75static bfd_boolean elf_link_output_sym
754021d0
AM
76 (struct elf_final_link_info *, const char *, Elf_Internal_Sym *, asection *,
77 struct elf_link_hash_entry *);
b34976b6 78static bfd_boolean elf_link_flush_output_syms
140f6c8e 79 (struct elf_final_link_info *, const struct elf_backend_data *);
b34976b6 80static bfd_boolean elf_link_output_extsym
268b6b39 81 (struct elf_link_hash_entry *, void *);
b34976b6 82static bfd_boolean elf_link_input_bfd
268b6b39 83 (struct elf_final_link_info *, bfd *);
b34976b6 84static bfd_boolean elf_reloc_link_order
268b6b39 85 (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
252b5132
RH
86
87/* This struct is used to pass information to elf_link_output_extsym. */
88
89struct elf_outext_info
90{
b34976b6
AM
91 bfd_boolean failed;
92 bfd_boolean localsyms;
252b5132
RH
93 struct elf_final_link_info *finfo;
94};
95
1049f94e 96/* When performing a relocatable link, the input relocations are
31367b81
MM
97 preserved. But, if they reference global symbols, the indices
98 referenced must be updated. Update all the relocations in
99 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
100
101static void
268b6b39
AM
102elf_link_adjust_relocs (bfd *abfd,
103 Elf_Internal_Shdr *rel_hdr,
104 unsigned int count,
105 struct elf_link_hash_entry **rel_hash)
31367b81
MM
106{
107 unsigned int i;
9c5bfbb7 108 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
947216bf 109 bfd_byte *erela;
268b6b39
AM
110 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
111 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
140f6c8e
AM
112 bfd_vma r_type_mask;
113 int r_sym_shift;
209f668e 114
140f6c8e 115 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
209f668e 116 {
947216bf
AM
117 swap_in = bed->s->swap_reloc_in;
118 swap_out = bed->s->swap_reloc_out;
209f668e 119 }
140f6c8e 120 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
209f668e 121 {
947216bf
AM
122 swap_in = bed->s->swap_reloca_in;
123 swap_out = bed->s->swap_reloca_out;
209f668e 124 }
947216bf
AM
125 else
126 abort ();
127
128 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
129 abort ();
31367b81 130
140f6c8e
AM
131 if (bed->s->arch_size == 32)
132 {
133 r_type_mask = 0xff;
134 r_sym_shift = 8;
135 }
136 else
137 {
138 r_type_mask = 0xffffffff;
139 r_sym_shift = 32;
140 }
141
947216bf
AM
142 erela = rel_hdr->contents;
143 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
31367b81 144 {
947216bf
AM
145 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
146 unsigned int j;
147
31367b81
MM
148 if (*rel_hash == NULL)
149 continue;
150
151 BFD_ASSERT ((*rel_hash)->indx >= 0);
152
947216bf
AM
153 (*swap_in) (abfd, erela, irela);
154 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
140f6c8e
AM
155 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
156 | (irela[j].r_info & r_type_mask));
947216bf 157 (*swap_out) (abfd, irela, erela);
31367b81
MM
158 }
159}
160
58821868
AM
161struct elf_link_sort_rela
162{
140f6c8e
AM
163 union {
164 bfd_vma offset;
165 bfd_vma sym_mask;
166 } u;
db6751f2 167 enum elf_reloc_type_class type;
947216bf
AM
168 /* We use this as an array of size int_rels_per_ext_rel. */
169 Elf_Internal_Rela rela[1];
db6751f2
JJ
170};
171
172static int
268b6b39 173elf_link_sort_cmp1 (const void *A, const void *B)
db6751f2 174{
268b6b39
AM
175 const struct elf_link_sort_rela *a = A;
176 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
177 int relativea, relativeb;
178
179 relativea = a->type == reloc_class_relative;
180 relativeb = b->type == reloc_class_relative;
181
182 if (relativea < relativeb)
db6751f2 183 return 1;
fcfbdf31
JJ
184 if (relativea > relativeb)
185 return -1;
140f6c8e 186 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
db6751f2 187 return -1;
140f6c8e 188 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
db6751f2 189 return 1;
947216bf 190 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 191 return -1;
947216bf 192 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
193 return 1;
194 return 0;
195}
196
197static int
268b6b39 198elf_link_sort_cmp2 (const void *A, const void *B)
db6751f2 199{
268b6b39
AM
200 const struct elf_link_sort_rela *a = A;
201 const struct elf_link_sort_rela *b = B;
db6751f2
JJ
202 int copya, copyb;
203
140f6c8e 204 if (a->u.offset < b->u.offset)
db6751f2 205 return -1;
140f6c8e 206 if (a->u.offset > b->u.offset)
db6751f2 207 return 1;
290394d6
JJ
208 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
209 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
db6751f2
JJ
210 if (copya < copyb)
211 return -1;
212 if (copya > copyb)
213 return 1;
947216bf 214 if (a->rela->r_offset < b->rela->r_offset)
db6751f2 215 return -1;
947216bf 216 if (a->rela->r_offset > b->rela->r_offset)
db6751f2
JJ
217 return 1;
218 return 0;
219}
220
221static size_t
268b6b39 222elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
db6751f2 223{
e717da7e 224 asection *reldyn;
f51e552e 225 bfd_size_type count, size;
947216bf
AM
226 size_t i, ret, sort_elt, ext_size;
227 bfd_byte *sort, *s_non_relative, *p;
228 struct elf_link_sort_rela *sq;
9c5bfbb7 229 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
033fd5f9 230 int i2e = bed->s->int_rels_per_ext_rel;
268b6b39
AM
231 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
232 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
e717da7e 233 struct bfd_link_order *lo;
140f6c8e 234 bfd_vma r_sym_mask;
db6751f2
JJ
235
236 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
237 if (reldyn == NULL || reldyn->_raw_size == 0)
238 {
239 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
240 if (reldyn == NULL || reldyn->_raw_size == 0)
241 return 0;
140f6c8e 242 ext_size = bed->s->sizeof_rel;
947216bf
AM
243 swap_in = bed->s->swap_reloc_in;
244 swap_out = bed->s->swap_reloc_out;
db6751f2
JJ
245 }
246 else
947216bf 247 {
140f6c8e 248 ext_size = bed->s->sizeof_rela;
947216bf
AM
249 swap_in = bed->s->swap_reloca_in;
250 swap_out = bed->s->swap_reloca_out;
251 }
252 count = reldyn->_raw_size / ext_size;
db6751f2
JJ
253
254 size = 0;
e717da7e
AM
255 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
256 if (lo->type == bfd_indirect_link_order)
257 {
258 asection *o = lo->u.indirect.section;
259 size += o->_raw_size;
260 }
db6751f2
JJ
261
262 if (size != reldyn->_raw_size)
263 return 0;
264
947216bf
AM
265 sort_elt = (sizeof (struct elf_link_sort_rela)
266 + (i2e - 1) * sizeof (Elf_Internal_Rela));
267 sort = bfd_zmalloc (sort_elt * count);
268 if (sort == NULL)
db6751f2
JJ
269 {
270 (*info->callbacks->warning)
268b6b39 271 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
db6751f2
JJ
272 return 0;
273 }
274
140f6c8e
AM
275 if (bed->s->arch_size == 32)
276 r_sym_mask = ~(bfd_vma) 0xff;
277 else
278 r_sym_mask = ~(bfd_vma) 0xffffffff;
279
e717da7e
AM
280 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
281 if (lo->type == bfd_indirect_link_order)
db6751f2 282 {
947216bf 283 bfd_byte *erel, *erelend;
e717da7e 284 asection *o = lo->u.indirect.section;
db6751f2 285
947216bf
AM
286 erel = o->contents;
287 erelend = o->contents + o->_raw_size;
288 p = sort + o->output_offset / ext_size * sort_elt;
289 while (erel < erelend)
db6751f2 290 {
947216bf
AM
291 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
292 (*swap_in) (abfd, erel, s->rela);
293 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
140f6c8e 294 s->u.sym_mask = r_sym_mask;
947216bf
AM
295 p += sort_elt;
296 erel += ext_size;
db6751f2
JJ
297 }
298 }
299
268b6b39 300 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
947216bf
AM
301
302 for (i = 0, p = sort; i < count; i++, p += sort_elt)
db6751f2 303 {
947216bf
AM
304 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
305 if (s->type != reloc_class_relative)
306 break;
db6751f2 307 }
947216bf
AM
308 ret = i;
309 s_non_relative = p;
310
311 sq = (struct elf_link_sort_rela *) s_non_relative;
312 for (; i < count; i++, p += sort_elt)
313 {
314 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
140f6c8e 315 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
947216bf 316 sq = sp;
140f6c8e 317 sp->u.offset = sq->rela->r_offset;
947216bf
AM
318 }
319
268b6b39 320 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
dc810e39 321
e717da7e
AM
322 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
323 if (lo->type == bfd_indirect_link_order)
db6751f2 324 {
947216bf 325 bfd_byte *erel, *erelend;
e717da7e 326 asection *o = lo->u.indirect.section;
db6751f2 327
947216bf
AM
328 erel = o->contents;
329 erelend = o->contents + o->_raw_size;
330 p = sort + o->output_offset / ext_size * sort_elt;
331 while (erel < erelend)
db6751f2 332 {
947216bf
AM
333 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
334 (*swap_out) (abfd, s->rela, erel);
335 p += sort_elt;
336 erel += ext_size;
db6751f2
JJ
337 }
338 }
339
5ed6aba4 340 free (sort);
db6751f2
JJ
341 *psec = reldyn;
342 return ret;
343}
344
252b5132
RH
345/* Do the final step of an ELF link. */
346
b34976b6 347bfd_boolean
268b6b39 348elf_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132 349{
b34976b6
AM
350 bfd_boolean dynamic;
351 bfd_boolean emit_relocs;
252b5132
RH
352 bfd *dynobj;
353 struct elf_final_link_info finfo;
354 register asection *o;
355 register struct bfd_link_order *p;
356 register bfd *sub;
dc810e39
AM
357 bfd_size_type max_contents_size;
358 bfd_size_type max_external_reloc_size;
359 bfd_size_type max_internal_reloc_count;
360 bfd_size_type max_sym_count;
9ad5cbcf 361 bfd_size_type max_sym_shndx_count;
252b5132
RH
362 file_ptr off;
363 Elf_Internal_Sym elfsym;
364 unsigned int i;
365 Elf_Internal_Shdr *symtab_hdr;
c97e73dd 366 Elf_Internal_Shdr *symtab_shndx_hdr;
252b5132 367 Elf_Internal_Shdr *symstrtab_hdr;
9c5bfbb7 368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 369 struct elf_outext_info eoinfo;
b34976b6 370 bfd_boolean merged;
db6751f2
JJ
371 size_t relativecount = 0;
372 asection *reldyn = 0;
dc810e39 373 bfd_size_type amt;
252b5132 374
0eddce27 375 if (! is_elf_hash_table (info->hash))
b34976b6 376 return FALSE;
8ea2e4bd 377
252b5132
RH
378 if (info->shared)
379 abfd->flags |= DYNAMIC;
380
381 dynamic = elf_hash_table (info)->dynamic_sections_created;
382 dynobj = elf_hash_table (info)->dynobj;
383
1049f94e 384 emit_relocs = (info->relocatable
c44233aa
AM
385 || info->emitrelocations
386 || bed->elf_backend_emit_relocs);
9317eacc 387
252b5132
RH
388 finfo.info = info;
389 finfo.output_bfd = abfd;
140f6c8e 390 finfo.symstrtab = _bfd_elf_stringtab_init ();
252b5132 391 if (finfo.symstrtab == NULL)
b34976b6 392 return FALSE;
252b5132
RH
393
394 if (! dynamic)
395 {
396 finfo.dynsym_sec = NULL;
397 finfo.hash_sec = NULL;
398 finfo.symver_sec = NULL;
399 }
400 else
401 {
402 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
403 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
404 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
405 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
406 /* Note that it is OK if symver_sec is NULL. */
407 }
408
409 finfo.contents = NULL;
410 finfo.external_relocs = NULL;
411 finfo.internal_relocs = NULL;
412 finfo.external_syms = NULL;
9ad5cbcf 413 finfo.locsym_shndx = NULL;
252b5132
RH
414 finfo.internal_syms = NULL;
415 finfo.indices = NULL;
416 finfo.sections = NULL;
417 finfo.symbuf = NULL;
9ad5cbcf 418 finfo.symshndxbuf = NULL;
252b5132 419 finfo.symbuf_count = 0;
c97e73dd 420 finfo.shndxbuf_size = 0;
252b5132
RH
421
422 /* Count up the number of relocations we will output for each output
423 section, so that we know the sizes of the reloc sections. We
424 also figure out some maximum sizes. */
425 max_contents_size = 0;
426 max_external_reloc_size = 0;
427 max_internal_reloc_count = 0;
428 max_sym_count = 0;
9ad5cbcf 429 max_sym_shndx_count = 0;
b34976b6 430 merged = FALSE;
268b6b39 431 for (o = abfd->sections; o != NULL; o = o->next)
252b5132 432 {
3f9a32bd 433 struct bfd_elf_section_data *esdo = elf_section_data (o);
252b5132
RH
434 o->reloc_count = 0;
435
436 for (p = o->link_order_head; p != NULL; p = p->next)
437 {
3f9a32bd
AM
438 unsigned int reloc_count = 0;
439 struct bfd_elf_section_data *esdi = NULL;
440 unsigned int *rel_count1;
441
252b5132
RH
442 if (p->type == bfd_section_reloc_link_order
443 || p->type == bfd_symbol_reloc_link_order)
3f9a32bd 444 reloc_count = 1;
252b5132
RH
445 else if (p->type == bfd_indirect_link_order)
446 {
447 asection *sec;
448
449 sec = p->u.indirect.section;
3f9a32bd 450 esdi = elf_section_data (sec);
252b5132
RH
451
452 /* Mark all sections which are to be included in the
453 link. This will normally be every section. We need
454 to do this so that we can identify any sections which
455 the linker has decided to not include. */
b34976b6 456 sec->linker_mark = TRUE;
252b5132 457
f5fa8ca2 458 if (sec->flags & SEC_MERGE)
b34976b6 459 merged = TRUE;
f5fa8ca2 460
1049f94e 461 if (info->relocatable || info->emitrelocations)
3f9a32bd 462 reloc_count = sec->reloc_count;
c44233aa 463 else if (bed->elf_backend_count_relocs)
9317eacc
CM
464 {
465 Elf_Internal_Rela * relocs;
466
268b6b39
AM
467 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
468 info->keep_memory);
9317eacc 469
3f9a32bd 470 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
9317eacc 471
6cdc0ccc 472 if (elf_section_data (o)->relocs != relocs)
9317eacc
CM
473 free (relocs);
474 }
252b5132
RH
475
476 if (sec->_raw_size > max_contents_size)
477 max_contents_size = sec->_raw_size;
478 if (sec->_cooked_size > max_contents_size)
479 max_contents_size = sec->_cooked_size;
480
481 /* We are interested in just local symbols, not all
482 symbols. */
483 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
484 && (sec->owner->flags & DYNAMIC) == 0)
485 {
486 size_t sym_count;
487
488 if (elf_bad_symtab (sec->owner))
489 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
140f6c8e 490 / bed->s->sizeof_sym);
252b5132
RH
491 else
492 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
493
494 if (sym_count > max_sym_count)
495 max_sym_count = sym_count;
496
9ad5cbcf
AM
497 if (sym_count > max_sym_shndx_count
498 && elf_symtab_shndx (sec->owner) != 0)
499 max_sym_shndx_count = sym_count;
500
252b5132
RH
501 if ((sec->flags & SEC_RELOC) != 0)
502 {
503 size_t ext_size;
504
505 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
506 if (ext_size > max_external_reloc_size)
507 max_external_reloc_size = ext_size;
508 if (sec->reloc_count > max_internal_reloc_count)
509 max_internal_reloc_count = sec->reloc_count;
510 }
511 }
512 }
3f9a32bd
AM
513
514 if (reloc_count == 0)
515 continue;
516
517 o->reloc_count += reloc_count;
518
519 /* MIPS may have a mix of REL and RELA relocs on sections.
520 To support this curious ABI we keep reloc counts in
521 elf_section_data too. We must be careful to add the
522 relocations from the input section to the right output
523 count. FIXME: Get rid of one count. We have
524 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
525 rel_count1 = &esdo->rel_count;
526 if (esdi != NULL)
527 {
528 bfd_boolean same_size;
529 bfd_size_type entsize1;
530
531 entsize1 = esdi->rel_hdr.sh_entsize;
140f6c8e
AM
532 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
533 || entsize1 == bed->s->sizeof_rela);
534 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
3f9a32bd
AM
535
536 if (!same_size)
537 rel_count1 = &esdo->rel_count2;
538
539 if (esdi->rel_hdr2 != NULL)
540 {
541 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
542 unsigned int alt_count;
543 unsigned int *rel_count2;
544
545 BFD_ASSERT (entsize2 != entsize1
140f6c8e
AM
546 && (entsize2 == bed->s->sizeof_rel
547 || entsize2 == bed->s->sizeof_rela));
3f9a32bd
AM
548
549 rel_count2 = &esdo->rel_count2;
550 if (!same_size)
551 rel_count2 = &esdo->rel_count;
552
553 /* The following is probably too simplistic if the
554 backend counts output relocs unusually. */
555 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
556 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
557 *rel_count2 += alt_count;
558 reloc_count -= alt_count;
559 }
560 }
561 *rel_count1 += reloc_count;
252b5132
RH
562 }
563
564 if (o->reloc_count > 0)
565 o->flags |= SEC_RELOC;
566 else
567 {
568 /* Explicitly clear the SEC_RELOC flag. The linker tends to
569 set it (this is probably a bug) and if it is set
570 assign_section_numbers will create a reloc section. */
571 o->flags &=~ SEC_RELOC;
572 }
573
574 /* If the SEC_ALLOC flag is not set, force the section VMA to
575 zero. This is done in elf_fake_sections as well, but forcing
576 the VMA to 0 here will ensure that relocs against these
577 sections are handled correctly. */
578 if ((o->flags & SEC_ALLOC) == 0
579 && ! o->user_set_vma)
580 o->vma = 0;
581 }
582
1049f94e 583 if (! info->relocatable && merged)
f5fa8ca2 584 elf_link_hash_traverse (elf_hash_table (info),
268b6b39 585 _bfd_elf_link_sec_merge_syms, abfd);
f5fa8ca2 586
252b5132
RH
587 /* Figure out the file positions for everything but the symbol table
588 and the relocs. We set symcount to force assign_section_numbers
589 to create a symbol table. */
590 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
591 BFD_ASSERT (! abfd->output_has_begun);
592 if (! _bfd_elf_compute_section_file_positions (abfd, info))
593 goto error_return;
594
595 /* That created the reloc sections. Set their sizes, and assign
596 them file positions, and allocate some buffers. */
597 for (o = abfd->sections; o != NULL; o = o->next)
598 {
599 if ((o->flags & SEC_RELOC) != 0)
600 {
45d6a902
AM
601 if (!(_bfd_elf_link_size_reloc_section
602 (abfd, &elf_section_data (o)->rel_hdr, o)))
252b5132
RH
603 goto error_return;
604
23bc299b 605 if (elf_section_data (o)->rel_hdr2
45d6a902
AM
606 && !(_bfd_elf_link_size_reloc_section
607 (abfd, elf_section_data (o)->rel_hdr2, o)))
252b5132 608 goto error_return;
252b5132 609 }
b037af20
MM
610
611 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
3e932841 612 to count upwards while actually outputting the relocations. */
b037af20
MM
613 elf_section_data (o)->rel_count = 0;
614 elf_section_data (o)->rel_count2 = 0;
252b5132
RH
615 }
616
617 _bfd_elf_assign_file_positions_for_relocs (abfd);
618
619 /* We have now assigned file positions for all the sections except
620 .symtab and .strtab. We start the .symtab section at the current
621 file position, and write directly to it. We build the .strtab
622 section in memory. */
623 bfd_get_symcount (abfd) = 0;
624 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
625 /* sh_name is set in prep_headers. */
626 symtab_hdr->sh_type = SHT_SYMTAB;
c97e73dd 627 /* sh_flags, sh_addr and sh_size all start off zero. */
140f6c8e 628 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
252b5132
RH
629 /* sh_link is set in assign_section_numbers. */
630 /* sh_info is set below. */
631 /* sh_offset is set just below. */
45d6a902 632 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
252b5132
RH
633
634 off = elf_tdata (abfd)->next_file_pos;
b34976b6 635 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
252b5132
RH
636
637 /* Note that at this point elf_tdata (abfd)->next_file_pos is
638 incorrect. We do not yet know the size of the .symtab section.
639 We correct next_file_pos below, after we do know the size. */
640
641 /* Allocate a buffer to hold swapped out symbols. This is to avoid
642 continuously seeking to the right position in the file. */
643 if (! info->keep_memory || max_sym_count < 20)
644 finfo.symbuf_size = 20;
645 else
646 finfo.symbuf_size = max_sym_count;
dc810e39 647 amt = finfo.symbuf_size;
140f6c8e 648 amt *= bed->s->sizeof_sym;
268b6b39 649 finfo.symbuf = bfd_malloc (amt);
252b5132
RH
650 if (finfo.symbuf == NULL)
651 goto error_return;
9ad5cbcf
AM
652 if (elf_numsections (abfd) > SHN_LORESERVE)
653 {
c97e73dd
AM
654 /* Wild guess at number of output symbols. realloc'd as needed. */
655 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
656 finfo.shndxbuf_size = amt;
9ad5cbcf 657 amt *= sizeof (Elf_External_Sym_Shndx);
268b6b39 658 finfo.symshndxbuf = bfd_zmalloc (amt);
9ad5cbcf
AM
659 if (finfo.symshndxbuf == NULL)
660 goto error_return;
661 }
252b5132
RH
662
663 /* Start writing out the symbol table. The first symbol is always a
664 dummy symbol. */
9317eacc
CM
665 if (info->strip != strip_all
666 || emit_relocs)
252b5132
RH
667 {
668 elfsym.st_value = 0;
669 elfsym.st_size = 0;
670 elfsym.st_info = 0;
671 elfsym.st_other = 0;
672 elfsym.st_shndx = SHN_UNDEF;
754021d0
AM
673 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
674 NULL))
252b5132
RH
675 goto error_return;
676 }
677
678#if 0
679 /* Some standard ELF linkers do this, but we don't because it causes
680 bootstrap comparison failures. */
681 /* Output a file symbol for the output file as the second symbol.
682 We output this even if we are discarding local symbols, although
683 I'm not sure if this is correct. */
684 elfsym.st_value = 0;
685 elfsym.st_size = 0;
686 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
687 elfsym.st_other = 0;
688 elfsym.st_shndx = SHN_ABS;
689 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
754021d0 690 &elfsym, bfd_abs_section_ptr, NULL))
252b5132
RH
691 goto error_return;
692#endif
693
694 /* Output a symbol for each section. We output these even if we are
695 discarding local symbols, since they are used for relocs. These
696 symbols have no names. We store the index of each one in the
697 index field of the section, so that we can find it again when
698 outputting relocs. */
9317eacc
CM
699 if (info->strip != strip_all
700 || emit_relocs)
252b5132
RH
701 {
702 elfsym.st_size = 0;
703 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
704 elfsym.st_other = 0;
9ad5cbcf 705 for (i = 1; i < elf_numsections (abfd); i++)
252b5132 706 {
140f6c8e 707 o = bfd_section_from_elf_index (abfd, i);
252b5132
RH
708 if (o != NULL)
709 o->target_index = bfd_get_symcount (abfd);
710 elfsym.st_shndx = i;
1049f94e 711 if (info->relocatable || o == NULL)
252b5132
RH
712 elfsym.st_value = 0;
713 else
714 elfsym.st_value = o->vma;
754021d0 715 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
252b5132 716 goto error_return;
c97e73dd 717 if (i == SHN_LORESERVE - 1)
9ad5cbcf 718 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
719 }
720 }
721
722 /* Allocate some memory to hold information read in from the input
723 files. */
9ad5cbcf
AM
724 if (max_contents_size != 0)
725 {
268b6b39 726 finfo.contents = bfd_malloc (max_contents_size);
9ad5cbcf
AM
727 if (finfo.contents == NULL)
728 goto error_return;
729 }
730
731 if (max_external_reloc_size != 0)
732 {
268b6b39 733 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
9ad5cbcf
AM
734 if (finfo.external_relocs == NULL)
735 goto error_return;
736 }
737
738 if (max_internal_reloc_count != 0)
739 {
740 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
741 amt *= sizeof (Elf_Internal_Rela);
268b6b39 742 finfo.internal_relocs = bfd_malloc (amt);
9ad5cbcf
AM
743 if (finfo.internal_relocs == NULL)
744 goto error_return;
745 }
746
747 if (max_sym_count != 0)
748 {
140f6c8e 749 amt = max_sym_count * bed->s->sizeof_sym;
268b6b39 750 finfo.external_syms = bfd_malloc (amt);
9ad5cbcf
AM
751 if (finfo.external_syms == NULL)
752 goto error_return;
753
754 amt = max_sym_count * sizeof (Elf_Internal_Sym);
268b6b39 755 finfo.internal_syms = bfd_malloc (amt);
9ad5cbcf
AM
756 if (finfo.internal_syms == NULL)
757 goto error_return;
758
759 amt = max_sym_count * sizeof (long);
268b6b39 760 finfo.indices = bfd_malloc (amt);
9ad5cbcf
AM
761 if (finfo.indices == NULL)
762 goto error_return;
763
764 amt = max_sym_count * sizeof (asection *);
268b6b39 765 finfo.sections = bfd_malloc (amt);
9ad5cbcf
AM
766 if (finfo.sections == NULL)
767 goto error_return;
768 }
769
770 if (max_sym_shndx_count != 0)
771 {
772 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
268b6b39 773 finfo.locsym_shndx = bfd_malloc (amt);
9ad5cbcf
AM
774 if (finfo.locsym_shndx == NULL)
775 goto error_return;
776 }
252b5132 777
e1918d23 778 if (elf_hash_table (info)->tls_sec)
13ae64f3 779 {
e1918d23 780 bfd_vma base, end = 0;
13ae64f3
JJ
781 asection *sec;
782
e1918d23 783 for (sec = elf_hash_table (info)->tls_sec;
13ae64f3
JJ
784 sec && (sec->flags & SEC_THREAD_LOCAL);
785 sec = sec->next)
786 {
787 bfd_vma size = sec->_raw_size;
788
e1918d23 789 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
13ae64f3
JJ
790 {
791 struct bfd_link_order *o;
792
13ae64f3
JJ
793 for (o = sec->link_order_head; o != NULL; o = o->next)
794 if (size < o->offset + o->size)
58821868 795 size = o->offset + o->size;
13ae64f3
JJ
796 }
797 end = sec->vma + size;
798 }
e1918d23
AM
799 base = elf_hash_table (info)->tls_sec->vma;
800 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
801 elf_hash_table (info)->tls_size = end - base;
13ae64f3
JJ
802 }
803
252b5132
RH
804 /* Since ELF permits relocations to be against local symbols, we
805 must have the local symbols available when we do the relocations.
806 Since we would rather only read the local symbols once, and we
807 would rather not keep them in memory, we handle all the
808 relocations for a single input file at the same time.
809
810 Unfortunately, there is no way to know the total number of local
811 symbols until we have seen all of them, and the local symbol
812 indices precede the global symbol indices. This means that when
1049f94e 813 we are generating relocatable output, and we see a reloc against
252b5132
RH
814 a global symbol, we can not know the symbol index until we have
815 finished examining all the local symbols to see which ones we are
816 going to output. To deal with this, we keep the relocations in
817 memory, and don't output them until the end of the link. This is
818 an unfortunate waste of memory, but I don't see a good way around
1049f94e 819 it. Fortunately, it only happens when performing a relocatable
252b5132
RH
820 link, which is not the common case. FIXME: If keep_memory is set
821 we could write the relocs out and then read them again; I don't
822 know how bad the memory loss will be. */
823
824 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
b34976b6 825 sub->output_has_begun = FALSE;
252b5132
RH
826 for (o = abfd->sections; o != NULL; o = o->next)
827 {
828 for (p = o->link_order_head; p != NULL; p = p->next)
829 {
830 if (p->type == bfd_indirect_link_order
a50c1845 831 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
4ddafab0 832 == bfd_target_elf_flavour)
a50c1845 833 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
252b5132 834 {
252b5132
RH
835 if (! sub->output_has_begun)
836 {
837 if (! elf_link_input_bfd (&finfo, sub))
838 goto error_return;
b34976b6 839 sub->output_has_begun = TRUE;
252b5132
RH
840 }
841 }
842 else if (p->type == bfd_section_reloc_link_order
843 || p->type == bfd_symbol_reloc_link_order)
844 {
845 if (! elf_reloc_link_order (abfd, info, o, p))
846 goto error_return;
847 }
848 else
849 {
850 if (! _bfd_default_link_order (abfd, info, o, p))
851 goto error_return;
852 }
853 }
854 }
855
c44233aa
AM
856 /* Output any global symbols that got converted to local in a
857 version script or due to symbol visibility. We do this in a
858 separate step since ELF requires all local symbols to appear
859 prior to any global symbols. FIXME: We should only do this if
860 some global symbols were, in fact, converted to become local.
861 FIXME: Will this work correctly with the Irix 5 linker? */
b34976b6 862 eoinfo.failed = FALSE;
c44233aa 863 eoinfo.finfo = &finfo;
b34976b6 864 eoinfo.localsyms = TRUE;
c44233aa 865 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 866 &eoinfo);
c44233aa 867 if (eoinfo.failed)
b34976b6 868 return FALSE;
c44233aa 869
252b5132 870 /* That wrote out all the local symbols. Finish up the symbol table
5cc7c785
L
871 with the global symbols. Even if we want to strip everything we
872 can, we still need to deal with those global symbols that got
3e932841 873 converted to local in a version script. */
252b5132 874
30b30c21 875 /* The sh_info field records the index of the first non local symbol. */
252b5132 876 symtab_hdr->sh_info = bfd_get_symcount (abfd);
30b30c21 877
fc8c40a0
AM
878 if (dynamic
879 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
30b30c21
RH
880 {
881 Elf_Internal_Sym sym;
140f6c8e 882 bfd_byte *dynsym = finfo.dynsym_sec->contents;
71a40b32 883 long last_local = 0;
30b30c21
RH
884
885 /* Write out the section symbols for the output sections. */
886 if (info->shared)
887 {
888 asection *s;
889
890 sym.st_size = 0;
891 sym.st_name = 0;
892 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
893 sym.st_other = 0;
894
895 for (s = abfd->sections; s != NULL; s = s->next)
896 {
897 int indx;
140f6c8e
AM
898 bfd_byte *dest;
899 long dynindx;
9ad5cbcf 900
30b30c21 901 indx = elf_section_data (s)->this_idx;
140f6c8e 902 dynindx = elf_section_data (s)->dynindx;
30b30c21
RH
903 BFD_ASSERT (indx > 0);
904 sym.st_shndx = indx;
905 sym.st_value = s->vma;
140f6c8e
AM
906 dest = dynsym + dynindx * bed->s->sizeof_sym;
907 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
908 }
909
910 last_local = bfd_count_sections (abfd);
911 }
912
913 /* Write out the local dynsyms. */
914 if (elf_hash_table (info)->dynlocal)
915 {
916 struct elf_link_local_dynamic_entry *e;
917 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
918 {
318da145 919 asection *s;
140f6c8e 920 bfd_byte *dest;
30b30c21 921
b037af20
MM
922 sym.st_size = e->isym.st_size;
923 sym.st_other = e->isym.st_other;
924
1fa0ddb3
RH
925 /* Copy the internal symbol as is.
926 Note that we saved a word of storage and overwrote
c44233aa
AM
927 the original st_name with the dynstr_index. */
928 sym = e->isym;
30b30c21 929
c8e5ddc8 930 if (e->isym.st_shndx != SHN_UNDEF
58821868
AM
931 && (e->isym.st_shndx < SHN_LORESERVE
932 || e->isym.st_shndx > SHN_HIRESERVE))
587ff49e
RH
933 {
934 s = bfd_section_from_elf_index (e->input_bfd,
935 e->isym.st_shndx);
936
937 sym.st_shndx =
938 elf_section_data (s->output_section)->this_idx;
939 sym.st_value = (s->output_section->vma
940 + s->output_offset
941 + e->isym.st_value);
942 }
30b30c21
RH
943
944 if (last_local < e->dynindx)
945 last_local = e->dynindx;
946
140f6c8e
AM
947 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
948 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
30b30c21
RH
949 }
950 }
951
71a40b32
ILT
952 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
953 last_local + 1;
30b30c21 954 }
252b5132
RH
955
956 /* We get the global symbols from the hash table. */
b34976b6
AM
957 eoinfo.failed = FALSE;
958 eoinfo.localsyms = FALSE;
252b5132
RH
959 eoinfo.finfo = &finfo;
960 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
268b6b39 961 &eoinfo);
252b5132 962 if (eoinfo.failed)
b34976b6 963 return FALSE;
252b5132 964
587ff49e
RH
965 /* If backend needs to output some symbols not present in the hash
966 table, do it now. */
967 if (bed->elf_backend_output_arch_syms)
968 {
b34976b6 969 typedef bfd_boolean (*out_sym_func)
754021d0
AM
970 (void *, const char *, Elf_Internal_Sym *, asection *,
971 struct elf_link_hash_entry *);
dc810e39
AM
972
973 if (! ((*bed->elf_backend_output_arch_syms)
268b6b39 974 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
b34976b6 975 return FALSE;
3e932841 976 }
587ff49e 977
252b5132 978 /* Flush all symbols to the file. */
140f6c8e 979 if (! elf_link_flush_output_syms (&finfo, bed))
b34976b6 980 return FALSE;
252b5132
RH
981
982 /* Now we know the size of the symtab section. */
983 off += symtab_hdr->sh_size;
984
c97e73dd
AM
985 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
986 if (symtab_shndx_hdr->sh_name != 0)
987 {
988 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
989 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
990 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
991 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
992 symtab_shndx_hdr->sh_size = amt;
993
994 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
b34976b6 995 off, TRUE);
c97e73dd
AM
996
997 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
268b6b39 998 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
b34976b6 999 return FALSE;
c97e73dd
AM
1000 }
1001
1002
252b5132
RH
1003 /* Finish up and write out the symbol string table (.strtab)
1004 section. */
1005 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
1006 /* sh_name was set in prep_headers. */
1007 symstrtab_hdr->sh_type = SHT_STRTAB;
1008 symstrtab_hdr->sh_flags = 0;
1009 symstrtab_hdr->sh_addr = 0;
1010 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
1011 symstrtab_hdr->sh_entsize = 0;
1012 symstrtab_hdr->sh_link = 0;
1013 symstrtab_hdr->sh_info = 0;
1014 /* sh_offset is set just below. */
1015 symstrtab_hdr->sh_addralign = 1;
1016
b34976b6 1017 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
252b5132
RH
1018 elf_tdata (abfd)->next_file_pos = off;
1019
1020 if (bfd_get_symcount (abfd) > 0)
1021 {
1022 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
1023 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
b34976b6 1024 return FALSE;
252b5132
RH
1025 }
1026
1027 /* Adjust the relocs to have the correct symbol indices. */
1028 for (o = abfd->sections; o != NULL; o = o->next)
1029 {
252b5132
RH
1030 if ((o->flags & SEC_RELOC) == 0)
1031 continue;
1032
3e932841 1033 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
31367b81
MM
1034 elf_section_data (o)->rel_count,
1035 elf_section_data (o)->rel_hashes);
1036 if (elf_section_data (o)->rel_hdr2 != NULL)
1037 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
1038 elf_section_data (o)->rel_count2,
3e932841 1039 (elf_section_data (o)->rel_hashes
31367b81 1040 + elf_section_data (o)->rel_count));
252b5132
RH
1041
1042 /* Set the reloc_count field to 0 to prevent write_relocs from
1043 trying to swap the relocs out itself. */
1044 o->reloc_count = 0;
1045 }
1046
db6751f2
JJ
1047 if (dynamic && info->combreloc && dynobj != NULL)
1048 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
1049
252b5132
RH
1050 /* If we are linking against a dynamic object, or generating a
1051 shared library, finish up the dynamic linking information. */
1052 if (dynamic)
1053 {
140f6c8e 1054 bfd_byte *dyncon, *dynconend;
252b5132
RH
1055
1056 /* Fix up .dynamic entries. */
1057 o = bfd_get_section_by_name (dynobj, ".dynamic");
1058 BFD_ASSERT (o != NULL);
1059
140f6c8e
AM
1060 dyncon = o->contents;
1061 dynconend = o->contents + o->_raw_size;
1062 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
252b5132
RH
1063 {
1064 Elf_Internal_Dyn dyn;
1065 const char *name;
1066 unsigned int type;
1067
140f6c8e 1068 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
252b5132
RH
1069
1070 switch (dyn.d_tag)
1071 {
1072 default:
140f6c8e 1073 continue;
db6751f2 1074 case DT_NULL:
140f6c8e 1075 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
db6751f2
JJ
1076 {
1077 switch (elf_section_data (reldyn)->this_hdr.sh_type)
1078 {
1079 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
1080 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
140f6c8e 1081 default: continue;
db6751f2 1082 }
140f6c8e
AM
1083 dyn.d_un.d_val = relativecount;
1084 relativecount = 0;
1085 break;
db6751f2 1086 }
140f6c8e
AM
1087 continue;
1088
252b5132 1089 case DT_INIT:
f0c2e336 1090 name = info->init_function;
252b5132
RH
1091 goto get_sym;
1092 case DT_FINI:
f0c2e336 1093 name = info->fini_function;
252b5132
RH
1094 get_sym:
1095 {
1096 struct elf_link_hash_entry *h;
1097
1098 h = elf_link_hash_lookup (elf_hash_table (info), name,
b34976b6 1099 FALSE, FALSE, TRUE);
252b5132
RH
1100 if (h != NULL
1101 && (h->root.type == bfd_link_hash_defined
1102 || h->root.type == bfd_link_hash_defweak))
1103 {
1104 dyn.d_un.d_val = h->root.u.def.value;
1105 o = h->root.u.def.section;
1106 if (o->output_section != NULL)
1107 dyn.d_un.d_val += (o->output_section->vma
1108 + o->output_offset);
1109 else
1110 {
1111 /* The symbol is imported from another shared
1112 library and does not apply to this one. */
1113 dyn.d_un.d_val = 0;
1114 }
140f6c8e 1115 break;
252b5132
RH
1116 }
1117 }
140f6c8e 1118 continue;
252b5132 1119
30831527
RH
1120 case DT_PREINIT_ARRAYSZ:
1121 name = ".preinit_array";
1122 goto get_size;
1123 case DT_INIT_ARRAYSZ:
1124 name = ".init_array";
1125 goto get_size;
1126 case DT_FINI_ARRAYSZ:
1127 name = ".fini_array";
1128 get_size:
1129 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
1130 if (o == NULL)
1131 {
1132 (*_bfd_error_handler)
1133 (_("%s: could not find output section %s"),
1134 bfd_get_filename (abfd), name);
1135 goto error_return;
1136 }
25e27870
L
1137 if (o->_raw_size == 0)
1138 (*_bfd_error_handler)
1139 (_("warning: %s section has zero size"), name);
30831527 1140 dyn.d_un.d_val = o->_raw_size;
30831527
RH
1141 break;
1142
1143 case DT_PREINIT_ARRAY:
1144 name = ".preinit_array";
1145 goto get_vma;
1146 case DT_INIT_ARRAY:
1147 name = ".init_array";
1148 goto get_vma;
1149 case DT_FINI_ARRAY:
1150 name = ".fini_array";
1151 goto get_vma;
1152
252b5132
RH
1153 case DT_HASH:
1154 name = ".hash";
1155 goto get_vma;
1156 case DT_STRTAB:
1157 name = ".dynstr";
1158 goto get_vma;
1159 case DT_SYMTAB:
1160 name = ".dynsym";
1161 goto get_vma;
1162 case DT_VERDEF:
1163 name = ".gnu.version_d";
1164 goto get_vma;
1165 case DT_VERNEED:
1166 name = ".gnu.version_r";
1167 goto get_vma;
1168 case DT_VERSYM:
1169 name = ".gnu.version";
1170 get_vma:
1171 o = bfd_get_section_by_name (abfd, name);
2cb69dd3
JL
1172 if (o == NULL)
1173 {
1174 (*_bfd_error_handler)
1175 (_("%s: could not find output section %s"),
1176 bfd_get_filename (abfd), name);
1177 goto error_return;
1178 }
252b5132 1179 dyn.d_un.d_ptr = o->vma;
252b5132
RH
1180 break;
1181
1182 case DT_REL:
1183 case DT_RELA:
1184 case DT_RELSZ:
1185 case DT_RELASZ:
1186 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
1187 type = SHT_REL;
1188 else
1189 type = SHT_RELA;
1190 dyn.d_un.d_val = 0;
9ad5cbcf 1191 for (i = 1; i < elf_numsections (abfd); i++)
252b5132
RH
1192 {
1193 Elf_Internal_Shdr *hdr;
1194
1195 hdr = elf_elfsections (abfd)[i];
1196 if (hdr->sh_type == type
1197 && (hdr->sh_flags & SHF_ALLOC) != 0)
1198 {
1199 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
1200 dyn.d_un.d_val += hdr->sh_size;
1201 else
1202 {
1203 if (dyn.d_un.d_val == 0
1204 || hdr->sh_addr < dyn.d_un.d_val)
1205 dyn.d_un.d_val = hdr->sh_addr;
1206 }
1207 }
1208 }
252b5132
RH
1209 break;
1210 }
140f6c8e 1211 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
252b5132
RH
1212 }
1213 }
1214
1215 /* If we have created any dynamic sections, then output them. */
1216 if (dynobj != NULL)
1217 {
1218 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
1219 goto error_return;
1220
1221 for (o = dynobj->sections; o != NULL; o = o->next)
1222 {
1223 if ((o->flags & SEC_HAS_CONTENTS) == 0
fc8c40a0
AM
1224 || o->_raw_size == 0
1225 || o->output_section == bfd_abs_section_ptr)
252b5132
RH
1226 continue;
1227 if ((o->flags & SEC_LINKER_CREATED) == 0)
1228 {
1229 /* At this point, we are only interested in sections
45d6a902 1230 created by _bfd_elf_link_create_dynamic_sections. */
252b5132
RH
1231 continue;
1232 }
1233 if ((elf_section_data (o->output_section)->this_hdr.sh_type
1234 != SHT_STRTAB)
1235 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
1236 {
1237 if (! bfd_set_section_contents (abfd, o->output_section,
dc810e39
AM
1238 o->contents,
1239 (file_ptr) o->output_offset,
252b5132
RH
1240 o->_raw_size))
1241 goto error_return;
1242 }
1243 else
1244 {
252b5132 1245 /* The contents of the .dynstr section are actually in a
c44233aa 1246 stringtab. */
252b5132
RH
1247 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
1248 if (bfd_seek (abfd, off, SEEK_SET) != 0
2b0f7ef9
JJ
1249 || ! _bfd_elf_strtab_emit (abfd,
1250 elf_hash_table (info)->dynstr))
252b5132
RH
1251 goto error_return;
1252 }
1253 }
1254 }
1255
1049f94e 1256 if (info->relocatable)
1126897b 1257 {
b34976b6 1258 bfd_boolean failed = FALSE;
1126897b
AM
1259
1260 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
1261 if (failed)
1262 goto error_return;
1263 }
1264
252b5132
RH
1265 /* If we have optimized stabs strings, output them. */
1266 if (elf_hash_table (info)->stab_info != NULL)
1267 {
1268 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
1269 goto error_return;
1270 }
1271
126495ed 1272 if (info->eh_frame_hdr)
65765700 1273 {
126495ed
AM
1274 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
1275 goto error_return;
65765700
JJ
1276 }
1277
252b5132
RH
1278 if (finfo.symstrtab != NULL)
1279 _bfd_stringtab_free (finfo.symstrtab);
1280 if (finfo.contents != NULL)
1281 free (finfo.contents);
1282 if (finfo.external_relocs != NULL)
1283 free (finfo.external_relocs);
1284 if (finfo.internal_relocs != NULL)
1285 free (finfo.internal_relocs);
1286 if (finfo.external_syms != NULL)
1287 free (finfo.external_syms);
9ad5cbcf
AM
1288 if (finfo.locsym_shndx != NULL)
1289 free (finfo.locsym_shndx);
252b5132
RH
1290 if (finfo.internal_syms != NULL)
1291 free (finfo.internal_syms);
1292 if (finfo.indices != NULL)
1293 free (finfo.indices);
1294 if (finfo.sections != NULL)
1295 free (finfo.sections);
1296 if (finfo.symbuf != NULL)
1297 free (finfo.symbuf);
9ad5cbcf 1298 if (finfo.symshndxbuf != NULL)
c97e73dd 1299 free (finfo.symshndxbuf);
252b5132
RH
1300 for (o = abfd->sections; o != NULL; o = o->next)
1301 {
1302 if ((o->flags & SEC_RELOC) != 0
1303 && elf_section_data (o)->rel_hashes != NULL)
c44233aa 1304 free (elf_section_data (o)->rel_hashes);
252b5132
RH
1305 }
1306
b34976b6 1307 elf_tdata (abfd)->linker = TRUE;
252b5132 1308
b34976b6 1309 return TRUE;
252b5132
RH
1310
1311 error_return:
1312 if (finfo.symstrtab != NULL)
1313 _bfd_stringtab_free (finfo.symstrtab);
1314 if (finfo.contents != NULL)
1315 free (finfo.contents);
1316 if (finfo.external_relocs != NULL)
1317 free (finfo.external_relocs);
1318 if (finfo.internal_relocs != NULL)
1319 free (finfo.internal_relocs);
1320 if (finfo.external_syms != NULL)
1321 free (finfo.external_syms);
9ad5cbcf
AM
1322 if (finfo.locsym_shndx != NULL)
1323 free (finfo.locsym_shndx);
252b5132
RH
1324 if (finfo.internal_syms != NULL)
1325 free (finfo.internal_syms);
1326 if (finfo.indices != NULL)
1327 free (finfo.indices);
1328 if (finfo.sections != NULL)
1329 free (finfo.sections);
1330 if (finfo.symbuf != NULL)
1331 free (finfo.symbuf);
9ad5cbcf 1332 if (finfo.symshndxbuf != NULL)
c97e73dd 1333 free (finfo.symshndxbuf);
252b5132
RH
1334 for (o = abfd->sections; o != NULL; o = o->next)
1335 {
1336 if ((o->flags & SEC_RELOC) != 0
1337 && elf_section_data (o)->rel_hashes != NULL)
1338 free (elf_section_data (o)->rel_hashes);
1339 }
1340
b34976b6 1341 return FALSE;
252b5132
RH
1342}
1343
1344/* Add a symbol to the output symbol table. */
1345
b34976b6 1346static bfd_boolean
268b6b39
AM
1347elf_link_output_sym (struct elf_final_link_info *finfo,
1348 const char *name,
1349 Elf_Internal_Sym *elfsym,
754021d0
AM
1350 asection *input_sec,
1351 struct elf_link_hash_entry *h)
252b5132 1352{
140f6c8e 1353 bfd_byte *dest;
9ad5cbcf 1354 Elf_External_Sym_Shndx *destshndx;
b34976b6 1355 bfd_boolean (*output_symbol_hook)
754021d0
AM
1356 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
1357 struct elf_link_hash_entry *);
140f6c8e 1358 const struct elf_backend_data *bed;
252b5132 1359
140f6c8e
AM
1360 bed = get_elf_backend_data (finfo->output_bfd);
1361 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
252b5132
RH
1362 if (output_symbol_hook != NULL)
1363 {
754021d0 1364 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
b34976b6 1365 return FALSE;
252b5132
RH
1366 }
1367
268b6b39 1368 if (name == NULL || *name == '\0')
252b5132
RH
1369 elfsym->st_name = 0;
1370 else if (input_sec->flags & SEC_EXCLUDE)
1371 elfsym->st_name = 0;
1372 else
1373 {
1374 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
b34976b6 1375 name, TRUE, FALSE);
252b5132 1376 if (elfsym->st_name == (unsigned long) -1)
b34976b6 1377 return FALSE;
252b5132
RH
1378 }
1379
1380 if (finfo->symbuf_count >= finfo->symbuf_size)
1381 {
140f6c8e 1382 if (! elf_link_flush_output_syms (finfo, bed))
b34976b6 1383 return FALSE;
252b5132
RH
1384 }
1385
140f6c8e 1386 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
9ad5cbcf
AM
1387 destshndx = finfo->symshndxbuf;
1388 if (destshndx != NULL)
c97e73dd
AM
1389 {
1390 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
1391 {
1392 bfd_size_type amt;
252b5132 1393
c97e73dd
AM
1394 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
1395 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
1396 if (destshndx == NULL)
b34976b6 1397 return FALSE;
c97e73dd
AM
1398 memset ((char *) destshndx + amt, 0, amt);
1399 finfo->shndxbuf_size *= 2;
1400 }
1401 destshndx += bfd_get_symcount (finfo->output_bfd);
1402 }
1403
140f6c8e 1404 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
c97e73dd
AM
1405 finfo->symbuf_count += 1;
1406 bfd_get_symcount (finfo->output_bfd) += 1;
252b5132 1407
b34976b6 1408 return TRUE;
252b5132
RH
1409}
1410
1411/* Flush the output symbols to the file. */
1412
b34976b6 1413static bfd_boolean
140f6c8e
AM
1414elf_link_flush_output_syms (struct elf_final_link_info *finfo,
1415 const struct elf_backend_data *bed)
252b5132
RH
1416{
1417 if (finfo->symbuf_count > 0)
1418 {
9ad5cbcf 1419 Elf_Internal_Shdr *hdr;
dc810e39
AM
1420 file_ptr pos;
1421 bfd_size_type amt;
252b5132 1422
9ad5cbcf
AM
1423 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
1424 pos = hdr->sh_offset + hdr->sh_size;
140f6c8e 1425 amt = finfo->symbuf_count * bed->s->sizeof_sym;
dc810e39 1426 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
268b6b39 1427 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
b34976b6 1428 return FALSE;
252b5132 1429
9ad5cbcf 1430 hdr->sh_size += amt;
252b5132
RH
1431 finfo->symbuf_count = 0;
1432 }
1433
b34976b6 1434 return TRUE;
252b5132
RH
1435}
1436
f5d44ba0
AM
1437/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
1438 allowing an unsatisfied unversioned symbol in the DSO to match a
71552942
L
1439 versioned symbol that would normally require an explicit version.
1440 We also handle the case that a DSO references a hidden symbol
1441 which may be satisfied by a versioned symbol in another DSO. */
f5d44ba0 1442
b34976b6 1443static bfd_boolean
268b6b39 1444elf_link_check_versioned_symbol (struct bfd_link_info *info,
140f6c8e 1445 const struct elf_backend_data *bed,
268b6b39 1446 struct elf_link_hash_entry *h)
f5d44ba0 1447{
71552942 1448 bfd *abfd;
f5d44ba0 1449 struct elf_link_loaded_list *loaded;
f5d44ba0 1450
0eddce27 1451 if (!is_elf_hash_table (info->hash))
b34976b6 1452 return FALSE;
f5d44ba0 1453
71552942
L
1454 switch (h->root.type)
1455 {
1456 default:
1457 abfd = NULL;
1458 break;
1459
1460 case bfd_link_hash_undefined:
1461 case bfd_link_hash_undefweak:
1462 abfd = h->root.u.undef.abfd;
4a43e768
AM
1463 if ((abfd->flags & DYNAMIC) == 0
1464 || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
71552942
L
1465 return FALSE;
1466 break;
1467
1468 case bfd_link_hash_defined:
1469 case bfd_link_hash_defweak:
1470 abfd = h->root.u.def.section->owner;
1471 break;
1472
1473 case bfd_link_hash_common:
1474 abfd = h->root.u.c.p->section->owner;
1475 break;
1476 }
1477 BFD_ASSERT (abfd != NULL);
1478
f5d44ba0
AM
1479 for (loaded = elf_hash_table (info)->loaded;
1480 loaded != NULL;
1481 loaded = loaded->next)
1482 {
1483 bfd *input;
1484 Elf_Internal_Shdr *hdr;
1485 bfd_size_type symcount;
1486 bfd_size_type extsymcount;
1487 bfd_size_type extsymoff;
1488 Elf_Internal_Shdr *versymhdr;
6cdc0ccc
AM
1489 Elf_Internal_Sym *isym;
1490 Elf_Internal_Sym *isymend;
1491 Elf_Internal_Sym *isymbuf;
f5d44ba0 1492 Elf_External_Versym *ever;
6cdc0ccc 1493 Elf_External_Versym *extversym;
f5d44ba0
AM
1494
1495 input = loaded->abfd;
1496
1497 /* We check each DSO for a possible hidden versioned definition. */
71552942 1498 if (input == abfd
f5d44ba0
AM
1499 || (input->flags & DYNAMIC) == 0
1500 || elf_dynversym (input) == 0)
1501 continue;
1502
1503 hdr = &elf_tdata (input)->dynsymtab_hdr;
1504
140f6c8e 1505 symcount = hdr->sh_size / bed->s->sizeof_sym;
f5d44ba0
AM
1506 if (elf_bad_symtab (input))
1507 {
1508 extsymcount = symcount;
1509 extsymoff = 0;
1510 }
1511 else
1512 {
1513 extsymcount = symcount - hdr->sh_info;
1514 extsymoff = hdr->sh_info;
1515 }
1516
1517 if (extsymcount == 0)
1518 continue;
1519
6cdc0ccc
AM
1520 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
1521 NULL, NULL, NULL);
1522 if (isymbuf == NULL)
b34976b6 1523 return FALSE;
f5d44ba0 1524
f5d44ba0
AM
1525 /* Read in any version definitions. */
1526 versymhdr = &elf_tdata (input)->dynversym_hdr;
268b6b39 1527 extversym = bfd_malloc (versymhdr->sh_size);
f5d44ba0
AM
1528 if (extversym == NULL)
1529 goto error_ret;
1530
1531 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
268b6b39 1532 || (bfd_bread (extversym, versymhdr->sh_size, input)
f5d44ba0
AM
1533 != versymhdr->sh_size))
1534 {
1535 free (extversym);
1536 error_ret:
6cdc0ccc 1537 free (isymbuf);
b34976b6 1538 return FALSE;
f5d44ba0
AM
1539 }
1540
1541 ever = extversym + extsymoff;
6cdc0ccc
AM
1542 isymend = isymbuf + extsymcount;
1543 for (isym = isymbuf; isym < isymend; isym++, ever++)
f5d44ba0
AM
1544 {
1545 const char *name;
f5d44ba0 1546 Elf_Internal_Versym iver;
a15d9d3a 1547 unsigned short version_index;
f5d44ba0 1548
6cdc0ccc
AM
1549 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
1550 || isym->st_shndx == SHN_UNDEF)
f5d44ba0
AM
1551 continue;
1552
1553 name = bfd_elf_string_from_elf_section (input,
1554 hdr->sh_link,
6cdc0ccc 1555 isym->st_name);
f5d44ba0
AM
1556 if (strcmp (name, h->root.root.string) != 0)
1557 continue;
1558
1559 _bfd_elf_swap_versym_in (input, ever, &iver);
1560
1561 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
1562 {
1563 /* If we have a non-hidden versioned sym, then it should
1564 have provided a definition for the undefined sym. */
1565 abort ();
1566 }
1567
a15d9d3a
L
1568 version_index = iver.vs_vers & VERSYM_VERSION;
1569 if (version_index == 1 || version_index == 2)
f5d44ba0 1570 {
a15d9d3a 1571 /* This is the base or first version. We can use it. */
f5d44ba0 1572 free (extversym);
6cdc0ccc 1573 free (isymbuf);
b34976b6 1574 return TRUE;
f5d44ba0
AM
1575 }
1576 }
1577
1578 free (extversym);
6cdc0ccc 1579 free (isymbuf);
f5d44ba0
AM
1580 }
1581
b34976b6 1582 return FALSE;
f5d44ba0
AM
1583}
1584
252b5132
RH
1585/* Add an external symbol to the symbol table. This is called from
1586 the hash table traversal routine. When generating a shared object,
1587 we go through the symbol table twice. The first time we output
1588 anything that might have been forced to local scope in a version
1589 script. The second time we output the symbols that are still
1590 global symbols. */
1591
b34976b6 1592static bfd_boolean
268b6b39 1593elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
252b5132 1594{
268b6b39 1595 struct elf_outext_info *eoinfo = data;
252b5132 1596 struct elf_final_link_info *finfo = eoinfo->finfo;
b34976b6 1597 bfd_boolean strip;
252b5132
RH
1598 Elf_Internal_Sym sym;
1599 asection *input_sec;
140f6c8e 1600 const struct elf_backend_data *bed;
252b5132 1601
e92d460e
AM
1602 if (h->root.type == bfd_link_hash_warning)
1603 {
1604 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1605 if (h->root.type == bfd_link_hash_new)
b34976b6 1606 return TRUE;
e92d460e
AM
1607 }
1608
252b5132
RH
1609 /* Decide whether to output this symbol in this pass. */
1610 if (eoinfo->localsyms)
1611 {
1612 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 1613 return TRUE;
252b5132
RH
1614 }
1615 else
1616 {
1617 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
b34976b6 1618 return TRUE;
252b5132
RH
1619 }
1620
140f6c8e
AM
1621 bed = get_elf_backend_data (finfo->output_bfd);
1622
560e09e9
NC
1623 /* If we have an undefined symbol reference here then it must have
1624 come from a shared library that is being linked in. (Undefined
1625 references in regular files have already been handled). If we
1626 are reporting errors for this situation then do so now. */
1627 if (h->root.type == bfd_link_hash_undefined
252b5132 1628 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
f5d44ba0 1629 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
140f6c8e 1630 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
560e09e9 1631 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
252b5132
RH
1632 {
1633 if (! ((*finfo->info->callbacks->undefined_symbol)
1634 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
560e09e9 1635 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
252b5132 1636 {
b34976b6
AM
1637 eoinfo->failed = TRUE;
1638 return FALSE;
252b5132
RH
1639 }
1640 }
1641
1b1fe8fe
L
1642 /* We should also warn if a forced local symbol is referenced from
1643 shared libraries. */
1049f94e 1644 if (! finfo->info->relocatable
560e09e9 1645 && (! finfo->info->shared)
1b1fe8fe 1646 && (h->elf_link_hash_flags
560e09e9 1647 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
71552942 1648 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
140f6c8e 1649 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
1b1fe8fe
L
1650 {
1651 (*_bfd_error_handler)
1652 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
1653 bfd_get_filename (finfo->output_bfd),
1654 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1655 ? "internal"
1656 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
1657 ? "hidden" : "local",
1658 h->root.root.string,
1659 bfd_archive_filename (h->root.u.def.section->owner));
1660 eoinfo->failed = TRUE;
1661 return FALSE;
1662 }
1663
252b5132
RH
1664 /* We don't want to output symbols that have never been mentioned by
1665 a regular file, or that we have been told to strip. However, if
1666 h->indx is set to -2, the symbol is used by a reloc and we must
1667 output it. */
1668 if (h->indx == -2)
b34976b6 1669 strip = FALSE;
252b5132
RH
1670 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1671 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1672 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1673 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
b34976b6 1674 strip = TRUE;
75828352
AM
1675 else if (finfo->info->strip == strip_all)
1676 strip = TRUE;
1677 else if (finfo->info->strip == strip_some
1678 && bfd_hash_lookup (finfo->info->keep_hash,
1679 h->root.root.string, FALSE, FALSE) == NULL)
1680 strip = TRUE;
1681 else if (finfo->info->strip_discarded
1682 && (h->root.type == bfd_link_hash_defined
1683 || h->root.type == bfd_link_hash_defweak)
1684 && elf_discarded_section (h->root.u.def.section))
b34976b6 1685 strip = TRUE;
252b5132 1686 else
b34976b6 1687 strip = FALSE;
252b5132
RH
1688
1689 /* If we're stripping it, and it's not a dynamic symbol, there's
2bd171e0
ILT
1690 nothing else to do unless it is a forced local symbol. */
1691 if (strip
1692 && h->dynindx == -1
1693 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
b34976b6 1694 return TRUE;
252b5132
RH
1695
1696 sym.st_value = 0;
1697 sym.st_size = h->size;
1698 sym.st_other = h->other;
1699 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1700 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
1701 else if (h->root.type == bfd_link_hash_undefweak
1702 || h->root.type == bfd_link_hash_defweak)
1703 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
1704 else
1705 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
1706
1707 switch (h->root.type)
1708 {
1709 default:
1710 case bfd_link_hash_new:
e92d460e 1711 case bfd_link_hash_warning:
252b5132 1712 abort ();
b34976b6 1713 return FALSE;
252b5132
RH
1714
1715 case bfd_link_hash_undefined:
252b5132
RH
1716 case bfd_link_hash_undefweak:
1717 input_sec = bfd_und_section_ptr;
1718 sym.st_shndx = SHN_UNDEF;
1719 break;
1720
1721 case bfd_link_hash_defined:
1722 case bfd_link_hash_defweak:
1723 {
1724 input_sec = h->root.u.def.section;
1725 if (input_sec->output_section != NULL)
1726 {
1727 sym.st_shndx =
1728 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
1729 input_sec->output_section);
9ad5cbcf 1730 if (sym.st_shndx == SHN_BAD)
252b5132
RH
1731 {
1732 (*_bfd_error_handler)
1733 (_("%s: could not find output section %s for input section %s"),
1734 bfd_get_filename (finfo->output_bfd),
1735 input_sec->output_section->name,
1736 input_sec->name);
b34976b6
AM
1737 eoinfo->failed = TRUE;
1738 return FALSE;
252b5132
RH
1739 }
1740
1049f94e
AM
1741 /* ELF symbols in relocatable files are section relative,
1742 but in nonrelocatable files they are virtual
252b5132
RH
1743 addresses. */
1744 sym.st_value = h->root.u.def.value + input_sec->output_offset;
1049f94e 1745 if (! finfo->info->relocatable)
13ae64f3
JJ
1746 {
1747 sym.st_value += input_sec->output_section->vma;
1748 if (h->type == STT_TLS)
1749 {
1750 /* STT_TLS symbols are relative to PT_TLS segment
1751 base. */
e1918d23
AM
1752 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
1753 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
1754 }
1755 }
252b5132
RH
1756 }
1757 else
1758 {
1759 BFD_ASSERT (input_sec->owner == NULL
1760 || (input_sec->owner->flags & DYNAMIC) != 0);
1761 sym.st_shndx = SHN_UNDEF;
1762 input_sec = bfd_und_section_ptr;
1763 }
1764 }
1765 break;
1766
1767 case bfd_link_hash_common:
1768 input_sec = h->root.u.c.p->section;
1769 sym.st_shndx = SHN_COMMON;
1770 sym.st_value = 1 << h->root.u.c.p->alignment_power;
1771 break;
1772
1773 case bfd_link_hash_indirect:
1774 /* These symbols are created by symbol versioning. They point
c44233aa
AM
1775 to the decorated version of the name. For example, if the
1776 symbol foo@@GNU_1.2 is the default, which should be used when
1777 foo is used with no version, then we add an indirect symbol
1778 foo which points to foo@@GNU_1.2. We ignore these symbols,
1779 since the indirected symbol is already in the hash table. */
b34976b6 1780 return TRUE;
252b5132
RH
1781 }
1782
1783 /* Give the processor backend a chance to tweak the symbol value,
1784 and also to finish up anything that needs to be done for this
c44233aa
AM
1785 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
1786 forced local syms when non-shared is due to a historical quirk. */
252b5132
RH
1787 if ((h->dynindx != -1
1788 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
ef5aade5
L
1789 && ((finfo->info->shared
1790 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1791 || h->root.type != bfd_link_hash_undefweak))
c44233aa 1792 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
252b5132
RH
1793 && elf_hash_table (finfo->info)->dynamic_sections_created)
1794 {
252b5132
RH
1795 if (! ((*bed->elf_backend_finish_dynamic_symbol)
1796 (finfo->output_bfd, finfo->info, h, &sym)))
1797 {
b34976b6
AM
1798 eoinfo->failed = TRUE;
1799 return FALSE;
252b5132
RH
1800 }
1801 }
1802
1803 /* If we are marking the symbol as undefined, and there are no
1804 non-weak references to this symbol from a regular object, then
91d3970e
ILT
1805 mark the symbol as weak undefined; if there are non-weak
1806 references, mark the symbol as strong. We can't do this earlier,
252b5132
RH
1807 because it might not be marked as undefined until the
1808 finish_dynamic_symbol routine gets through with it. */
1809 if (sym.st_shndx == SHN_UNDEF
252b5132 1810 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
a7b97311
AM
1811 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
1812 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
91d3970e
ILT
1813 {
1814 int bindtype;
1815
1816 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
1817 bindtype = STB_GLOBAL;
1818 else
1819 bindtype = STB_WEAK;
1820 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
1821 }
252b5132 1822
1b1fe8fe
L
1823 /* If a non-weak symbol with non-default visibility is not defined
1824 locally, it is a fatal error. */
1049f94e 1825 if (! finfo->info->relocatable
9c7a29a3 1826 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
1b1fe8fe 1827 && ELF_ST_BIND (sym.st_info) != STB_WEAK
22d5e339 1828 && h->root.type == bfd_link_hash_undefined
2cd533b7 1829 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1b1fe8fe
L
1830 {
1831 (*_bfd_error_handler)
1832 (_("%s: %s symbol `%s' isn't defined"),
1833 bfd_get_filename (finfo->output_bfd),
1834 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
1835 ? "protected"
1836 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
1837 ? "internal" : "hidden",
1838 h->root.root.string);
1839 eoinfo->failed = TRUE;
1840 return FALSE;
1841 }
32c092c3 1842
252b5132 1843 /* If this symbol should be put in the .dynsym section, then put it
f5d44ba0
AM
1844 there now. We already know the symbol index. We also fill in
1845 the entry in the .hash section. */
252b5132
RH
1846 if (h->dynindx != -1
1847 && elf_hash_table (finfo->info)->dynamic_sections_created)
1848 {
1849 size_t bucketcount;
1850 size_t bucket;
c7ac6ff8 1851 size_t hash_entry_size;
252b5132
RH
1852 bfd_byte *bucketpos;
1853 bfd_vma chain;
140f6c8e 1854 bfd_byte *esym;
252b5132
RH
1855
1856 sym.st_name = h->dynstr_index;
140f6c8e
AM
1857 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
1858 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
252b5132
RH
1859
1860 bucketcount = elf_hash_table (finfo->info)->bucketcount;
1861 bucket = h->elf_hash_value % bucketcount;
3e932841 1862 hash_entry_size
c7ac6ff8 1863 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
252b5132 1864 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
c7ac6ff8
MM
1865 + (bucket + 2) * hash_entry_size);
1866 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
268b6b39 1867 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
c7ac6ff8
MM
1868 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
1869 ((bfd_byte *) finfo->hash_sec->contents
1870 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
252b5132
RH
1871
1872 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
1873 {
1874 Elf_Internal_Versym iversym;
dc810e39 1875 Elf_External_Versym *eversym;
252b5132
RH
1876
1877 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1878 {
1879 if (h->verinfo.verdef == NULL)
1880 iversym.vs_vers = 0;
1881 else
1882 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
1883 }
1884 else
1885 {
1886 if (h->verinfo.vertree == NULL)
1887 iversym.vs_vers = 1;
1888 else
1889 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
1890 }
1891
1892 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
1893 iversym.vs_vers |= VERSYM_HIDDEN;
1894
dc810e39
AM
1895 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
1896 eversym += h->dynindx;
1897 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
252b5132
RH
1898 }
1899 }
1900
1901 /* If we're stripping it, then it was just a dynamic symbol, and
1902 there's nothing else to do. */
7330fb86 1903 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
b34976b6 1904 return TRUE;
252b5132
RH
1905
1906 h->indx = bfd_get_symcount (finfo->output_bfd);
1907
754021d0 1908 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
252b5132 1909 {
b34976b6
AM
1910 eoinfo->failed = TRUE;
1911 return FALSE;
252b5132
RH
1912 }
1913
b34976b6 1914 return TRUE;
252b5132
RH
1915}
1916
1917/* Link an input file into the linker output file. This function
1918 handles all the sections and relocations of the input file at once.
1919 This is so that we only have to read the local symbols once, and
1920 don't have to keep them in memory. */
1921
b34976b6 1922static bfd_boolean
268b6b39 1923elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
252b5132 1924{
b34976b6 1925 bfd_boolean (*relocate_section)
268b6b39
AM
1926 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
1927 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
252b5132
RH
1928 bfd *output_bfd;
1929 Elf_Internal_Shdr *symtab_hdr;
1930 size_t locsymcount;
1931 size_t extsymoff;
6cdc0ccc 1932 Elf_Internal_Sym *isymbuf;
252b5132 1933 Elf_Internal_Sym *isym;
6cdc0ccc 1934 Elf_Internal_Sym *isymend;
252b5132
RH
1935 long *pindex;
1936 asection **ppsection;
1937 asection *o;
9c5bfbb7 1938 const struct elf_backend_data *bed;
b34976b6 1939 bfd_boolean emit_relocs;
f8deed93 1940 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
1941
1942 output_bfd = finfo->output_bfd;
c7ac6ff8
MM
1943 bed = get_elf_backend_data (output_bfd);
1944 relocate_section = bed->elf_backend_relocate_section;
252b5132
RH
1945
1946 /* If this is a dynamic object, we don't want to do anything here:
1947 we don't want the local symbols, and we don't want the section
1948 contents. */
1949 if ((input_bfd->flags & DYNAMIC) != 0)
b34976b6 1950 return TRUE;
252b5132 1951
1049f94e 1952 emit_relocs = (finfo->info->relocatable
c44233aa
AM
1953 || finfo->info->emitrelocations
1954 || bed->elf_backend_emit_relocs);
9317eacc 1955
252b5132
RH
1956 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1957 if (elf_bad_symtab (input_bfd))
1958 {
140f6c8e 1959 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
252b5132
RH
1960 extsymoff = 0;
1961 }
1962 else
1963 {
1964 locsymcount = symtab_hdr->sh_info;
1965 extsymoff = symtab_hdr->sh_info;
1966 }
1967
1968 /* Read the local symbols. */
6cdc0ccc
AM
1969 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1970 if (isymbuf == NULL && locsymcount != 0)
1971 {
1972 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
1973 finfo->internal_syms,
1974 finfo->external_syms,
1975 finfo->locsym_shndx);
1976 if (isymbuf == NULL)
b34976b6 1977 return FALSE;
252b5132
RH
1978 }
1979
6cdc0ccc
AM
1980 /* Find local symbol sections and adjust values of symbols in
1981 SEC_MERGE sections. Write out those local symbols we know are
1982 going into the output file. */
1983 isymend = isymbuf + locsymcount;
1984 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
1985 isym < isymend;
1986 isym++, pindex++, ppsection++)
252b5132
RH
1987 {
1988 asection *isec;
1989 const char *name;
1990 Elf_Internal_Sym osym;
1991
252b5132
RH
1992 *pindex = -1;
1993
1994 if (elf_bad_symtab (input_bfd))
1995 {
1996 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
1997 {
1998 *ppsection = NULL;
1999 continue;
2000 }
2001 }
2002
2003 if (isym->st_shndx == SHN_UNDEF)
862517b6 2004 isec = bfd_und_section_ptr;
9ad5cbcf
AM
2005 else if (isym->st_shndx < SHN_LORESERVE
2006 || isym->st_shndx > SHN_HIRESERVE)
f5fa8ca2 2007 {
140f6c8e 2008 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
65765700 2009 if (isec
68bfbfcc 2010 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
f5fa8ca2
JJ
2011 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2012 isym->st_value =
2013 _bfd_merged_section_offset (output_bfd, &isec,
65765700 2014 elf_section_data (isec)->sec_info,
268b6b39 2015 isym->st_value, 0);
f5fa8ca2 2016 }
252b5132 2017 else if (isym->st_shndx == SHN_ABS)
862517b6 2018 isec = bfd_abs_section_ptr;
252b5132 2019 else if (isym->st_shndx == SHN_COMMON)
862517b6 2020 isec = bfd_com_section_ptr;
252b5132
RH
2021 else
2022 {
2023 /* Who knows? */
2024 isec = NULL;
2025 }
2026
2027 *ppsection = isec;
2028
2029 /* Don't output the first, undefined, symbol. */
6cdc0ccc 2030 if (ppsection == finfo->sections)
252b5132
RH
2031 continue;
2032
24376d1b
AM
2033 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2034 {
24376d1b
AM
2035 /* We never output section symbols. Instead, we use the
2036 section symbol of the corresponding section in the output
2037 file. */
2038 continue;
2039 }
2040
252b5132
RH
2041 /* If we are stripping all symbols, we don't want to output this
2042 one. */
2043 if (finfo->info->strip == strip_all)
2044 continue;
2045
252b5132 2046 /* If we are discarding all local symbols, we don't want to
1049f94e 2047 output this one. If we are generating a relocatable output
252b5132
RH
2048 file, then some of the local symbols may be required by
2049 relocs; we output them below as we discover that they are
2050 needed. */
2051 if (finfo->info->discard == discard_all)
2052 continue;
2053
2054 /* If this symbol is defined in a section which we are
c44233aa
AM
2055 discarding, we don't need to keep it, but note that
2056 linker_mark is only reliable for sections that have contents.
2057 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
2058 as well as linker_mark. */
9ad5cbcf 2059 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
252b5132
RH
2060 && isec != NULL
2061 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
1049f94e 2062 || (! finfo->info->relocatable
252b5132
RH
2063 && (isec->flags & SEC_EXCLUDE) != 0)))
2064 continue;
2065
2066 /* Get the name of the symbol. */
2067 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
2068 isym->st_name);
2069 if (name == NULL)
b34976b6 2070 return FALSE;
252b5132
RH
2071
2072 /* See if we are discarding symbols with this name. */
2073 if ((finfo->info->strip == strip_some
b34976b6 2074 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
252b5132 2075 == NULL))
f5fa8ca2 2076 || (((finfo->info->discard == discard_sec_merge
1049f94e 2077 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
f5fa8ca2 2078 || finfo->info->discard == discard_l)
252b5132
RH
2079 && bfd_is_local_label_name (input_bfd, name)))
2080 continue;
2081
2082 /* If we get here, we are going to output this symbol. */
2083
2084 osym = *isym;
2085
2086 /* Adjust the section index for the output file. */
2087 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
2088 isec->output_section);
9ad5cbcf 2089 if (osym.st_shndx == SHN_BAD)
b34976b6 2090 return FALSE;
252b5132
RH
2091
2092 *pindex = bfd_get_symcount (output_bfd);
2093
1049f94e 2094 /* ELF symbols in relocatable files are section relative, but
252b5132
RH
2095 in executable files they are virtual addresses. Note that
2096 this code assumes that all ELF sections have an associated
2097 BFD section with a reasonable value for output_offset; below
2098 we assume that they also have a reasonable value for
2099 output_section. Any special sections must be set up to meet
2100 these requirements. */
2101 osym.st_value += isec->output_offset;
1049f94e 2102 if (! finfo->info->relocatable)
13ae64f3
JJ
2103 {
2104 osym.st_value += isec->output_section->vma;
2105 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
2106 {
2107 /* STT_TLS symbols are relative to PT_TLS segment base. */
e1918d23
AM
2108 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
2109 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
13ae64f3
JJ
2110 }
2111 }
252b5132 2112
754021d0 2113 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
b34976b6 2114 return FALSE;
252b5132
RH
2115 }
2116
2117 /* Relocate the contents of each section. */
f8deed93 2118 sym_hashes = elf_sym_hashes (input_bfd);
252b5132
RH
2119 for (o = input_bfd->sections; o != NULL; o = o->next)
2120 {
2121 bfd_byte *contents;
2122
2123 if (! o->linker_mark)
2124 {
2125 /* This section was omitted from the link. */
2126 continue;
2127 }
2128
2129 if ((o->flags & SEC_HAS_CONTENTS) == 0
2130 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2131 continue;
2132
2133 if ((o->flags & SEC_LINKER_CREATED) != 0)
2134 {
45d6a902 2135 /* Section was created by _bfd_elf_link_create_dynamic_sections
252b5132
RH
2136 or somesuch. */
2137 continue;
2138 }
2139
2140 /* Get the contents of the section. They have been cached by a
c44233aa
AM
2141 relaxation routine. Note that o is a section in an input
2142 file, so the contents field will not have been set by any of
2143 the routines which work on output files. */
252b5132
RH
2144 if (elf_section_data (o)->this_hdr.contents != NULL)
2145 contents = elf_section_data (o)->this_hdr.contents;
2146 else
2147 {
2148 contents = finfo->contents;
268b6b39
AM
2149 if (! bfd_get_section_contents (input_bfd, o, contents, 0,
2150 o->_raw_size))
b34976b6 2151 return FALSE;
252b5132
RH
2152 }
2153
2154 if ((o->flags & SEC_RELOC) != 0)
2155 {
2156 Elf_Internal_Rela *internal_relocs;
140f6c8e
AM
2157 bfd_vma r_type_mask;
2158 int r_sym_shift;
252b5132
RH
2159
2160 /* Get the swapped relocs. */
45d6a902
AM
2161 internal_relocs
2162 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
2163 finfo->internal_relocs, FALSE);
252b5132
RH
2164 if (internal_relocs == NULL
2165 && o->reloc_count > 0)
b34976b6 2166 return FALSE;
252b5132 2167
140f6c8e
AM
2168 if (bed->s->arch_size == 32)
2169 {
2170 r_type_mask = 0xff;
2171 r_sym_shift = 8;
2172 }
2173 else
2174 {
2175 r_type_mask = 0xffffffff;
2176 r_sym_shift = 32;
2177 }
2178
ec338859
AM
2179 /* Run through the relocs looking for any against symbols
2180 from discarded sections and section symbols from
2181 removed link-once sections. Complain about relocs
2182 against discarded sections. Zero relocs against removed
f97b9cb8
L
2183 link-once sections. Preserve debug information as much
2184 as we can. */
2185 if (!elf_section_ignore_discarded_relocs (o))
ec338859
AM
2186 {
2187 Elf_Internal_Rela *rel, *relend;
50b4d486 2188
ec338859
AM
2189 rel = internal_relocs;
2190 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
2191 for ( ; rel < relend; rel++)
2192 {
140f6c8e 2193 unsigned long r_symndx = rel->r_info >> r_sym_shift;
f97b9cb8 2194 asection *sec;
ec338859
AM
2195
2196 if (r_symndx >= locsymcount
2197 || (elf_bad_symtab (input_bfd)
2198 && finfo->sections[r_symndx] == NULL))
2199 {
2200 struct elf_link_hash_entry *h;
2201
2202 h = sym_hashes[r_symndx - extsymoff];
2203 while (h->root.type == bfd_link_hash_indirect
2204 || h->root.type == bfd_link_hash_warning)
2205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2206
2207 /* Complain if the definition comes from a
2208 discarded section. */
f97b9cb8 2209 sec = h->root.u.def.section;
ec338859
AM
2210 if ((h->root.type == bfd_link_hash_defined
2211 || h->root.type == bfd_link_hash_defweak)
f97b9cb8 2212 && elf_discarded_section (sec))
ec338859 2213 {
ec338859
AM
2214 if ((o->flags & SEC_DEBUGGING) != 0)
2215 {
45e9217a 2216 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
2217 /* Try to preserve debug information. */
2218 if ((o->flags & SEC_DEBUGGING) != 0
2219 && sec->kept_section != NULL
6e35c4da 2220 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
2221 h->root.u.def.section
2222 = sec->kept_section;
2223 else
2224 memset (rel, 0, sizeof (*rel));
ec338859
AM
2225 }
2226 else
6f6f27f8
L
2227 finfo->info->callbacks->error_handler
2228 (LD_DEFINITION_IN_DISCARDED_SECTION,
2229 _("%T: discarded in section `%s' from %s\n"),
2230 h->root.root.string,
2231 h->root.root.string,
2232 h->root.u.def.section->name,
2233 bfd_archive_filename (h->root.u.def.section->owner));
ec338859
AM
2234 }
2235 }
2236 else
2237 {
f97b9cb8 2238 sec = finfo->sections[r_symndx];
50b4d486 2239
ed4de5e2 2240 if (sec != NULL && elf_discarded_section (sec))
f9f32305 2241 {
ad43ed4c
L
2242 if ((o->flags & SEC_DEBUGGING) != 0
2243 || (sec->flags & SEC_LINK_ONCE) != 0)
f9f32305 2244 {
45e9217a 2245 BFD_ASSERT (r_symndx != 0);
f97b9cb8
L
2246 /* Try to preserve debug information. */
2247 if ((o->flags & SEC_DEBUGGING) != 0
2248 && sec->kept_section != NULL
6e35c4da 2249 && sec->_raw_size == sec->kept_section->_raw_size)
f97b9cb8
L
2250 finfo->sections[r_symndx]
2251 = sec->kept_section;
2252 else
2253 {
140f6c8e 2254 rel->r_info &= r_type_mask;
f97b9cb8
L
2255 rel->r_addend = 0;
2256 }
f9f32305
AM
2257 }
2258 else
f9f32305 2259 {
6f6f27f8
L
2260 static int count;
2261 int ok;
2262 char *buf;
2263
2264 ok = asprintf (&buf, "local symbol %d",
2265 count++);
2266 if (ok <= 0)
2267 buf = (char *) "local symbol";
2268 finfo->info->callbacks->error_handler
2269 (LD_DEFINITION_IN_DISCARDED_SECTION,
2270 _("%T: discarded in section `%s' from %s\n"),
2271 buf, buf, sec->name,
2272 bfd_archive_filename (input_bfd));
2273 if (ok != -1)
f9f32305 2274 free (buf);
ec338859
AM
2275 }
2276 }
2277 }
2278 }
2279 }
50b4d486 2280
252b5132
RH
2281 /* Relocate the section by invoking a back end routine.
2282
2283 The back end routine is responsible for adjusting the
2284 section contents as necessary, and (if using Rela relocs
1049f94e 2285 and generating a relocatable output file) adjusting the
252b5132
RH
2286 reloc addend as necessary.
2287
2288 The back end routine does not have to worry about setting
2289 the reloc address or the reloc symbol index.
2290
2291 The back end routine is given a pointer to the swapped in
2292 internal symbols, and can access the hash table entries
2293 for the external symbols via elf_sym_hashes (input_bfd).
2294
1049f94e 2295 When generating relocatable output, the back end routine
252b5132
RH
2296 must handle STB_LOCAL/STT_SECTION symbols specially. The
2297 output symbol is going to be a section symbol
2298 corresponding to the output section, which will require
2299 the addend to be adjusted. */
2300
2301 if (! (*relocate_section) (output_bfd, finfo->info,
2302 input_bfd, o, contents,
2303 internal_relocs,
6cdc0ccc 2304 isymbuf,
252b5132 2305 finfo->sections))
b34976b6 2306 return FALSE;
252b5132 2307
9317eacc 2308 if (emit_relocs)
252b5132
RH
2309 {
2310 Elf_Internal_Rela *irela;
2311 Elf_Internal_Rela *irelaend;
73722af0 2312 bfd_vma last_offset;
252b5132 2313 struct elf_link_hash_entry **rel_hash;
c89583f8 2314 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
4e8a9624 2315 unsigned int next_erel;
b34976b6 2316 bfd_boolean (*reloc_emitter)
268b6b39 2317 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
b34976b6 2318 bfd_boolean rela_normal;
b491616a
AM
2319
2320 input_rel_hdr = &elf_section_data (o)->rel_hdr;
2321 rela_normal = (bed->rela_normal
2322 && (input_rel_hdr->sh_entsize
140f6c8e 2323 == bed->s->sizeof_rela));
252b5132
RH
2324
2325 /* Adjust the reloc addresses and symbol indices. */
2326
2327 irela = internal_relocs;
dc810e39 2328 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 2329 rel_hash = (elf_section_data (o->output_section)->rel_hashes
31367b81
MM
2330 + elf_section_data (o->output_section)->rel_count
2331 + elf_section_data (o->output_section)->rel_count2);
73722af0 2332 last_offset = o->output_offset;
1049f94e 2333 if (!finfo->info->relocatable)
73722af0 2334 last_offset += o->output_section->vma;
209f668e 2335 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
252b5132
RH
2336 {
2337 unsigned long r_symndx;
252b5132 2338 asection *sec;
fad2542d 2339 Elf_Internal_Sym sym;
252b5132 2340
209f668e
NC
2341 if (next_erel == bed->s->int_rels_per_ext_rel)
2342 {
2343 rel_hash++;
2344 next_erel = 0;
2345 }
2346
d6fe2dc1
AM
2347 irela->r_offset = _bfd_elf_section_offset (output_bfd,
2348 finfo->info, o,
2349 irela->r_offset);
2350 if (irela->r_offset >= (bfd_vma) -2)
2351 {
73722af0
AM
2352 /* This is a reloc for a deleted entry or somesuch.
2353 Turn it into an R_*_NONE reloc, at the same
2354 offset as the last reloc. elf_eh_frame.c and
2355 elf_bfd_discard_info rely on reloc offsets
b34976b6 2356 being ordered. */
73722af0
AM
2357 irela->r_offset = last_offset;
2358 irela->r_info = 0;
2359 irela->r_addend = 0;
d6fe2dc1
AM
2360 continue;
2361 }
2362
252b5132
RH
2363 irela->r_offset += o->output_offset;
2364
7ad34365 2365 /* Relocs in an executable have to be virtual addresses. */
1049f94e 2366 if (!finfo->info->relocatable)
7ad34365
NC
2367 irela->r_offset += o->output_section->vma;
2368
73722af0 2369 last_offset = irela->r_offset;
252b5132 2370
140f6c8e 2371 r_symndx = irela->r_info >> r_sym_shift;
73722af0 2372 if (r_symndx == STN_UNDEF)
252b5132
RH
2373 continue;
2374
2375 if (r_symndx >= locsymcount
2376 || (elf_bad_symtab (input_bfd)
2377 && finfo->sections[r_symndx] == NULL))
2378 {
2379 struct elf_link_hash_entry *rh;
209f668e 2380 unsigned long indx;
252b5132
RH
2381
2382 /* This is a reloc against a global symbol. We
2383 have not yet output all the local symbols, so
2384 we do not know the symbol index of any global
2385 symbol. We set the rel_hash entry for this
2386 reloc to point to the global hash table entry
2387 for this symbol. The symbol index is then
2388 set at the end of elf_bfd_final_link. */
2389 indx = r_symndx - extsymoff;
2390 rh = elf_sym_hashes (input_bfd)[indx];
2391 while (rh->root.type == bfd_link_hash_indirect
2392 || rh->root.type == bfd_link_hash_warning)
2393 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
2394
2395 /* Setting the index to -2 tells
2396 elf_link_output_extsym that this symbol is
2397 used by a reloc. */
2398 BFD_ASSERT (rh->indx < 0);
2399 rh->indx = -2;
2400
2401 *rel_hash = rh;
2402
2403 continue;
2404 }
2405
3e932841 2406 /* This is a reloc against a local symbol. */
252b5132
RH
2407
2408 *rel_hash = NULL;
fad2542d 2409 sym = isymbuf[r_symndx];
252b5132 2410 sec = finfo->sections[r_symndx];
fad2542d 2411 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
252b5132
RH
2412 {
2413 /* I suppose the backend ought to fill in the
2414 section of any STT_SECTION symbol against a
2415 processor specific section. If we have
2416 discarded a section, the output_section will
2417 be the absolute section. */
b491616a
AM
2418 if (bfd_is_abs_section (sec)
2419 || (sec != NULL
2420 && bfd_is_abs_section (sec->output_section)))
252b5132
RH
2421 r_symndx = 0;
2422 else if (sec == NULL || sec->owner == NULL)
2423 {
2424 bfd_set_error (bfd_error_bad_value);
b34976b6 2425 return FALSE;
252b5132
RH
2426 }
2427 else
2428 {
2429 r_symndx = sec->output_section->target_index;
2430 BFD_ASSERT (r_symndx != 0);
2431 }
b491616a
AM
2432
2433 /* Adjust the addend according to where the
f5d44ba0 2434 section winds up in the output section. */
b491616a
AM
2435 if (rela_normal)
2436 irela->r_addend += sec->output_offset;
252b5132
RH
2437 }
2438 else
2439 {
2440 if (finfo->indices[r_symndx] == -1)
2441 {
dc810e39 2442 unsigned long shlink;
252b5132
RH
2443 const char *name;
2444 asection *osec;
2445
2446 if (finfo->info->strip == strip_all)
2447 {
2448 /* You can't do ld -r -s. */
2449 bfd_set_error (bfd_error_invalid_operation);
b34976b6 2450 return FALSE;
252b5132
RH
2451 }
2452
2453 /* This symbol was skipped earlier, but
2454 since it is needed by a reloc, we
2455 must output it now. */
dc810e39 2456 shlink = symtab_hdr->sh_link;
a7b97311 2457 name = (bfd_elf_string_from_elf_section
fad2542d 2458 (input_bfd, shlink, sym.st_name));
252b5132 2459 if (name == NULL)
b34976b6 2460 return FALSE;
252b5132
RH
2461
2462 osec = sec->output_section;
fad2542d 2463 sym.st_shndx =
252b5132
RH
2464 _bfd_elf_section_from_bfd_section (output_bfd,
2465 osec);
fad2542d 2466 if (sym.st_shndx == SHN_BAD)
b34976b6 2467 return FALSE;
252b5132 2468
fad2542d 2469 sym.st_value += sec->output_offset;
1049f94e 2470 if (! finfo->info->relocatable)
13ae64f3 2471 {
fad2542d
AM
2472 sym.st_value += osec->vma;
2473 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
13ae64f3
JJ
2474 {
2475 /* STT_TLS symbols are relative to PT_TLS
2476 segment base. */
e1918d23
AM
2477 BFD_ASSERT (elf_hash_table (finfo->info)
2478 ->tls_sec != NULL);
2479 sym.st_value -= (elf_hash_table (finfo->info)
2480 ->tls_sec->vma);
13ae64f3
JJ
2481 }
2482 }
252b5132 2483
a7b97311
AM
2484 finfo->indices[r_symndx]
2485 = bfd_get_symcount (output_bfd);
252b5132 2486
754021d0
AM
2487 if (! elf_link_output_sym (finfo, name, &sym, sec,
2488 NULL))
b34976b6 2489 return FALSE;
252b5132
RH
2490 }
2491
2492 r_symndx = finfo->indices[r_symndx];
2493 }
2494
140f6c8e
AM
2495 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
2496 | (irela->r_info & r_type_mask));
252b5132
RH
2497 }
2498
2499 /* Swap out the relocs. */
c44233aa 2500 if (bed->elf_backend_emit_relocs
1049f94e 2501 && !(finfo->info->relocatable
a7b97311 2502 || finfo->info->emitrelocations))
c44233aa
AM
2503 reloc_emitter = bed->elf_backend_emit_relocs;
2504 else
45d6a902 2505 reloc_emitter = _bfd_elf_link_output_relocs;
9317eacc 2506
c89583f8
AM
2507 if (input_rel_hdr->sh_size != 0
2508 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
2509 internal_relocs))
b34976b6 2510 return FALSE;
9317eacc 2511
c89583f8
AM
2512 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
2513 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
c44233aa
AM
2514 {
2515 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
dc810e39 2516 * bed->s->int_rels_per_ext_rel);
c89583f8 2517 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
41241523 2518 internal_relocs))
b34976b6 2519 return FALSE;
c44233aa 2520 }
252b5132
RH
2521 }
2522 }
2523
2524 /* Write out the modified section contents. */
73d074b4 2525 if (bed->elf_backend_write_section
f9f32305 2526 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
73d074b4
DJ
2527 {
2528 /* Section written out. */
2529 }
68bfbfcc 2530 else switch (o->sec_info_type)
f5fa8ca2 2531 {
65765700 2532 case ELF_INFO_TYPE_STABS:
f5fa8ca2 2533 if (! (_bfd_write_section_stabs
65765700
JJ
2534 (output_bfd,
2535 &elf_hash_table (finfo->info)->stab_info,
2536 o, &elf_section_data (o)->sec_info, contents)))
b34976b6 2537 return FALSE;
65765700
JJ
2538 break;
2539 case ELF_INFO_TYPE_MERGE:
126495ed
AM
2540 if (! _bfd_write_merged_section (output_bfd, o,
2541 elf_section_data (o)->sec_info))
b34976b6 2542 return FALSE;
65765700
JJ
2543 break;
2544 case ELF_INFO_TYPE_EH_FRAME:
2545 {
126495ed
AM
2546 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
2547 o, contents))
b34976b6 2548 return FALSE;
65765700
JJ
2549 }
2550 break;
2551 default:
2552 {
2553 bfd_size_type sec_size;
2554
2555 sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
2556 if (! (o->flags & SEC_EXCLUDE)
2557 && ! bfd_set_section_contents (output_bfd, o->output_section,
2558 contents,
2559 (file_ptr) o->output_offset,
2560 sec_size))
b34976b6 2561 return FALSE;
65765700
JJ
2562 }
2563 break;
252b5132
RH
2564 }
2565 }
2566
b34976b6 2567 return TRUE;
252b5132
RH
2568}
2569
2570/* Generate a reloc when linking an ELF file. This is a reloc
2571 requested by the linker, and does come from any input file. This
2572 is used to build constructor and destructor tables when linking
2573 with -Ur. */
2574
b34976b6 2575static bfd_boolean
268b6b39
AM
2576elf_reloc_link_order (bfd *output_bfd,
2577 struct bfd_link_info *info,
2578 asection *output_section,
2579 struct bfd_link_order *link_order)
252b5132
RH
2580{
2581 reloc_howto_type *howto;
2582 long indx;
2583 bfd_vma offset;
2584 bfd_vma addend;
2585 struct elf_link_hash_entry **rel_hash_ptr;
2586 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 2587 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947216bf
AM
2588 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
2589 bfd_byte *erel;
2590 unsigned int i;
252b5132
RH
2591
2592 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2593 if (howto == NULL)
2594 {
2595 bfd_set_error (bfd_error_bad_value);
b34976b6 2596 return FALSE;
252b5132
RH
2597 }
2598
2599 addend = link_order->u.reloc.p->addend;
2600
2601 /* Figure out the symbol index. */
2602 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
31367b81
MM
2603 + elf_section_data (output_section)->rel_count
2604 + elf_section_data (output_section)->rel_count2);
252b5132
RH
2605 if (link_order->type == bfd_section_reloc_link_order)
2606 {
2607 indx = link_order->u.reloc.p->u.section->target_index;
2608 BFD_ASSERT (indx != 0);
2609 *rel_hash_ptr = NULL;
2610 }
2611 else
2612 {
2613 struct elf_link_hash_entry *h;
2614
2615 /* Treat a reloc against a defined symbol as though it were
c44233aa 2616 actually against the section. */
252b5132
RH
2617 h = ((struct elf_link_hash_entry *)
2618 bfd_wrapped_link_hash_lookup (output_bfd, info,
2619 link_order->u.reloc.p->u.name,
b34976b6 2620 FALSE, FALSE, TRUE));
252b5132
RH
2621 if (h != NULL
2622 && (h->root.type == bfd_link_hash_defined
2623 || h->root.type == bfd_link_hash_defweak))
2624 {
2625 asection *section;
2626
2627 section = h->root.u.def.section;
2628 indx = section->output_section->target_index;
2629 *rel_hash_ptr = NULL;
2630 /* It seems that we ought to add the symbol value to the
c44233aa
AM
2631 addend here, but in practice it has already been added
2632 because it was passed to constructor_callback. */
252b5132
RH
2633 addend += section->output_section->vma + section->output_offset;
2634 }
2635 else if (h != NULL)
2636 {
2637 /* Setting the index to -2 tells elf_link_output_extsym that
2638 this symbol is used by a reloc. */
2639 h->indx = -2;
2640 *rel_hash_ptr = h;
2641 indx = 0;
2642 }
2643 else
2644 {
2645 if (! ((*info->callbacks->unattached_reloc)
268b6b39 2646 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
b34976b6 2647 return FALSE;
252b5132
RH
2648 indx = 0;
2649 }
2650 }
2651
2652 /* If this is an inplace reloc, we must write the addend into the
2653 object file. */
2654 if (howto->partial_inplace && addend != 0)
2655 {
2656 bfd_size_type size;
2657 bfd_reloc_status_type rstat;
2658 bfd_byte *buf;
b34976b6 2659 bfd_boolean ok;
dc810e39 2660 const char *sym_name;
252b5132
RH
2661
2662 size = bfd_get_reloc_size (howto);
268b6b39
AM
2663 buf = bfd_zmalloc (size);
2664 if (buf == NULL)
b34976b6 2665 return FALSE;
268b6b39 2666 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
252b5132
RH
2667 switch (rstat)
2668 {
2669 case bfd_reloc_ok:
2670 break;
dc810e39 2671
252b5132
RH
2672 default:
2673 case bfd_reloc_outofrange:
2674 abort ();
dc810e39 2675
252b5132 2676 case bfd_reloc_overflow:
dc810e39
AM
2677 if (link_order->type == bfd_section_reloc_link_order)
2678 sym_name = bfd_section_name (output_bfd,
2679 link_order->u.reloc.p->u.section);
2680 else
2681 sym_name = link_order->u.reloc.p->u.name;
252b5132 2682 if (! ((*info->callbacks->reloc_overflow)
268b6b39 2683 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
252b5132
RH
2684 {
2685 free (buf);
b34976b6 2686 return FALSE;
252b5132
RH
2687 }
2688 break;
2689 }
268b6b39
AM
2690 ok = bfd_set_section_contents (output_bfd, output_section, buf,
2691 link_order->offset, size);
252b5132
RH
2692 free (buf);
2693 if (! ok)
b34976b6 2694 return FALSE;
252b5132
RH
2695 }
2696
2697 /* The address of a reloc is relative to the section in a
1049f94e 2698 relocatable file, and is a virtual address in an executable
252b5132
RH
2699 file. */
2700 offset = link_order->offset;
1049f94e 2701 if (! info->relocatable)
252b5132
RH
2702 offset += output_section->vma;
2703
947216bf
AM
2704 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2705 {
2706 irel[i].r_offset = offset;
2707 irel[i].r_info = 0;
2708 irel[i].r_addend = 0;
2709 }
140f6c8e
AM
2710 if (bed->s->arch_size == 32)
2711 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
2712 else
2713 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
252b5132 2714
947216bf
AM
2715 rel_hdr = &elf_section_data (output_section)->rel_hdr;
2716 erel = rel_hdr->contents;
252b5132
RH
2717 if (rel_hdr->sh_type == SHT_REL)
2718 {
947216bf 2719 erel += (elf_section_data (output_section)->rel_count
140f6c8e 2720 * bed->s->sizeof_rel);
947216bf 2721 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
252b5132
RH
2722 }
2723 else
2724 {
947216bf
AM
2725 irel[0].r_addend = addend;
2726 erel += (elf_section_data (output_section)->rel_count
140f6c8e 2727 * bed->s->sizeof_rela);
947216bf 2728 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
252b5132
RH
2729 }
2730
0525d26e 2731 ++elf_section_data (output_section)->rel_count;
252b5132 2732
b34976b6 2733 return TRUE;
252b5132 2734}
252b5132 2735\f
252b5132
RH
2736/* Garbage collect unused sections. */
2737
b34976b6 2738static bfd_boolean elf_gc_sweep_symbol
268b6b39 2739 (struct elf_link_hash_entry *, void *);
252b5132 2740
b34976b6 2741static bfd_boolean elf_gc_allocate_got_offsets
268b6b39 2742 (struct elf_link_hash_entry *, void *);
252b5132
RH
2743
2744/* The mark phase of garbage collection. For a given section, mark
dbb410c3
AM
2745 it and any sections in this section's group, and all the sections
2746 which define symbols to which it refers. */
252b5132 2747
b34976b6 2748typedef asection * (*gc_mark_hook_fn)
268b6b39
AM
2749 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2750 struct elf_link_hash_entry *, Elf_Internal_Sym *);
b34976b6
AM
2751
2752static bfd_boolean
268b6b39
AM
2753elf_gc_mark (struct bfd_link_info *info,
2754 asection *sec,
2755 gc_mark_hook_fn gc_mark_hook)
252b5132 2756{
b34976b6 2757 bfd_boolean ret;
dbb410c3 2758 asection *group_sec;
252b5132
RH
2759
2760 sec->gc_mark = 1;
2761
dbb410c3
AM
2762 /* Mark all the sections in the group. */
2763 group_sec = elf_section_data (sec)->next_in_group;
2764 if (group_sec && !group_sec->gc_mark)
2765 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
b34976b6 2766 return FALSE;
252b5132 2767
dbb410c3 2768 /* Look through the section relocs. */
b34976b6 2769 ret = TRUE;
252b5132
RH
2770 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
2771 {
2772 Elf_Internal_Rela *relstart, *rel, *relend;
2773 Elf_Internal_Shdr *symtab_hdr;
2774 struct elf_link_hash_entry **sym_hashes;
2775 size_t nlocsyms;
2776 size_t extsymoff;
252b5132 2777 bfd *input_bfd = sec->owner;
9c5bfbb7 2778 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
6cdc0ccc 2779 Elf_Internal_Sym *isym = NULL;
140f6c8e 2780 int r_sym_shift;
252b5132
RH
2781
2782 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2783 sym_hashes = elf_sym_hashes (input_bfd);
2784
2785 /* Read the local symbols. */
2786 if (elf_bad_symtab (input_bfd))
2787 {
140f6c8e 2788 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
c44233aa 2789 extsymoff = 0;
252b5132
RH
2790 }
2791 else
2792 extsymoff = nlocsyms = symtab_hdr->sh_info;
9ad5cbcf 2793
6cdc0ccc
AM
2794 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2795 if (isym == NULL && nlocsyms != 0)
9ad5cbcf 2796 {
6cdc0ccc
AM
2797 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
2798 NULL, NULL, NULL);
2799 if (isym == NULL)
b34976b6 2800 return FALSE;
9ad5cbcf
AM
2801 }
2802
252b5132 2803 /* Read the relocations. */
268b6b39 2804 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
45d6a902 2805 info->keep_memory);
252b5132
RH
2806 if (relstart == NULL)
2807 {
b34976b6 2808 ret = FALSE;
252b5132
RH
2809 goto out1;
2810 }
c7ac6ff8 2811 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132 2812
140f6c8e
AM
2813 if (bed->s->arch_size == 32)
2814 r_sym_shift = 8;
2815 else
2816 r_sym_shift = 32;
2817
252b5132
RH
2818 for (rel = relstart; rel < relend; rel++)
2819 {
2820 unsigned long r_symndx;
2821 asection *rsec;
2822 struct elf_link_hash_entry *h;
252b5132 2823
140f6c8e 2824 r_symndx = rel->r_info >> r_sym_shift;
252b5132
RH
2825 if (r_symndx == 0)
2826 continue;
2827
6cdc0ccc
AM
2828 if (r_symndx >= nlocsyms
2829 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
252b5132
RH
2830 {
2831 h = sym_hashes[r_symndx - extsymoff];
1e2f5b6e 2832 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
252b5132
RH
2833 }
2834 else
2835 {
6cdc0ccc 2836 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
252b5132
RH
2837 }
2838
2839 if (rsec && !rsec->gc_mark)
b91afed7
AM
2840 {
2841 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
2842 rsec->gc_mark = 1;
2843 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
2844 {
b34976b6 2845 ret = FALSE;
b91afed7
AM
2846 goto out2;
2847 }
2848 }
252b5132
RH
2849 }
2850
2851 out2:
6cdc0ccc 2852 if (elf_section_data (sec)->relocs != relstart)
252b5132
RH
2853 free (relstart);
2854 out1:
6cdc0ccc
AM
2855 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
2856 {
2857 if (! info->keep_memory)
2858 free (isym);
2859 else
2860 symtab_hdr->contents = (unsigned char *) isym;
2861 }
252b5132
RH
2862 }
2863
2864 return ret;
2865}
2866
2867/* The sweep phase of garbage collection. Remove all garbage sections. */
2868
b34976b6 2869typedef bfd_boolean (*gc_sweep_hook_fn)
268b6b39 2870 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
b34976b6
AM
2871
2872static bfd_boolean
268b6b39 2873elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
252b5132
RH
2874{
2875 bfd *sub;
2876
2877 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2878 {
2879 asection *o;
2880
f6af82bd
AM
2881 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
2882 continue;
2883
252b5132
RH
2884 for (o = sub->sections; o != NULL; o = o->next)
2885 {
2886 /* Keep special sections. Keep .debug sections. */
2887 if ((o->flags & SEC_LINKER_CREATED)
2888 || (o->flags & SEC_DEBUGGING))
2889 o->gc_mark = 1;
2890
2891 if (o->gc_mark)
2892 continue;
2893
2894 /* Skip sweeping sections already excluded. */
2895 if (o->flags & SEC_EXCLUDE)
2896 continue;
2897
2898 /* Since this is early in the link process, it is simple
2899 to remove a section from the output. */
2900 o->flags |= SEC_EXCLUDE;
2901
2902 /* But we also have to update some of the relocation
2903 info we collected before. */
2904 if (gc_sweep_hook
2905 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
2906 {
2907 Elf_Internal_Rela *internal_relocs;
b34976b6 2908 bfd_boolean r;
252b5132 2909
45d6a902 2910 internal_relocs
268b6b39 2911 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
45d6a902 2912 info->keep_memory);
252b5132 2913 if (internal_relocs == NULL)
b34976b6 2914 return FALSE;
252b5132 2915
3e932841 2916 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
252b5132 2917
6cdc0ccc 2918 if (elf_section_data (o)->relocs != internal_relocs)
252b5132
RH
2919 free (internal_relocs);
2920
2921 if (!r)
b34976b6 2922 return FALSE;
252b5132
RH
2923 }
2924 }
2925 }
2926
2927 /* Remove the symbols that were in the swept sections from the dynamic
2928 symbol table. GCFIXME: Anyone know how to get them out of the
2929 static symbol table as well? */
2930 {
2931 int i = 0;
2932
268b6b39 2933 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
252b5132
RH
2934
2935 elf_hash_table (info)->dynsymcount = i;
2936 }
2937
b34976b6 2938 return TRUE;
252b5132
RH
2939}
2940
2941/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
2942
b34976b6 2943static bfd_boolean
268b6b39 2944elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
252b5132 2945{
268b6b39 2946 int *idx = idxptr;
252b5132 2947
e92d460e
AM
2948 if (h->root.type == bfd_link_hash_warning)
2949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2950
252b5132
RH
2951 if (h->dynindx != -1
2952 && ((h->root.type != bfd_link_hash_defined
2953 && h->root.type != bfd_link_hash_defweak)
2954 || h->root.u.def.section->gc_mark))
2955 h->dynindx = (*idx)++;
2956
b34976b6 2957 return TRUE;
252b5132
RH
2958}
2959
4cc11e76 2960/* Propagate collected vtable information. This is called through
252b5132
RH
2961 elf_link_hash_traverse. */
2962
b34976b6 2963static bfd_boolean
268b6b39 2964elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
252b5132 2965{
e92d460e
AM
2966 if (h->root.type == bfd_link_hash_warning)
2967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2968
3e932841 2969 /* Those that are not vtables. */
252b5132 2970 if (h->vtable_parent == NULL)
b34976b6 2971 return TRUE;
252b5132
RH
2972
2973 /* Those vtables that do not have parents, we cannot merge. */
2974 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
b34976b6 2975 return TRUE;
252b5132
RH
2976
2977 /* If we've already been done, exit. */
2978 if (h->vtable_entries_used && h->vtable_entries_used[-1])
b34976b6 2979 return TRUE;
252b5132
RH
2980
2981 /* Make sure the parent's table is up to date. */
2982 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
2983
2984 if (h->vtable_entries_used == NULL)
2985 {
2986 /* None of this table's entries were referenced. Re-use the
2987 parent's table. */
2988 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
2989 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
2990 }
2991 else
2992 {
2993 size_t n;
b34976b6 2994 bfd_boolean *cu, *pu;
252b5132
RH
2995
2996 /* Or the parent's entries into ours. */
2997 cu = h->vtable_entries_used;
b34976b6 2998 cu[-1] = TRUE;
252b5132
RH
2999 pu = h->vtable_parent->vtable_entries_used;
3000 if (pu != NULL)
3001 {
9c5bfbb7
AM
3002 const struct elf_backend_data *bed;
3003 unsigned int log_file_align;
0d1ea5c0 3004
9c5bfbb7
AM
3005 bed = get_elf_backend_data (h->root.u.def.section->owner);
3006 log_file_align = bed->s->log_file_align;
45d6a902 3007 n = h->vtable_parent->vtable_entries_size >> log_file_align;
374b596d 3008 while (n--)
252b5132 3009 {
374b596d 3010 if (*pu)
b34976b6 3011 *cu = TRUE;
374b596d
NC
3012 pu++;
3013 cu++;
252b5132
RH
3014 }
3015 }
3016 }
3017
b34976b6 3018 return TRUE;
252b5132
RH
3019}
3020
b34976b6 3021static bfd_boolean
268b6b39 3022elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
252b5132
RH
3023{
3024 asection *sec;
3025 bfd_vma hstart, hend;
3026 Elf_Internal_Rela *relstart, *relend, *rel;
9c5bfbb7 3027 const struct elf_backend_data *bed;
45d6a902 3028 unsigned int log_file_align;
252b5132 3029
e92d460e
AM
3030 if (h->root.type == bfd_link_hash_warning)
3031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3032
252b5132
RH
3033 /* Take care of both those symbols that do not describe vtables as
3034 well as those that are not loaded. */
3035 if (h->vtable_parent == NULL)
b34976b6 3036 return TRUE;
252b5132
RH
3037
3038 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3039 || h->root.type == bfd_link_hash_defweak);
3040
3041 sec = h->root.u.def.section;
3042 hstart = h->root.u.def.value;
3043 hend = hstart + h->size;
3044
268b6b39 3045 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
252b5132 3046 if (!relstart)
b34976b6 3047 return *(bfd_boolean *) okp = FALSE;
c7ac6ff8 3048 bed = get_elf_backend_data (sec->owner);
45d6a902 3049 log_file_align = bed->s->log_file_align;
0d1ea5c0 3050
c7ac6ff8 3051 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
252b5132
RH
3052
3053 for (rel = relstart; rel < relend; ++rel)
3054 if (rel->r_offset >= hstart && rel->r_offset < hend)
3055 {
3056 /* If the entry is in use, do nothing. */
3057 if (h->vtable_entries_used
3058 && (rel->r_offset - hstart) < h->vtable_entries_size)
3059 {
45d6a902 3060 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
252b5132
RH
3061 if (h->vtable_entries_used[entry])
3062 continue;
3063 }
3064 /* Otherwise, kill it. */
3065 rel->r_offset = rel->r_info = rel->r_addend = 0;
3066 }
3067
b34976b6 3068 return TRUE;
252b5132
RH
3069}
3070
3071/* Do mark and sweep of unused sections. */
3072
b34976b6 3073bfd_boolean
268b6b39 3074elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 3075{
b34976b6 3076 bfd_boolean ok = TRUE;
252b5132
RH
3077 bfd *sub;
3078 asection * (*gc_mark_hook)
268b6b39
AM
3079 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
3080 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
252b5132
RH
3081
3082 if (!get_elf_backend_data (abfd)->can_gc_sections
0eddce27
AM
3083 || info->relocatable
3084 || info->emitrelocations
3085 || !is_elf_hash_table (info->hash)
252b5132 3086 || elf_hash_table (info)->dynamic_sections_created)
2627de83
AM
3087 {
3088 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
3089 return TRUE;
3090 }
252b5132
RH
3091
3092 /* Apply transitive closure to the vtable entry usage info. */
3093 elf_link_hash_traverse (elf_hash_table (info),
3094 elf_gc_propagate_vtable_entries_used,
268b6b39 3095 &ok);
252b5132 3096 if (!ok)
b34976b6 3097 return FALSE;
252b5132
RH
3098
3099 /* Kill the vtable relocations that were not used. */
3100 elf_link_hash_traverse (elf_hash_table (info),
3101 elf_gc_smash_unused_vtentry_relocs,
268b6b39 3102 &ok);
252b5132 3103 if (!ok)
b34976b6 3104 return FALSE;
252b5132
RH
3105
3106 /* Grovel through relocs to find out who stays ... */
3107
3108 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
3109 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3110 {
3111 asection *o;
f6af82bd
AM
3112
3113 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
3114 continue;
3115
252b5132
RH
3116 for (o = sub->sections; o != NULL; o = o->next)
3117 {
3118 if (o->flags & SEC_KEEP)
c44233aa 3119 if (!elf_gc_mark (info, o, gc_mark_hook))
b34976b6 3120 return FALSE;
252b5132
RH
3121 }
3122 }
3123
3124 /* ... and mark SEC_EXCLUDE for those that go. */
a7b97311 3125 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
b34976b6 3126 return FALSE;
252b5132 3127
b34976b6 3128 return TRUE;
252b5132
RH
3129}
3130\f
4cc11e76 3131/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
252b5132 3132
b34976b6 3133bfd_boolean
268b6b39
AM
3134elf_gc_record_vtinherit (bfd *abfd,
3135 asection *sec,
3136 struct elf_link_hash_entry *h,
3137 bfd_vma offset)
252b5132
RH
3138{
3139 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3140 struct elf_link_hash_entry **search, *child;
3141 bfd_size_type extsymcount;
140f6c8e 3142 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
3143
3144 /* The sh_info field of the symtab header tells us where the
3145 external symbols start. We don't care about the local symbols at
3146 this point. */
140f6c8e 3147 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
252b5132
RH
3148 if (!elf_bad_symtab (abfd))
3149 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
3150
3151 sym_hashes = elf_sym_hashes (abfd);
3152 sym_hashes_end = sym_hashes + extsymcount;
3153
3154 /* Hunt down the child symbol, which is in this section at the same
3155 offset as the relocation. */
3156 for (search = sym_hashes; search != sym_hashes_end; ++search)
3157 {
3158 if ((child = *search) != NULL
3159 && (child->root.type == bfd_link_hash_defined
3160 || child->root.type == bfd_link_hash_defweak)
3161 && child->root.u.def.section == sec
3162 && child->root.u.def.value == offset)
3163 goto win;
3164 }
3165
3166 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8f615d07 3167 bfd_archive_filename (abfd), sec->name,
a7b97311 3168 (unsigned long) offset);
252b5132 3169 bfd_set_error (bfd_error_invalid_operation);
b34976b6 3170 return FALSE;
252b5132 3171
dc810e39 3172 win:
252b5132
RH
3173 if (!h)
3174 {
3175 /* This *should* only be the absolute section. It could potentially
3176 be that someone has defined a non-global vtable though, which
3177 would be bad. It isn't worth paging in the local symbols to be
3178 sure though; that case should simply be handled by the assembler. */
3179
3180 child->vtable_parent = (struct elf_link_hash_entry *) -1;
3181 }
3182 else
3183 child->vtable_parent = h;
3184
b34976b6 3185 return TRUE;
252b5132
RH
3186}
3187
4cc11e76 3188/* Called from check_relocs to record the existence of a VTENTRY reloc. */
252b5132 3189
b34976b6 3190bfd_boolean
268b6b39
AM
3191elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
3192 asection *sec ATTRIBUTE_UNUSED,
3193 struct elf_link_hash_entry *h,
3194 bfd_vma addend)
252b5132 3195{
9c5bfbb7 3196 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 3197 unsigned int log_file_align = bed->s->log_file_align;
0d1ea5c0 3198
64c456e1 3199 if (addend >= h->vtable_entries_size)
252b5132 3200 {
64c456e1 3201 size_t size, bytes, file_align;
b34976b6 3202 bfd_boolean *ptr = h->vtable_entries_used;
252b5132
RH
3203
3204 /* While the symbol is undefined, we have to be prepared to handle
3205 a zero size. */
64c456e1 3206 file_align = 1 << log_file_align;
252b5132 3207 if (h->root.type == bfd_link_hash_undefined)
64c456e1 3208 size = addend + file_align;
252b5132
RH
3209 else
3210 {
3211 size = h->size;
64c456e1 3212 if (addend >= size)
252b5132
RH
3213 {
3214 /* Oops! We've got a reference past the defined end of
3215 the table. This is probably a bug -- shall we warn? */
64c456e1 3216 size = addend + file_align;
252b5132
RH
3217 }
3218 }
64c456e1 3219 size = (size + file_align - 1) & -file_align;
252b5132
RH
3220
3221 /* Allocate one extra entry for use as a "done" flag for the
64c456e1
AM
3222 consolidation pass. */
3223 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
252b5132
RH
3224
3225 if (ptr)
3226 {
268b6b39 3227 ptr = bfd_realloc (ptr - 1, bytes);
3e932841 3228
fed79cc6
NC
3229 if (ptr != NULL)
3230 {
3231 size_t oldbytes;
252b5132 3232
45d6a902 3233 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
b34976b6 3234 * sizeof (bfd_boolean));
a7b97311 3235 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
fed79cc6 3236 }
252b5132
RH
3237 }
3238 else
268b6b39 3239 ptr = bfd_zmalloc (bytes);
252b5132 3240
fed79cc6 3241 if (ptr == NULL)
b34976b6 3242 return FALSE;
3e932841 3243
252b5132 3244 /* And arrange for that done flag to be at index -1. */
fed79cc6 3245 h->vtable_entries_used = ptr + 1;
252b5132
RH
3246 h->vtable_entries_size = size;
3247 }
3e932841 3248
45d6a902 3249 h->vtable_entries_used[addend >> log_file_align] = TRUE;
252b5132 3250
b34976b6 3251 return TRUE;
252b5132
RH
3252}
3253
140f6c8e
AM
3254struct alloc_got_off_arg {
3255 bfd_vma gotoff;
3256 unsigned int got_elt_size;
3257};
3258
252b5132
RH
3259/* And an accompanying bit to work out final got entry offsets once
3260 we're done. Should be called from final_link. */
3261
b34976b6 3262bfd_boolean
268b6b39
AM
3263elf_gc_common_finalize_got_offsets (bfd *abfd,
3264 struct bfd_link_info *info)
252b5132
RH
3265{
3266 bfd *i;
9c5bfbb7 3267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 3268 bfd_vma gotoff;
140f6c8e
AM
3269 unsigned int got_elt_size = bed->s->arch_size / 8;
3270 struct alloc_got_off_arg gofarg;
252b5132 3271
0eddce27
AM
3272 if (! is_elf_hash_table (info->hash))
3273 return FALSE;
3274
252b5132
RH
3275 /* The GOT offset is relative to the .got section, but the GOT header is
3276 put into the .got.plt section, if the backend uses it. */
3277 if (bed->want_got_plt)
3278 gotoff = 0;
3279 else
3280 gotoff = bed->got_header_size;
3281
3282 /* Do the local .got entries first. */
3283 for (i = info->input_bfds; i; i = i->link_next)
3284 {
f6af82bd 3285 bfd_signed_vma *local_got;
252b5132
RH
3286 bfd_size_type j, locsymcount;
3287 Elf_Internal_Shdr *symtab_hdr;
3288
f6af82bd
AM
3289 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
3290 continue;
3291
3292 local_got = elf_local_got_refcounts (i);
252b5132
RH
3293 if (!local_got)
3294 continue;
3295
3296 symtab_hdr = &elf_tdata (i)->symtab_hdr;
3297 if (elf_bad_symtab (i))
140f6c8e 3298 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
252b5132
RH
3299 else
3300 locsymcount = symtab_hdr->sh_info;
3301
3302 for (j = 0; j < locsymcount; ++j)
3303 {
3304 if (local_got[j] > 0)
3305 {
3306 local_got[j] = gotoff;
140f6c8e 3307 gotoff += got_elt_size;
252b5132
RH
3308 }
3309 else
3310 local_got[j] = (bfd_vma) -1;
3311 }
3312 }
3313
dd5724d5
AM
3314 /* Then the global .got entries. .plt refcounts are handled by
3315 adjust_dynamic_symbol */
140f6c8e
AM
3316 gofarg.gotoff = gotoff;
3317 gofarg.got_elt_size = got_elt_size;
252b5132
RH
3318 elf_link_hash_traverse (elf_hash_table (info),
3319 elf_gc_allocate_got_offsets,
140f6c8e 3320 &gofarg);
b34976b6 3321 return TRUE;
252b5132
RH
3322}
3323
3324/* We need a special top-level link routine to convert got reference counts
3325 to real got offsets. */
3326
b34976b6 3327static bfd_boolean
140f6c8e 3328elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
252b5132 3329{
140f6c8e 3330 struct alloc_got_off_arg *gofarg = arg;
252b5132 3331
e92d460e
AM
3332 if (h->root.type == bfd_link_hash_warning)
3333 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3334
252b5132
RH
3335 if (h->got.refcount > 0)
3336 {
140f6c8e
AM
3337 h->got.offset = gofarg->gotoff;
3338 gofarg->gotoff += gofarg->got_elt_size;
252b5132
RH
3339 }
3340 else
3341 h->got.offset = (bfd_vma) -1;
3342
b34976b6 3343 return TRUE;
252b5132
RH
3344}
3345
3346/* Many folk need no more in the way of final link than this, once
3347 got entry reference counting is enabled. */
3348
b34976b6 3349bfd_boolean
268b6b39 3350elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
3351{
3352 if (!elf_gc_common_finalize_got_offsets (abfd, info))
b34976b6 3353 return FALSE;
252b5132
RH
3354
3355 /* Invoke the regular ELF backend linker to do all the work. */
3356 return elf_bfd_final_link (abfd, info);
3357}
3358
b34976b6 3359bfd_boolean
268b6b39 3360elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
73d074b4 3361{
268b6b39 3362 struct elf_reloc_cookie *rcookie = cookie;
73d074b4
DJ
3363
3364 if (rcookie->bad_symtab)
3365 rcookie->rel = rcookie->rels;
3366
3367 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
3368 {
d6fe2dc1 3369 unsigned long r_symndx;
73d074b4
DJ
3370
3371 if (! rcookie->bad_symtab)
3372 if (rcookie->rel->r_offset > offset)
b34976b6 3373 return FALSE;
73d074b4
DJ
3374 if (rcookie->rel->r_offset != offset)
3375 continue;
3376
140f6c8e 3377 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
d6fe2dc1 3378 if (r_symndx == SHN_UNDEF)
b34976b6 3379 return TRUE;
d6fe2dc1 3380
73d074b4 3381 if (r_symndx >= rcookie->locsymcount
6cdc0ccc 3382 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
73d074b4
DJ
3383 {
3384 struct elf_link_hash_entry *h;
3385
3386 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
3387
3388 while (h->root.type == bfd_link_hash_indirect
3389 || h->root.type == bfd_link_hash_warning)
3390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3391
3392 if ((h->root.type == bfd_link_hash_defined
3393 || h->root.type == bfd_link_hash_defweak)
ed4de5e2 3394 && elf_discarded_section (h->root.u.def.section))
b34976b6 3395 return TRUE;
73d074b4 3396 else
b34976b6 3397 return FALSE;
73d074b4 3398 }
6cdc0ccc 3399 else
73d074b4
DJ
3400 {
3401 /* It's not a relocation against a global symbol,
44421011 3402 but it could be a relocation against a local
73d074b4
DJ
3403 symbol for a discarded section. */
3404 asection *isec;
6cdc0ccc 3405 Elf_Internal_Sym *isym;
73d074b4
DJ
3406
3407 /* Need to: get the symbol; get the section. */
6cdc0ccc
AM
3408 isym = &rcookie->locsyms[r_symndx];
3409 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
73d074b4 3410 {
140f6c8e 3411 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
ed4de5e2 3412 if (isec != NULL && elf_discarded_section (isec))
b34976b6 3413 return TRUE;
73d074b4
DJ
3414 }
3415 }
b34976b6 3416 return FALSE;
73d074b4 3417 }
b34976b6 3418 return FALSE;
73d074b4
DJ
3419}
3420
3421/* Discard unneeded references to discarded sections.
b34976b6 3422 Returns TRUE if any section's size was changed. */
73d074b4 3423/* This function assumes that the relocations are in sorted order,
ab3acfbe 3424 which is true for all known assemblers. */
73d074b4 3425
b34976b6 3426bfd_boolean
268b6b39 3427elf_bfd_discard_info (bfd *output_bfd, struct bfd_link_info *info)
73d074b4
DJ
3428{
3429 struct elf_reloc_cookie cookie;
126495ed 3430 asection *stab, *eh;
73d074b4 3431 Elf_Internal_Shdr *symtab_hdr;
9c5bfbb7 3432 const struct elf_backend_data *bed;
73d074b4 3433 bfd *abfd;
99eb2ac8 3434 unsigned int count;
b34976b6 3435 bfd_boolean ret = FALSE;
73d074b4 3436
d6fe2dc1 3437 if (info->traditional_format
0eddce27 3438 || !is_elf_hash_table (info->hash))
b34976b6 3439 return FALSE;
65765700 3440
73d074b4
DJ
3441 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
3442 {
163c1c30
L
3443 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
3444 continue;
3445
73d074b4
DJ
3446 bed = get_elf_backend_data (abfd);
3447
3448 if ((abfd->flags & DYNAMIC) != 0)
3449 continue;
3450
126495ed 3451 eh = bfd_get_section_by_name (abfd, ".eh_frame");
1049f94e 3452 if (info->relocatable
2d36fe5f
AH
3453 || (eh != NULL
3454 && (eh->_raw_size == 0
3455 || bfd_is_abs_section (eh->output_section))))
126495ed 3456 eh = NULL;
65765700 3457
99eb2ac8
AM
3458 stab = bfd_get_section_by_name (abfd, ".stab");
3459 if (stab != NULL
3460 && (stab->_raw_size == 0
3461 || bfd_is_abs_section (stab->output_section)
68bfbfcc 3462 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
99eb2ac8
AM
3463 stab = NULL;
3464
3465 if (stab == NULL
3466 && eh == NULL
3467 && bed->elf_backend_discard_info == NULL)
73d074b4
DJ
3468 continue;
3469
3470 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
73d074b4
DJ
3471 cookie.abfd = abfd;
3472 cookie.sym_hashes = elf_sym_hashes (abfd);
3473 cookie.bad_symtab = elf_bad_symtab (abfd);
3474 if (cookie.bad_symtab)
3475 {
140f6c8e 3476 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
73d074b4
DJ
3477 cookie.extsymoff = 0;
3478 }
3479 else
3480 {
3481 cookie.locsymcount = symtab_hdr->sh_info;
3482 cookie.extsymoff = symtab_hdr->sh_info;
3483 }
3484
140f6c8e
AM
3485 if (bed->s->arch_size == 32)
3486 cookie.r_sym_shift = 8;
3487 else
3488 cookie.r_sym_shift = 32;
3489
6cdc0ccc
AM
3490 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3491 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
c44233aa 3492 {
6cdc0ccc
AM
3493 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3494 cookie.locsymcount, 0,
3495 NULL, NULL, NULL);
c44233aa 3496 if (cookie.locsyms == NULL)
b34976b6 3497 return FALSE;
9ad5cbcf 3498 }
73d074b4 3499
99eb2ac8 3500 if (stab != NULL)
73d074b4 3501 {
99eb2ac8
AM
3502 cookie.rels = NULL;
3503 count = stab->reloc_count;
3504 if (count != 0)
268b6b39
AM
3505 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
3506 info->keep_memory);
99eb2ac8 3507 if (cookie.rels != NULL)
73d074b4
DJ
3508 {
3509 cookie.rel = cookie.rels;
99eb2ac8
AM
3510 cookie.relend = cookie.rels;
3511 cookie.relend += count * bed->s->int_rels_per_ext_rel;
65765700
JJ
3512 if (_bfd_discard_section_stabs (abfd, stab,
3513 elf_section_data (stab)->sec_info,
73d074b4
DJ
3514 elf_reloc_symbol_deleted_p,
3515 &cookie))
b34976b6 3516 ret = TRUE;
6cdc0ccc 3517 if (elf_section_data (stab)->relocs != cookie.rels)
73d074b4
DJ
3518 free (cookie.rels);
3519 }
3520 }
3521
99eb2ac8 3522 if (eh != NULL)
65765700
JJ
3523 {
3524 cookie.rels = NULL;
99eb2ac8
AM
3525 count = eh->reloc_count;
3526 if (count != 0)
268b6b39
AM
3527 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
3528 info->keep_memory);
99eb2ac8
AM
3529 cookie.rel = cookie.rels;
3530 cookie.relend = cookie.rels;
3531 if (cookie.rels != NULL)
3532 cookie.relend += count * bed->s->int_rels_per_ext_rel;
3533
126495ed 3534 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
65765700
JJ
3535 elf_reloc_symbol_deleted_p,
3536 &cookie))
b34976b6 3537 ret = TRUE;
99eb2ac8
AM
3538
3539 if (cookie.rels != NULL
3540 && elf_section_data (eh)->relocs != cookie.rels)
65765700
JJ
3541 free (cookie.rels);
3542 }
3543
99eb2ac8
AM
3544 if (bed->elf_backend_discard_info != NULL
3545 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
b34976b6 3546 ret = TRUE;
73d074b4 3547
6cdc0ccc
AM
3548 if (cookie.locsyms != NULL
3549 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
3550 {
3551 if (! info->keep_memory)
3552 free (cookie.locsyms);
3553 else
3554 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
3555 }
73d074b4 3556 }
65765700 3557
126495ed 3558 if (info->eh_frame_hdr
1049f94e 3559 && !info->relocatable
126495ed 3560 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
b34976b6 3561 ret = TRUE;
99eb2ac8 3562
73d074b4
DJ
3563 return ret;
3564}
3565
b34976b6 3566static bfd_boolean
268b6b39 3567elf_section_ignore_discarded_relocs (asection *sec)
73d074b4 3568{
9c5bfbb7 3569 const struct elf_backend_data *bed;
40b829d4 3570
68bfbfcc 3571 switch (sec->sec_info_type)
65765700
JJ
3572 {
3573 case ELF_INFO_TYPE_STABS:
3574 case ELF_INFO_TYPE_EH_FRAME:
b34976b6 3575 return TRUE;
65765700
JJ
3576 default:
3577 break;
3578 }
40b829d4
AM
3579
3580 bed = get_elf_backend_data (sec->owner);
3581 if (bed->elf_backend_ignore_discarded_relocs != NULL
3582 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
b34976b6 3583 return TRUE;
65765700 3584
b34976b6 3585 return FALSE;
73d074b4 3586}
This page took 0.681377 seconds and 4 git commands to generate.