1 /* Meta support for 32-bit ELF
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 Contributed by Imagination Technologies Ltd.
5 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "elf32-metag.h"
27 #include "elf/metag.h"
29 #define GOT_ENTRY_SIZE 4
30 #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
34 1 - with GOT offset */
35 #define METAG_ELF_ABI_VERSION 1
37 static const unsigned int plt0_entry[] =
39 0x02000005, /* MOVT D0Re0, #HI(GOT+4) */
40 0x02000000, /* ADD D0Re0, D0Re0, #LO(GOT+4) */
41 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
42 0xc600012a, /* GETD PC, [D0Re0+#4] */
46 static const unsigned int plt0_pic_entry[] =
48 0x82900001, /* ADDT A0.2, CPC0, #0 */
49 0x82100000, /* ADD A0.2, A0.2, #0 */
50 0xa3100c20, /* MOV D0Re0, A0.2 */
51 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
52 0xc600012a, /* GETD PC, [D0Re0+#4] */
55 static const unsigned int plt_entry[] =
57 0x82100005, /* MOVT A0.2, #HI(GOT+off) */
58 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
59 0xc600806a, /* GETD PC, [A0.2] */
60 0x03000004, /* MOV D1Re0, #LO(offset) */
61 0xa0000000 /* B PLT0 */
64 static const unsigned int plt_pic_entry[] =
66 0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */
67 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
68 0xc600806a, /* GETD PC, [A0.2] */
69 0x03000004, /* MOV D1Re0, #LO(offset) */
70 0xa0000000 /* B PLT0 */
73 /* Variable names follow a coding style.
74 Please follow this (Apps Hungarian) style:
76 Structure/Variable Prefix
77 elf_link_hash_table "etab"
78 elf_link_hash_entry "eh"
80 elf_metag_link_hash_table "htab"
81 elf_metag_link_hash_entry "hh"
83 bfd_link_hash_table "btab"
84 bfd_link_hash_entry "bh"
86 bfd_hash_table containing stubs "bstab"
87 elf_metag_stub_hash_entry "hsh"
89 elf_metag_dyn_reloc_entry "hdh"
91 Always remember to use GNU Coding Style. */
93 #define PLT_ENTRY_SIZE sizeof(plt_entry)
95 static reloc_howto_type elf_metag_howto_table[] =
97 /* High order 16 bit absolute. */
98 HOWTO (R_METAG_HIADDR16, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 FALSE, /* pc_relative */
104 complain_overflow_dont, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_METAG_HIADDR16", /* name */
107 FALSE, /* partial_inplace */
109 0x0007fff8, /* dst_mask */
110 FALSE), /* pcrel_offset */
112 /* Low order 16 bit absolute. */
113 HOWTO (R_METAG_LOADDR16, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE, /* pc_relative */
119 complain_overflow_dont,/* complain_on_overflow */
120 bfd_elf_generic_reloc, /* special_function */
121 "R_METAG_LOADDR16", /* name */
122 FALSE, /* partial_inplace */
124 0x0007fff8, /* dst_mask */
125 FALSE), /* pcrel_offset */
127 /* 32 bit absolute. */
128 HOWTO (R_METAG_ADDR32, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 FALSE, /* pc_relative */
134 complain_overflow_bitfield, /* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_METAG_ADDR32", /* name */
137 FALSE, /* partial_inplace */
138 0x00000000, /* src_mask */
139 0xffffffff, /* dst_mask */
140 FALSE), /* pcrel_offset */
143 HOWTO (R_METAG_NONE, /* type */
145 3, /* size (0 = byte, 1 = short, 2 = long) */
147 FALSE, /* pc_relative */
149 complain_overflow_dont, /* complain_on_overflow */
150 bfd_elf_generic_reloc, /* special_function */
151 "R_METAG_NONE", /* name */
152 FALSE, /* partial_inplace */
155 FALSE), /* pcrel_offset */
157 /* 19 bit pc relative */
158 HOWTO (R_METAG_RELBRANCH, /* type */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
162 TRUE, /* pc_relative */
164 complain_overflow_signed, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_METAG_RELBRANCH", /* name */
167 FALSE, /* partial_inplace */
169 0x00ffffe0, /* dst_mask */
170 FALSE), /* pcrel_offset */
173 HOWTO (R_METAG_GETSETOFF, /* type */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
177 FALSE, /* pc_relative */
179 complain_overflow_dont, /* complain_on_overflow */
180 bfd_elf_generic_reloc, /* special_function */
181 "R_METAG_GETSETOFF", /* name */
182 FALSE, /* partial_inplace */
185 FALSE), /* pcrel_offset */
212 HOWTO (R_METAG_GNU_VTINHERIT, /* type */
214 2, /* size (0 = byte, 1 = short, 2 = long) */
216 FALSE, /* pc_relative */
218 complain_overflow_dont, /* complain_on_overflow */
219 NULL, /* special_function */
220 "R_METAG_GNU_VTINHERIT", /* name */
221 FALSE, /* partial_inplace */
224 FALSE), /* pcrel_offset */
226 HOWTO (R_METAG_GNU_VTENTRY, /* type */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
230 FALSE, /* pc_relative */
232 complain_overflow_dont, /* complain_on_overflow */
233 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
234 "R_METAG_GNU_VTENTRY", /* name */
235 FALSE, /* partial_inplace */
238 FALSE), /* pcrel_offset */
240 /* High order 16 bit GOT offset */
241 HOWTO (R_METAG_HI16_GOTOFF, /* type */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
245 FALSE, /* pc_relative */
247 complain_overflow_dont, /* complain_on_overflow */
248 bfd_elf_generic_reloc, /* special_function */
249 "R_METAG_HI16_GOTOFF", /* name */
250 FALSE, /* partial_inplace */
252 0x0007fff8, /* dst_mask */
253 FALSE), /* pcrel_offset */
255 /* Low order 16 bit GOT offset */
256 HOWTO (R_METAG_LO16_GOTOFF, /* type */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
260 FALSE, /* pc_relative */
262 complain_overflow_dont, /* complain_on_overflow */
263 bfd_elf_generic_reloc, /* special_function */
264 "R_METAG_LO16_GOTOFF", /* name */
265 FALSE, /* partial_inplace */
267 0x0007fff8, /* dst_mask */
268 FALSE), /* pcrel_offset */
270 /* GET/SET GOT offset */
271 HOWTO (R_METAG_GETSET_GOTOFF, /* type */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
275 FALSE, /* pc_relative */
277 complain_overflow_dont, /* complain_on_overflow */
278 bfd_elf_generic_reloc, /* special_function */
279 "R_METAG_GETSET_GOTOFF", /* name */
280 FALSE, /* partial_inplace */
283 FALSE), /* pcrel_offset */
285 /* GET/SET GOT relative */
286 HOWTO (R_METAG_GETSET_GOT, /* type */
288 1, /* size (0 = byte, 1 = short, 2 = long) */
290 FALSE, /* pc_relative */
292 complain_overflow_dont, /* complain_on_overflow */
293 bfd_elf_generic_reloc, /* special_function */
294 "R_METAG_GETSET_GOT", /* name */
295 FALSE, /* partial_inplace */
298 FALSE), /* pcrel_offset */
300 /* High order 16 bit GOT reference */
301 HOWTO (R_METAG_HI16_GOTPC, /* type */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
305 FALSE, /* pc_relative */
307 complain_overflow_dont, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_METAG_HI16_GOTPC", /* name */
310 FALSE, /* partial_inplace */
312 0x0007fff8, /* dst_mask */
313 FALSE), /* pcrel_offset */
315 /* Low order 16 bit GOT reference */
316 HOWTO (R_METAG_LO16_GOTPC, /* type */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
320 FALSE, /* pc_relative */
322 complain_overflow_dont, /* complain_on_overflow */
323 bfd_elf_generic_reloc, /* special_function */
324 "R_METAG_LO16_GOTPC", /* name */
325 FALSE, /* partial_inplace */
327 0x0007fff8, /* dst_mask */
328 FALSE), /* pcrel_offset */
330 /* High order 16 bit PLT */
331 HOWTO (R_METAG_HI16_PLT, /* type */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
335 FALSE, /* pc_relative */
337 complain_overflow_dont, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_METAG_HI16_PLT", /* name */
340 FALSE, /* partial_inplace */
342 0x0007fff8, /* dst_mask */
343 FALSE), /* pcrel_offset */
345 /* Low order 16 bit PLT */
346 HOWTO (R_METAG_LO16_PLT, /* type */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
350 FALSE, /* pc_relative */
352 complain_overflow_dont, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_METAG_LO16_PLT", /* name */
355 FALSE, /* partial_inplace */
357 0xffffffff, /* dst_mask */
358 FALSE), /* pcrel_offset */
360 HOWTO (R_METAG_RELBRANCH_PLT, /* type */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
364 TRUE, /* pc_relative */
366 complain_overflow_signed, /* complain_on_overflow */
367 bfd_elf_generic_reloc, /* special_function */
368 "R_METAG_RELBRANCH_PLT", /* name */
369 FALSE, /* partial_inplace */
371 0x00ffffe0, /* dst_mask */
372 FALSE), /* pcrel_offset */
374 /* Dummy relocs used by the linker internally. */
375 HOWTO (R_METAG_GOTOFF, /* type */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
379 FALSE, /* pc_relative */
381 complain_overflow_bitfield, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 "R_METAG_GOTOFF", /* name */
384 FALSE, /* partial_inplace */
385 0xffffffff, /* src_mask */
386 0xffffffff, /* dst_mask */
387 FALSE), /* pcrel_offset */
389 HOWTO (R_METAG_PLT, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 FALSE, /* pc_relative */
395 complain_overflow_bitfield, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 "R_METAG_GOTOFF", /* name */
398 FALSE, /* partial_inplace */
399 0xffffffff, /* src_mask */
400 0xffffffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 /* This is used only by the dynamic linker. The symbol should exist
404 both in the object being run and in some shared library. The
405 dynamic linker copies the data addressed by the symbol from the
406 shared library into the object, because the object being
407 run has to have the data at some particular address. */
408 HOWTO (R_METAG_COPY, /* type */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
412 FALSE, /* pc_relative */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc, /* special_function */
416 "R_METAG_COPY", /* name */
417 FALSE, /* partial_inplace */
418 0xffffffff, /* src_mask */
419 0xffffffff, /* dst_mask */
420 FALSE), /* pcrel_offset */
422 /* Marks a procedure linkage table entry for a symbol. */
423 HOWTO (R_METAG_JMP_SLOT, /* type */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
427 FALSE, /* pc_relative */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* special_function */
431 "R_METAG_JMP_SLOT", /* name */
432 FALSE, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
437 /* Used only by the dynamic linker. When the object is run, this
438 longword is set to the load address of the object, plus the
440 HOWTO (R_METAG_RELATIVE, /* type */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
444 FALSE, /* pc_relative */
446 complain_overflow_bitfield, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* special_function */
448 "R_METAG_RELATIVE", /* name */
449 FALSE, /* partial_inplace */
450 0xffffffff, /* src_mask */
451 0xffffffff, /* dst_mask */
452 FALSE), /* pcrel_offset */
454 HOWTO (R_METAG_GLOB_DAT, /* type */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
458 FALSE, /* pc_relative */
460 complain_overflow_bitfield, /* complain_on_overflow */
461 bfd_elf_generic_reloc, /* special_function */
462 "R_METAG_GLOB_DAT", /* name */
463 FALSE, /* partial_inplace */
464 0xffffffff, /* src_mask */
465 0xffffffff, /* dst_mask */
466 FALSE), /* pcrel_offset */
468 HOWTO (R_METAG_TLS_GD, /* type */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
472 FALSE, /* pc_relative */
474 complain_overflow_dont, /* complain_on_overflow */
475 bfd_elf_generic_reloc, /* special_function */
476 "R_METAG_TLS_GD", /* name */
477 FALSE, /* partial_inplace */
479 0x0007fff8, /* dst_mask */
480 FALSE), /* pcrel_offset */
482 HOWTO (R_METAG_TLS_LDM, /* type */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
486 FALSE, /* pc_relative */
488 complain_overflow_bitfield, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 "R_METAG_TLS_LDM", /* name */
491 FALSE, /* partial_inplace */
493 0x0007fff8, /* dst_mask */
494 FALSE), /* pcrel_offset */
496 HOWTO (R_METAG_TLS_LDO_HI16, /* type */
498 2, /* size (0 = byte, 1 = short, 2 = long) */
500 FALSE, /* pc_relative */
502 complain_overflow_bitfield, /* complain_on_overflow */
503 bfd_elf_generic_reloc, /* special_function */
504 "R_METAG_TLS_LDO_HI16", /* name */
505 FALSE, /* partial_inplace */
507 0x0007fff8, /* dst_mask */
508 FALSE), /* pcrel_offset */
510 HOWTO (R_METAG_TLS_LDO_LO16, /* type */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
514 FALSE, /* pc_relative */
516 complain_overflow_bitfield, /* complain_on_overflow */
517 bfd_elf_generic_reloc, /* special_function */
518 "R_METAG_TLS_LDO_LO16", /* name */
519 FALSE, /* partial_inplace */
521 0x0007fff8, /* dst_mask */
522 FALSE), /* pcrel_offset */
524 /* Dummy reloc used by the linker internally. */
525 HOWTO (R_METAG_TLS_LDO, /* type */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
529 FALSE, /* pc_relative */
531 complain_overflow_bitfield, /* complain_on_overflow */
532 bfd_elf_generic_reloc, /* special_function */
533 "R_METAG_TLS_LDO", /* name */
534 FALSE, /* partial_inplace */
536 0x0007fff8, /* dst_mask */
537 FALSE), /* pcrel_offset */
539 HOWTO (R_METAG_TLS_IE, /* type */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
543 FALSE, /* pc_relative */
545 complain_overflow_dont, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_METAG_TLS_IE", /* name */
548 FALSE, /* partial_inplace */
550 0x0007ff80, /* dst_mask */
551 FALSE), /* pcrel_offset */
553 /* Dummy reloc used by the linker internally. */
554 HOWTO (R_METAG_TLS_IENONPIC, /* type */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
558 FALSE, /* pc_relative */
560 complain_overflow_dont, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_METAG_TLS_IENONPIC", /* name */
563 FALSE, /* partial_inplace */
565 0x0007fff8, /* dst_mask */
566 FALSE), /* pcrel_offset */
568 HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */
570 2, /* size (0 = byte, 1 = short, 2 = long) */
572 FALSE, /* pc_relative */
574 complain_overflow_dont, /* complain_on_overflow */
575 bfd_elf_generic_reloc, /* special_function */
576 "R_METAG_TLS_IENONPIC_HI16", /* name */
577 FALSE, /* partial_inplace */
579 0x0007fff8, /* dst_mask */
580 FALSE), /* pcrel_offset */
582 HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
586 FALSE, /* pc_relative */
588 complain_overflow_dont, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_METAG_TLS_IENONPIC_LO16", /* name */
591 FALSE, /* partial_inplace */
593 0x0007fff8, /* dst_mask */
594 FALSE), /* pcrel_offset */
596 HOWTO (R_METAG_TLS_TPOFF, /* type */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
600 FALSE, /* pc_relative */
602 complain_overflow_bitfield, /* complain_on_overflow */
603 bfd_elf_generic_reloc, /* special_function */
604 "R_METAG_TLS_TPOFF", /* name */
605 FALSE, /* partial_inplace */
607 0xffffffff, /* dst_mask */
608 FALSE), /* pcrel_offset */
610 HOWTO (R_METAG_TLS_DTPMOD, /* type */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
614 FALSE, /* pc_relative */
616 complain_overflow_bitfield, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 "R_METAG_TLS_DTPMOD", /* name */
619 FALSE, /* partial_inplace */
621 0xffffffff, /* dst_mask */
622 FALSE), /* pcrel_offset */
624 HOWTO (R_METAG_TLS_DTPOFF, /* type */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
628 FALSE, /* pc_relative */
630 complain_overflow_bitfield, /* complain_on_overflow */
631 bfd_elf_generic_reloc, /* special_function */
632 "R_METAG_TLS_DTPOFF", /* name */
633 FALSE, /* partial_inplace */
635 0xffffffff, /* dst_mask */
636 FALSE), /* pcrel_offset */
638 /* Dummy reloc used by the linker internally. */
639 HOWTO (R_METAG_TLS_LE, /* type */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
643 FALSE, /* pc_relative */
645 complain_overflow_bitfield, /* complain_on_overflow */
646 bfd_elf_generic_reloc, /* special_function */
647 "R_METAG_TLS_LE", /* name */
648 FALSE, /* partial_inplace */
650 0xffffffff, /* dst_mask */
651 FALSE), /* pcrel_offset */
653 HOWTO (R_METAG_TLS_LE_HI16, /* type */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
657 FALSE, /* pc_relative */
659 complain_overflow_dont, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_METAG_TLS_LE_HI16", /* name */
662 FALSE, /* partial_inplace */
664 0x0007fff8, /* dst_mask */
665 FALSE), /* pcrel_offset */
667 HOWTO (R_METAG_TLS_LE_LO16, /* type */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
671 FALSE, /* pc_relative */
673 complain_overflow_dont, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* special_function */
675 "R_METAG_TLS_LE_LO16", /* name */
676 FALSE, /* partial_inplace */
678 0x0007fff8, /* dst_mask */
679 FALSE), /* pcrel_offset */
683 #define BRANCH_BITS 19
685 /* The GOT is typically accessed using a [GS]ETD instruction. The size of the
686 immediate offset which can be used in such instructions therefore limits
687 the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
688 is pointing to the base of the GOT then the size is limited to the maximum
689 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
690 in a [GS]ETD instruction is signed, so by setting the base address register
691 to an offset of that 0x2000 byte maximum unsigned offset from the base of
692 the GOT we can use negative offsets in addition to positive. This
693 effectively doubles the usable GOT size to 0x4000 bytes. */
694 #define GOT_REG_OFFSET 0x2000
696 struct metag_reloc_map
698 bfd_reloc_code_real_type bfd_reloc_val;
699 unsigned int metag_reloc_val;
702 static const struct metag_reloc_map metag_reloc_map [] =
704 { BFD_RELOC_NONE, R_METAG_NONE },
705 { BFD_RELOC_32, R_METAG_ADDR32 },
706 { BFD_RELOC_METAG_HIADDR16, R_METAG_HIADDR16 },
707 { BFD_RELOC_METAG_LOADDR16, R_METAG_LOADDR16 },
708 { BFD_RELOC_METAG_RELBRANCH, R_METAG_RELBRANCH },
709 { BFD_RELOC_METAG_GETSETOFF, R_METAG_GETSETOFF },
710 { BFD_RELOC_VTABLE_INHERIT, R_METAG_GNU_VTINHERIT },
711 { BFD_RELOC_VTABLE_ENTRY, R_METAG_GNU_VTENTRY },
712 { BFD_RELOC_METAG_REL8, R_METAG_REL8 },
713 { BFD_RELOC_METAG_REL16, R_METAG_REL16 },
714 { BFD_RELOC_METAG_HI16_GOTOFF, R_METAG_HI16_GOTOFF },
715 { BFD_RELOC_METAG_LO16_GOTOFF, R_METAG_LO16_GOTOFF },
716 { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF },
717 { BFD_RELOC_METAG_GETSET_GOT, R_METAG_GETSET_GOT },
718 { BFD_RELOC_METAG_HI16_GOTPC, R_METAG_HI16_GOTPC },
719 { BFD_RELOC_METAG_LO16_GOTPC, R_METAG_LO16_GOTPC },
720 { BFD_RELOC_METAG_HI16_PLT, R_METAG_HI16_PLT },
721 { BFD_RELOC_METAG_LO16_PLT, R_METAG_LO16_PLT },
722 { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT },
723 { BFD_RELOC_METAG_GOTOFF, R_METAG_GOTOFF },
724 { BFD_RELOC_METAG_PLT, R_METAG_PLT },
725 { BFD_RELOC_METAG_COPY, R_METAG_COPY },
726 { BFD_RELOC_METAG_JMP_SLOT, R_METAG_JMP_SLOT },
727 { BFD_RELOC_METAG_RELATIVE, R_METAG_RELATIVE },
728 { BFD_RELOC_METAG_GLOB_DAT, R_METAG_GLOB_DAT },
729 { BFD_RELOC_METAG_TLS_GD, R_METAG_TLS_GD },
730 { BFD_RELOC_METAG_TLS_LDM, R_METAG_TLS_LDM },
731 { BFD_RELOC_METAG_TLS_LDO_HI16, R_METAG_TLS_LDO_HI16 },
732 { BFD_RELOC_METAG_TLS_LDO_LO16, R_METAG_TLS_LDO_LO16 },
733 { BFD_RELOC_METAG_TLS_LDO, R_METAG_TLS_LDO },
734 { BFD_RELOC_METAG_TLS_IE, R_METAG_TLS_IE },
735 { BFD_RELOC_METAG_TLS_IENONPIC, R_METAG_TLS_IENONPIC },
736 { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 },
737 { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 },
738 { BFD_RELOC_METAG_TLS_TPOFF, R_METAG_TLS_TPOFF },
739 { BFD_RELOC_METAG_TLS_DTPMOD, R_METAG_TLS_DTPMOD },
740 { BFD_RELOC_METAG_TLS_DTPOFF, R_METAG_TLS_DTPOFF },
741 { BFD_RELOC_METAG_TLS_LE, R_METAG_TLS_LE },
742 { BFD_RELOC_METAG_TLS_LE_HI16, R_METAG_TLS_LE_HI16 },
743 { BFD_RELOC_METAG_TLS_LE_LO16, R_METAG_TLS_LE_LO16 },
746 enum elf_metag_stub_type
748 metag_stub_long_branch,
749 metag_stub_long_branch_shared,
753 struct elf_metag_stub_hash_entry
755 /* Base hash table entry structure. */
756 struct bfd_hash_entry bh_root;
758 /* The stub section. */
761 /* Offset within stub_sec of the beginning of this stub. */
764 /* Given the symbol's value and its section we can determine its final
765 value when building the stubs (so the stub knows where to jump. */
766 bfd_vma target_value;
767 asection *target_section;
769 enum elf_metag_stub_type stub_type;
771 /* The symbol table entry, if any, that this was derived from. */
772 struct elf_metag_link_hash_entry *hh;
774 /* And the reloc addend that this was derived from. */
777 /* Where this stub is being called from, or, in the case of combined
778 stub sections, the first input section in the group. */
782 struct elf_metag_link_hash_entry
784 struct elf_link_hash_entry eh;
786 /* A pointer to the most recently used stub hash entry against this
788 struct elf_metag_stub_hash_entry *hsh_cache;
790 /* Used to count relocations for delayed sizing of relocation
792 struct elf_metag_dyn_reloc_entry {
794 /* Next relocation in the chain. */
795 struct elf_metag_dyn_reloc_entry *hdh_next;
797 /* The input section of the reloc. */
800 /* Number of relocs copied in this section. */
803 /* Number of relative relocs copied for the input section. */
804 bfd_size_type relative_count;
809 GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8
813 struct elf_metag_link_hash_table
815 /* The main hash table. */
816 struct elf_link_hash_table etab;
818 /* The stub hash table. */
819 struct bfd_hash_table bstab;
821 /* Linker stub bfd. */
824 /* Linker call-backs. */
825 asection * (*add_stub_section) (const char *, asection *);
826 void (*layout_sections_again) (void);
828 /* Array to keep track of which stub sections have been created, and
829 information on stub grouping. */
832 /* This is the section to which stubs in the group will be
835 /* The stub section. */
839 /* Assorted information used by elf_metag_size_stubs. */
840 unsigned int bfd_count;
841 unsigned int top_index;
842 asection **input_list;
843 Elf_Internal_Sym **all_local_syms;
845 /* Small local sym cache. */
846 struct sym_cache sym_cache;
848 /* Data for LDM relocations. */
851 bfd_signed_vma refcount;
856 /* Return the base vma address which should be subtracted from the
857 real address when resolving a dtpoff relocation. This is PT_TLS
860 dtpoff_base (struct bfd_link_info *info)
862 /* If tls_sec is NULL, we should have signalled an error already. */
863 if (elf_hash_table (info)->tls_sec == NULL)
865 return elf_hash_table (info)->tls_sec->vma;
868 /* Return the relocation value for R_METAG_TLS_IE */
870 tpoff (struct bfd_link_info *info, bfd_vma address)
872 /* If tls_sec is NULL, we should have signalled an error already. */
873 if (elf_hash_table (info)->tls_sec == NULL)
875 /* METAG TLS ABI is variant I and static TLS blocks start just after
876 tcbhead structure which has 2 pointer fields. */
877 return (address - elf_hash_table (info)->tls_sec->vma
878 + align_power ((bfd_vma) 8,
879 elf_hash_table (info)->tls_sec->alignment_power));
883 metag_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
885 Elf_Internal_Rela *dst)
889 r_type = ELF32_R_TYPE (dst->r_info);
890 if (r_type >= (unsigned int) R_METAG_MAX)
892 /* xgettext:c-format */
893 _bfd_error_handler (_("%B: invalid METAG reloc number: %d"), abfd, r_type);
896 cache_ptr->howto = & elf_metag_howto_table [r_type];
899 static reloc_howto_type *
900 metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
901 bfd_reloc_code_real_type code)
905 for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++)
906 if (metag_reloc_map [i].bfd_reloc_val == code)
907 return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val];
912 static reloc_howto_type *
913 metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
918 for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++)
919 if (elf_metag_howto_table[i].name != NULL
920 && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0)
921 return &elf_metag_howto_table[i];
926 /* Various hash macros and functions. */
927 #define metag_link_hash_table(p) \
928 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
929 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
931 #define metag_elf_hash_entry(ent) \
932 ((struct elf_metag_link_hash_entry *)(ent))
934 #define metag_stub_hash_entry(ent) \
935 ((struct elf_metag_stub_hash_entry *)(ent))
937 #define metag_stub_hash_lookup(table, string, create, copy) \
938 ((struct elf_metag_stub_hash_entry *) \
939 bfd_hash_lookup ((table), (string), (create), (copy)))
941 #define metag_elf_local_got_tls_type(abfd) \
942 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
944 /* Assorted hash table functions. */
946 /* Initialize an entry in the stub hash table. */
948 static struct bfd_hash_entry *
949 stub_hash_newfunc (struct bfd_hash_entry *entry,
950 struct bfd_hash_table *table,
953 /* Allocate the structure if it has not already been allocated by a
957 entry = bfd_hash_allocate (table,
958 sizeof (struct elf_metag_stub_hash_entry));
963 /* Call the allocation method of the superclass. */
964 entry = bfd_hash_newfunc (entry, table, string);
967 struct elf_metag_stub_hash_entry *hsh;
969 /* Initialize the local fields. */
970 hsh = (struct elf_metag_stub_hash_entry *) entry;
971 hsh->stub_sec = NULL;
972 hsh->stub_offset = 0;
973 hsh->target_value = 0;
974 hsh->target_section = NULL;
975 hsh->stub_type = metag_stub_long_branch;
983 /* Initialize an entry in the link hash table. */
985 static struct bfd_hash_entry *
986 metag_link_hash_newfunc (struct bfd_hash_entry *entry,
987 struct bfd_hash_table *table,
990 /* Allocate the structure if it has not already been allocated by a
994 entry = bfd_hash_allocate (table,
995 sizeof (struct elf_metag_link_hash_entry));
1000 /* Call the allocation method of the superclass. */
1001 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1004 struct elf_metag_link_hash_entry *hh;
1006 /* Initialize the local fields. */
1007 hh = (struct elf_metag_link_hash_entry *) entry;
1008 hh->hsh_cache = NULL;
1009 hh->dyn_relocs = NULL;
1010 hh->tls_type = GOT_UNKNOWN;
1016 /* Free the derived linker hash table. */
1019 elf_metag_link_hash_table_free (bfd *obfd)
1021 struct elf_metag_link_hash_table *htab
1022 = (struct elf_metag_link_hash_table *) obfd->link.hash;
1024 bfd_hash_table_free (&htab->bstab);
1025 _bfd_elf_link_hash_table_free (obfd);
1028 /* Create the derived linker hash table. The Meta ELF port uses the derived
1029 hash table to keep information specific to the Meta ELF linker (without
1030 using static variables). */
1032 static struct bfd_link_hash_table *
1033 elf_metag_link_hash_table_create (bfd *abfd)
1035 struct elf_metag_link_hash_table *htab;
1036 bfd_size_type amt = sizeof (*htab);
1038 htab = bfd_zmalloc (amt);
1042 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
1043 metag_link_hash_newfunc,
1044 sizeof (struct elf_metag_link_hash_entry),
1051 /* Init the stub hash table too. */
1052 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
1053 sizeof (struct elf_metag_stub_hash_entry)))
1055 _bfd_elf_link_hash_table_free (abfd);
1058 htab->etab.root.hash_table_free = elf_metag_link_hash_table_free;
1060 return &htab->etab.root;
1063 /* Section name for stubs is the associated section name plus this
1065 #define STUB_SUFFIX ".stub"
1067 /* Build a name for an entry in the stub hash table. */
1070 metag_stub_name (const asection *input_section,
1071 const asection *sym_sec,
1072 const struct elf_metag_link_hash_entry *hh,
1073 const Elf_Internal_Rela *rel)
1080 len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1;
1081 stub_name = bfd_malloc (len);
1082 if (stub_name != NULL)
1084 sprintf (stub_name, "%08x_%s+%x",
1085 input_section->id & 0xffffffff,
1086 hh->eh.root.root.string,
1087 (int) rel->r_addend & 0xffffffff);
1092 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1093 stub_name = bfd_malloc (len);
1094 if (stub_name != NULL)
1096 sprintf (stub_name, "%08x_%x:%x+%x",
1097 input_section->id & 0xffffffff,
1098 sym_sec->id & 0xffffffff,
1099 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1100 (int) rel->r_addend & 0xffffffff);
1106 /* Look up an entry in the stub hash. Stub entries are cached because
1107 creating the stub name takes a bit of time. */
1109 static struct elf_metag_stub_hash_entry *
1110 metag_get_stub_entry (const asection *input_section,
1111 const asection *sym_sec,
1112 struct elf_metag_link_hash_entry *hh,
1113 const Elf_Internal_Rela *rel,
1114 struct elf_metag_link_hash_table *htab)
1116 struct elf_metag_stub_hash_entry *hsh;
1117 const asection *id_sec;
1119 /* If this input section is part of a group of sections sharing one
1120 stub section, then use the id of the first section in the group.
1121 Stub names need to include a section id, as there may well be
1122 more than one stub used to reach say, printf, and we need to
1123 distinguish between them. */
1124 id_sec = htab->stub_group[input_section->id].link_sec;
1126 if (hh != NULL && hh->hsh_cache != NULL
1127 && hh->hsh_cache->hh == hh
1128 && hh->hsh_cache->id_sec == id_sec)
1130 hsh = hh->hsh_cache;
1136 stub_name = metag_stub_name (id_sec, sym_sec, hh, rel);
1137 if (stub_name == NULL)
1140 hsh = metag_stub_hash_lookup (&htab->bstab,
1141 stub_name, FALSE, FALSE);
1144 hh->hsh_cache = hsh;
1152 /* Add a new stub entry to the stub hash. Not all fields of the new
1153 stub entry are initialised. */
1155 static struct elf_metag_stub_hash_entry *
1156 metag_add_stub (const char *stub_name,
1158 struct elf_metag_link_hash_table *htab)
1162 struct elf_metag_stub_hash_entry *hsh;
1164 link_sec = htab->stub_group[section->id].link_sec;
1165 stub_sec = htab->stub_group[section->id].stub_sec;
1166 if (stub_sec == NULL)
1168 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1169 if (stub_sec == NULL)
1175 namelen = strlen (link_sec->name);
1176 len = namelen + sizeof (STUB_SUFFIX);
1177 s_name = bfd_alloc (htab->stub_bfd, len);
1181 memcpy (s_name, link_sec->name, namelen);
1182 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1184 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1185 if (stub_sec == NULL)
1187 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1189 htab->stub_group[section->id].stub_sec = stub_sec;
1192 /* Enter this entry into the linker stub hash table. */
1193 hsh = metag_stub_hash_lookup (&htab->bstab, stub_name,
1197 /* xgettext:c-format */
1198 _bfd_error_handler (_("%B: cannot create stub entry %s"),
1199 section->owner, stub_name);
1203 hsh->stub_sec = stub_sec;
1204 hsh->stub_offset = 0;
1205 hsh->id_sec = link_sec;
1209 /* Check a signed integer value can be represented in the given number
1213 within_signed_range (int value, unsigned int bits)
1215 int min_val = -(1 << (bits - 1));
1216 int max_val = (1 << (bits - 1)) - 1;
1217 return (value <= max_val) && (value >= min_val);
1220 /* Perform a relocation as part of a final link. */
1222 static bfd_reloc_status_type
1223 metag_final_link_relocate (reloc_howto_type *howto,
1225 asection *input_section,
1227 Elf_Internal_Rela *rel,
1229 struct elf_metag_link_hash_entry *hh,
1230 struct elf_metag_link_hash_table *htab,
1233 bfd_reloc_status_type r = bfd_reloc_ok;
1234 bfd_byte *hit_data = contents + rel->r_offset;
1235 int opcode, op_shift, op_extended, l1, l2;
1236 bfd_signed_vma srel, addend = rel->r_addend;
1237 struct elf_metag_stub_hash_entry *hsh = NULL;
1240 /* Find out where we are and where we're going. */
1241 location = (rel->r_offset +
1242 input_section->output_offset +
1243 input_section->output_section->vma);
1245 switch (howto->type)
1247 case R_METAG_RELBRANCH:
1248 case R_METAG_RELBRANCH_PLT:
1249 /* Make it a pc relative offset. */
1250 relocation -= location;
1252 case R_METAG_TLS_GD:
1253 case R_METAG_TLS_IE:
1254 relocation -= elf_gp (input_section->output_section->owner);
1260 switch (howto->type)
1262 case R_METAG_RELBRANCH_PLT:
1263 case R_METAG_RELBRANCH:
1264 opcode = bfd_get_32 (input_bfd, hit_data);
1266 srel = (bfd_signed_vma) relocation;
1269 /* If the branch is out of reach, then redirect the
1270 call to the local stub for this function. */
1271 if (srel > ((1 << (BRANCH_BITS + 1)) - 1) ||
1272 (srel < - (1 << (BRANCH_BITS + 1))))
1274 if (sym_sec == NULL)
1277 hsh = metag_get_stub_entry (input_section, sym_sec,
1280 return bfd_reloc_undefined;
1282 /* Munge up the value and addend so that we call the stub
1283 rather than the procedure directly. */
1284 srel = (hsh->stub_offset
1285 + hsh->stub_sec->output_offset
1286 + hsh->stub_sec->output_section->vma);
1292 if (!within_signed_range (srel, BRANCH_BITS))
1294 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1297 return bfd_reloc_overflow;
1300 opcode &= ~(0x7ffff << 5);
1301 opcode |= ((srel & 0x7ffff) << 5);
1303 bfd_put_32 (input_bfd, opcode, hit_data);
1305 case R_METAG_GETSETOFF:
1306 case R_METAG_GETSET_GOT:
1307 case R_METAG_GETSET_GOTOFF:
1308 opcode = bfd_get_32 (input_bfd, hit_data);
1310 srel = (bfd_signed_vma) relocation;
1313 /* Is this a standard or extended GET/SET? */
1314 if ((opcode & 0xf0000000) == 0xa0000000)
1316 /* Extended GET/SET. */
1323 /* Standard GET/SET. */
1324 l1 = opcode & 0x01000000;
1325 l2 = opcode & 0x04000000;
1329 /* Calculate the width of the GET/SET and how much we need to
1330 shift the result by. */
1342 /* GET/SET offsets are scaled by the width of the transfer. */
1343 srel = srel >> op_shift;
1345 /* Extended GET/SET has signed 12 bits of offset, standard has
1349 if (!within_signed_range (srel, 12))
1351 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1354 return bfd_reloc_overflow;
1356 opcode &= ~(0xfff << 7);
1357 opcode |= ((srel & 0xfff) << 7);
1361 if (!within_signed_range (srel, 5))
1363 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1366 return bfd_reloc_overflow;
1368 opcode &= ~(0x3f << 8);
1369 opcode |= ((srel & 0x3f) << 8);
1372 bfd_put_32 (input_bfd, opcode, hit_data);
1374 case R_METAG_TLS_GD:
1375 case R_METAG_TLS_LDM:
1376 opcode = bfd_get_32 (input_bfd, hit_data);
1378 if ((bfd_signed_vma)relocation < 0)
1380 /* sign extend immediate */
1381 if ((opcode & 0xf2000001) == 0x02000000)
1383 /* ADD De.e,Dx.r,#I16 */
1387 return bfd_reloc_overflow;
1390 bfd_put_32 (input_bfd, opcode, hit_data);
1392 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1393 contents, rel->r_offset,
1394 relocation, rel->r_addend);
1397 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1398 contents, rel->r_offset,
1399 relocation, rel->r_addend);
1405 /* This is defined because R_METAG_NONE != 0...
1406 See RELOC_AGAINST_DISCARDED_SECTION for details. */
1407 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1408 rel, relend, howto, contents) \
1410 _bfd_clear_contents (howto, input_bfd, input_section, \
1411 contents + rel->r_offset); \
1413 if (bfd_link_relocatable (info) \
1414 && (input_section->flags & SEC_DEBUGGING)) \
1416 /* Only remove relocations in debug sections since other \
1417 sections may require relocations. */ \
1418 Elf_Internal_Shdr *rel_hdr; \
1420 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1422 /* Avoid empty output section. */ \
1423 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
1425 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1426 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
1427 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1429 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
1431 input_section->reloc_count--; \
1438 rel->r_info = R_METAG_NONE; \
1439 rel->r_addend = 0; \
1443 /* Relocate a META ELF section.
1445 The RELOCATE_SECTION function is called by the new ELF backend linker
1446 to handle the relocations for a section.
1448 The relocs are always passed as Rela structures; if the section
1449 actually uses Rel structures, the r_addend field will always be
1452 This function is responsible for adjusting the section contents as
1453 necessary, and (if using Rela relocs and generating a relocatable
1454 output file) adjusting the reloc addend as necessary.
1456 This function does not have to worry about setting the reloc
1457 address or the reloc symbol index.
1459 LOCAL_SYMS is a pointer to the swapped in local symbols.
1461 LOCAL_SECTIONS is an array giving the section in the input file
1462 corresponding to the st_shndx field of each local symbol.
1464 The global hash table entry for the global symbols can be found
1465 via elf_sym_hashes (input_bfd).
1467 When generating relocatable output, this function must handle
1468 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1469 going to be the section symbol corresponding to the output
1470 section, which means that the addend must be adjusted
1474 elf_metag_relocate_section (bfd *output_bfd,
1475 struct bfd_link_info *info,
1477 asection *input_section,
1479 Elf_Internal_Rela *relocs,
1480 Elf_Internal_Sym *local_syms,
1481 asection **local_sections)
1483 bfd_vma *local_got_offsets;
1484 Elf_Internal_Shdr *symtab_hdr;
1485 struct elf_link_hash_entry **eh_syms;
1486 struct elf_metag_link_hash_table *htab;
1487 Elf_Internal_Rela *rel;
1488 Elf_Internal_Rela *relend;
1491 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1492 eh_syms = elf_sym_hashes (input_bfd);
1493 relend = relocs + input_section->reloc_count;
1495 htab = metag_link_hash_table (info);
1496 local_got_offsets = elf_local_got_offsets (input_bfd);
1500 for (rel = relocs; rel < relend; rel ++)
1502 reloc_howto_type *howto;
1503 unsigned long r_symndx;
1504 Elf_Internal_Sym *sym;
1506 struct elf_metag_link_hash_entry *hh;
1508 bfd_reloc_status_type r;
1512 r_type = ELF32_R_TYPE (rel->r_info);
1514 if (r_type == R_METAG_GNU_VTINHERIT
1515 || r_type == R_METAG_GNU_VTENTRY
1516 || r_type == R_METAG_NONE)
1519 r_symndx = ELF32_R_SYM (rel->r_info);
1521 howto = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info);
1526 if (r_symndx < symtab_hdr->sh_info)
1528 sym = local_syms + r_symndx;
1529 sec = local_sections [r_symndx];
1530 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1532 name = bfd_elf_string_from_elf_section
1533 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1534 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1538 struct elf_link_hash_entry *eh;
1539 bfd_boolean unresolved_reloc, warned, ignored;
1541 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1542 r_symndx, symtab_hdr, eh_syms,
1543 eh, sec, relocation,
1544 unresolved_reloc, warned, ignored);
1546 name = eh->root.root.string;
1547 hh = (struct elf_metag_link_hash_entry *) eh;
1550 if (sec != NULL && discarded_section (sec))
1551 METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1552 rel, relend, howto, contents);
1554 if (bfd_link_relocatable (info))
1559 case R_METAG_ADDR32:
1560 case R_METAG_RELBRANCH:
1561 if ((input_section->flags & SEC_ALLOC) == 0)
1564 if ((bfd_link_pic (info)
1565 && r_symndx != STN_UNDEF
1566 && (input_section->flags & SEC_ALLOC) != 0
1567 && (r_type != R_METAG_RELBRANCH
1568 || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
1569 || (!bfd_link_pic (info)
1571 && hh->eh.dynindx != -1
1572 && !hh->eh.non_got_ref
1573 && ((hh->eh.def_dynamic
1574 && !hh->eh.def_regular)
1575 || hh->eh.root.type == bfd_link_hash_undefweak
1576 || hh->eh.root.type == bfd_link_hash_undefined)))
1578 Elf_Internal_Rela outrel;
1579 bfd_boolean skip, relocate;
1582 /* When generating a shared object, these relocations
1583 are copied into the output file to be resolved at run
1586 sreloc = elf_section_data (input_section)->sreloc;
1587 BFD_ASSERT (sreloc != NULL);
1592 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1596 if (outrel.r_offset == (bfd_vma) -1)
1598 else if (outrel.r_offset == (bfd_vma) -2)
1599 skip = TRUE, relocate = TRUE;
1600 outrel.r_offset += (input_section->output_section->vma
1601 + input_section->output_offset);
1605 memset (&outrel, 0, sizeof outrel);
1606 outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE);
1608 else if (r_type == R_METAG_RELBRANCH)
1610 BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1);
1611 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1612 outrel.r_addend = rel->r_addend;
1616 /* h->dynindx may be -1 if this symbol was marked to
1619 || ((info->symbolic || hh->eh.dynindx == -1)
1620 && hh->eh.def_regular))
1623 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1624 outrel.r_addend = relocation + rel->r_addend;
1628 BFD_ASSERT (hh->eh.dynindx != -1);
1629 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1630 outrel.r_addend = rel->r_addend;
1634 loc = sreloc->contents;
1635 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
1636 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
1637 ++sreloc->reloc_count;
1639 /* If this reloc is against an external symbol, we do
1640 not want to fiddle with the addend. Otherwise, we
1641 need to include the symbol value so that it becomes
1642 an addend for the dynamic reloc. */
1648 case R_METAG_RELBRANCH_PLT:
1649 /* Relocation is to the entry for this symbol in the
1650 procedure linkage table. */
1655 if (hh->eh.forced_local)
1658 if (hh->eh.plt.offset == (bfd_vma) -1 || htab->etab.splt == NULL)
1660 /* We didn't make a PLT entry for this symbol. This
1661 happens when statically linking PIC code, or when
1662 using -Bsymbolic. */
1666 relocation = (htab->etab.splt->output_section->vma
1667 + htab->etab.splt->output_offset
1668 + hh->eh.plt.offset);
1670 case R_METAG_HI16_GOTPC:
1671 case R_METAG_LO16_GOTPC:
1672 BFD_ASSERT (htab->etab.sgot != NULL);
1674 relocation = (htab->etab.sgot->output_section->vma +
1675 htab->etab.sgot->output_offset);
1676 relocation += GOT_REG_OFFSET;
1677 relocation -= (input_section->output_section->vma
1678 + input_section->output_offset
1681 case R_METAG_HI16_GOTOFF:
1682 case R_METAG_LO16_GOTOFF:
1683 case R_METAG_GETSET_GOTOFF:
1684 BFD_ASSERT (htab->etab.sgot != NULL);
1686 relocation -= (htab->etab.sgot->output_section->vma +
1687 htab->etab.sgot->output_offset);
1688 relocation -= GOT_REG_OFFSET;
1690 case R_METAG_GETSET_GOT:
1693 bfd_boolean do_got = 0;
1695 /* Relocation is to the entry for this symbol in the
1696 global offset table. */
1701 off = hh->eh.got.offset;
1702 dyn = htab->etab.dynamic_sections_created;
1703 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1704 bfd_link_pic (info),
1707 /* If we aren't going to call finish_dynamic_symbol,
1708 then we need to handle initialisation of the .got
1709 entry and create needed relocs here. Since the
1710 offset must always be a multiple of 4, we use the
1711 least significant bit to record whether we have
1712 initialised it already. */
1717 hh->eh.got.offset |= 1;
1724 /* Local symbol case. */
1725 if (local_got_offsets == NULL)
1728 off = local_got_offsets[r_symndx];
1730 /* The offset must always be a multiple of 4. We use
1731 the least significant bit to record whether we have
1732 already generated the necessary reloc. */
1737 local_got_offsets[r_symndx] |= 1;
1744 if (bfd_link_pic (info))
1746 /* Output a dynamic relocation for this GOT entry.
1747 In this case it is relative to the base of the
1748 object because the symbol index is zero. */
1749 Elf_Internal_Rela outrel;
1751 asection *s = htab->etab.srelgot;
1753 outrel.r_offset = (off
1754 + htab->etab.sgot->output_offset
1755 + htab->etab.sgot->output_section->vma);
1756 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1757 outrel.r_addend = relocation;
1759 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1760 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1763 bfd_put_32 (output_bfd, relocation,
1764 htab->etab.sgot->contents + off);
1767 if (off >= (bfd_vma) -2)
1770 relocation = off - GOT_REG_OFFSET;
1773 case R_METAG_TLS_GD:
1774 case R_METAG_TLS_IE:
1776 /* XXXMJF There is room here for optimisations. For example
1777 converting from GD->IE, etc. */
1782 if (htab->etab.sgot == NULL)
1789 dyn = htab->etab.dynamic_sections_created;
1791 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1792 bfd_link_pic (info),
1794 && (!bfd_link_pic (info)
1795 || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
1797 indx = hh->eh.dynindx;
1799 off = hh->eh.got.offset;
1800 tls_type = hh->tls_type;
1804 /* Local symbol case. */
1805 if (local_got_offsets == NULL)
1808 off = local_got_offsets[r_symndx];
1809 tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx];
1812 if (tls_type == GOT_UNKNOWN)
1819 bfd_boolean need_relocs = FALSE;
1820 Elf_Internal_Rela outrel;
1821 bfd_byte *loc = NULL;
1824 /* The GOT entries have not been initialized yet. Do it
1825 now, and emit any relocations. If both an IE GOT and a
1826 GD GOT are necessary, we emit the GD first. */
1828 if ((bfd_link_pic (info) || indx != 0)
1830 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
1831 || hh->eh.root.type != bfd_link_hash_undefweak))
1834 loc = htab->etab.srelgot->contents;
1835 /* FIXME (CAO): Should this be reloc_count++ ? */
1836 loc += htab->etab.srelgot->reloc_count * sizeof (Elf32_External_Rela);
1839 if (tls_type & GOT_TLS_GD)
1843 outrel.r_offset = (cur_off
1844 + htab->etab.sgot->output_section->vma
1845 + htab->etab.sgot->output_offset);
1846 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD);
1847 outrel.r_addend = 0;
1848 bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + cur_off);
1850 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1851 htab->etab.srelgot->reloc_count++;
1852 loc += sizeof (Elf32_External_Rela);
1855 bfd_put_32 (output_bfd, 0,
1856 htab->etab.sgot->contents + cur_off + 4);
1859 bfd_put_32 (output_bfd, 0,
1860 htab->etab.sgot->contents + cur_off + 4);
1861 outrel.r_info = ELF32_R_INFO (indx,
1862 R_METAG_TLS_DTPOFF);
1863 outrel.r_offset += 4;
1864 bfd_elf32_swap_reloca_out (output_bfd,
1866 htab->etab.srelgot->reloc_count++;
1867 loc += sizeof (Elf32_External_Rela);
1872 /* We don't support changing the TLS model. */
1874 if (bfd_link_pic (info))
1875 _bfd_error_handler (_("%B(%A): multiple TLS models are not supported"),
1876 input_bfd, input_section);
1878 _bfd_error_handler (_("%B(%A): shared library symbol %s encountered whilst performing a static link"),
1879 input_bfd, input_section, name);
1886 if (tls_type & GOT_TLS_IE)
1890 outrel.r_offset = (cur_off
1891 + htab->etab.sgot->output_section->vma
1892 + htab->etab.sgot->output_offset);
1893 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF);
1896 outrel.r_addend = relocation - dtpoff_base (info);
1898 outrel.r_addend = 0;
1900 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1901 htab->etab.srelgot->reloc_count++;
1902 loc += sizeof (Elf32_External_Rela);
1905 bfd_put_32 (output_bfd, tpoff (info, relocation),
1906 htab->etab.sgot->contents + cur_off);
1912 hh->eh.got.offset |= 1;
1914 local_got_offsets[r_symndx] |= 1;
1917 /* Add the base of the GOT to the relocation value. */
1918 relocation = off - GOT_REG_OFFSET;
1923 case R_METAG_TLS_IENONPIC_HI16:
1924 case R_METAG_TLS_IENONPIC_LO16:
1925 case R_METAG_TLS_LE_HI16:
1926 case R_METAG_TLS_LE_LO16:
1927 if (bfd_link_pic (info))
1930 /* xgettext:c-format */
1931 (_("%B(%A+%#Lx): %s relocation not permitted in shared object"),
1932 input_bfd, input_section, rel->r_offset, howto->name);
1936 relocation = tpoff (info, relocation);
1938 case R_METAG_TLS_LDO_HI16:
1939 case R_METAG_TLS_LDO_LO16:
1940 if (! bfd_link_pic (info))
1941 relocation = tpoff (info, relocation);
1943 relocation -= dtpoff_base (info);
1945 case R_METAG_TLS_LDM:
1949 if (htab->etab.sgot == NULL)
1951 off = htab->tls_ldm_got.offset;
1956 Elf_Internal_Rela outrel;
1959 outrel.r_offset = (off
1960 + htab->etab.sgot->output_section->vma
1961 + htab->etab.sgot->output_offset);
1963 outrel.r_addend = 0;
1964 outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD);
1965 loc = htab->etab.srelgot->contents;
1966 loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1967 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1968 htab->tls_ldm_got.offset |= 1;
1971 relocation = off - GOT_REG_OFFSET;
1978 r = metag_final_link_relocate (howto, input_bfd, input_section,
1979 contents, rel, relocation, hh, htab,
1982 if (r != bfd_reloc_ok)
1984 const char * msg = (const char *) NULL;
1988 case bfd_reloc_overflow:
1989 (*info->callbacks->reloc_overflow)
1990 (info, (hh ? &hh->eh.root : NULL), name, howto->name,
1991 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1994 case bfd_reloc_undefined:
1995 (*info->callbacks->undefined_symbol)
1996 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1999 case bfd_reloc_outofrange:
2000 msg = _("internal error: out of range error");
2003 case bfd_reloc_notsupported:
2004 msg = _("internal error: unsupported relocation error");
2007 case bfd_reloc_dangerous:
2008 msg = _("internal error: dangerous relocation");
2012 msg = _("internal error: unknown error");
2017 (*info->callbacks->warning) (info, msg, name, input_bfd,
2018 input_section, rel->r_offset);
2025 /* Create the .plt and .got sections, and set up our hash table
2026 short-cuts to various dynamic sections. */
2029 elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2031 struct elf_metag_link_hash_table *htab;
2032 struct elf_link_hash_entry *eh;
2033 struct bfd_link_hash_entry *bh;
2034 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2036 /* Don't try to create the .plt and .got twice. */
2037 htab = metag_link_hash_table (info);
2038 if (htab->etab.splt != NULL)
2041 /* Call the generic code to do most of the work. */
2042 if (! _bfd_elf_create_dynamic_sections (abfd, info))
2045 /* The header goes at the start of the dynamic .got section, which
2046 is placed after the dynamic .got.plt section. ie. The header is
2047 not necessarily at the start of the output .got section. */
2048 htab->etab.sgot->size += 12;
2050 /* Define the symbol __GLOBAL_OFFSET_TABLE__ on the header. */
2052 if (!(_bfd_generic_link_add_one_symbol
2053 (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->etab.sgot,
2054 (bfd_vma) 0, NULL, FALSE, bed->collect, &bh)))
2056 eh = (struct elf_link_hash_entry *) bh;
2057 eh->def_regular = 1;
2058 eh->type = STT_OBJECT;
2059 eh->other = STV_HIDDEN;
2061 if (! bfd_link_executable (info)
2062 && ! bfd_elf_link_record_dynamic_symbol (info, eh))
2065 htab->etab.hgot = eh;
2070 /* Look through the relocs for a section during the first phase, and
2071 calculate needed space in the global offset table, procedure linkage
2072 table, and dynamic reloc sections. At this point we haven't
2073 necessarily read all the input files. */
2076 elf_metag_check_relocs (bfd *abfd,
2077 struct bfd_link_info *info,
2079 const Elf_Internal_Rela *relocs)
2081 Elf_Internal_Shdr *symtab_hdr;
2082 struct elf_link_hash_entry **eh_syms;
2083 const Elf_Internal_Rela *rel;
2084 const Elf_Internal_Rela *rel_end;
2085 struct elf_metag_link_hash_table *htab;
2088 int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
2090 if (bfd_link_relocatable (info))
2093 htab = metag_link_hash_table (info);
2094 dynobj = htab->etab.dynobj;
2095 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2096 eh_syms = elf_sym_hashes (abfd);
2102 rel_end = relocs + sec->reloc_count;
2103 for (rel = relocs; rel < rel_end; rel++)
2106 struct elf_metag_link_hash_entry *hh;
2107 Elf_Internal_Sym *isym;
2108 unsigned long r_symndx;
2110 r_symndx = ELF32_R_SYM (rel->r_info);
2111 r_type = ELF32_R_TYPE (rel->r_info);
2112 if (r_symndx < symtab_hdr->sh_info)
2114 /* A local symbol. */
2115 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2126 hh = (struct elf_metag_link_hash_entry *)
2127 eh_syms[r_symndx - symtab_hdr->sh_info];
2128 while (hh->eh.root.type == bfd_link_hash_indirect
2129 || hh->eh.root.type == bfd_link_hash_warning)
2130 hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link;
2133 /* Some relocs require a global offset table. */
2134 if (htab->etab.sgot == NULL)
2138 case R_METAG_TLS_GD:
2139 case R_METAG_TLS_LDM:
2140 case R_METAG_TLS_IE:
2141 if (bfd_link_pic (info))
2142 info->flags |= DF_STATIC_TLS;
2145 case R_METAG_HI16_GOTOFF:
2146 case R_METAG_LO16_GOTOFF:
2147 case R_METAG_GETSET_GOTOFF:
2148 case R_METAG_GETSET_GOT:
2149 case R_METAG_HI16_GOTPC:
2150 case R_METAG_LO16_GOTPC:
2152 htab->etab.dynobj = dynobj = abfd;
2153 if (!elf_metag_create_dynamic_sections (dynobj, info))
2164 case R_METAG_TLS_IE:
2165 case R_METAG_TLS_GD:
2166 case R_METAG_GETSET_GOT:
2170 tls_type = GOT_NORMAL;
2172 case R_METAG_TLS_IE:
2173 tls_type = GOT_TLS_IE;
2175 case R_METAG_TLS_GD:
2176 tls_type = GOT_TLS_GD;
2182 hh->eh.got.refcount += 1;
2183 old_tls_type = hh->tls_type;
2187 bfd_signed_vma *local_got_refcounts;
2189 /* This is a global offset table entry for a local
2191 local_got_refcounts = elf_local_got_refcounts (abfd);
2192 if (local_got_refcounts == NULL)
2196 size = symtab_hdr->sh_info;
2197 size *= sizeof (bfd_signed_vma);
2198 /* Add in space to store the local GOT TLS types. */
2199 size += symtab_hdr->sh_info;
2200 local_got_refcounts = ((bfd_signed_vma *)
2201 bfd_zalloc (abfd, size));
2202 if (local_got_refcounts == NULL)
2204 elf_local_got_refcounts (abfd) = local_got_refcounts;
2205 memset (metag_elf_local_got_tls_type (abfd),
2206 GOT_UNKNOWN, symtab_hdr->sh_info);
2208 local_got_refcounts[r_symndx] += 1;
2209 old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx];
2212 if (old_tls_type != tls_type)
2216 hh->tls_type = tls_type;
2220 metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
2226 case R_METAG_TLS_LDM:
2227 metag_link_hash_table (info)->tls_ldm_got.refcount += 1;
2230 case R_METAG_RELBRANCH_PLT:
2231 /* This symbol requires a procedure linkage table entry. We
2232 actually build the entry in adjust_dynamic_symbol,
2233 because this might be a case of linking PIC code without
2234 linking in any dynamic objects, in which case we don't
2235 need to generate a procedure linkage table after all. */
2237 /* If this is a local symbol, we resolve it directly without
2238 creating a procedure linkage table entry. */
2242 if (hh->eh.forced_local)
2245 hh->eh.needs_plt = 1;
2246 hh->eh.plt.refcount += 1;
2249 case R_METAG_HIADDR16:
2250 case R_METAG_LOADDR16:
2251 /* Let's help debug shared library creation. These relocs
2252 cannot be used in shared libs. Don't error out for
2253 sections we don't care about, such as debug sections or
2254 non-constant sections. */
2255 if (bfd_link_pic (info)
2256 && (sec->flags & SEC_ALLOC) != 0
2257 && (sec->flags & SEC_READONLY) != 0)
2262 name = hh->eh.root.root.string;
2264 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
2266 /* xgettext:c-format */
2267 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2268 abfd, elf_metag_howto_table[r_type].name, name);
2269 bfd_set_error (bfd_error_bad_value);
2274 case R_METAG_ADDR32:
2275 case R_METAG_RELBRANCH:
2276 case R_METAG_GETSETOFF:
2277 if (hh != NULL && !bfd_link_pic (info))
2279 hh->eh.non_got_ref = 1;
2280 hh->eh.plt.refcount += 1;
2283 /* If we are creating a shared library, and this is a reloc
2284 against a global symbol, or a non PC relative reloc
2285 against a local symbol, then we need to copy the reloc
2286 into the shared library. However, if we are linking with
2287 -Bsymbolic, we do not need to copy a reloc against a
2288 global symbol which is defined in an object we are
2289 including in the link (i.e., DEF_REGULAR is set). At
2290 this point we have not seen all the input files, so it is
2291 possible that DEF_REGULAR is not set now but will be set
2292 later (it is never cleared). We account for that
2293 possibility below by storing information in the
2294 dyn_relocs field of the hash table entry. A similar
2295 situation occurs when creating shared libraries and symbol
2296 visibility changes render the symbol local.
2298 If on the other hand, we are creating an executable, we
2299 may need to keep relocations for symbols satisfied by a
2300 dynamic library if we manage to avoid copy relocs for the
2302 if ((bfd_link_pic (info)
2303 && (sec->flags & SEC_ALLOC) != 0
2304 && (r_type != R_METAG_RELBRANCH
2306 && (! info->symbolic
2307 || hh->eh.root.type == bfd_link_hash_defweak
2308 || !hh->eh.def_regular))))
2309 || (!bfd_link_pic (info)
2310 && (sec->flags & SEC_ALLOC) != 0
2312 && (hh->eh.root.type == bfd_link_hash_defweak
2313 || !hh->eh.def_regular)))
2315 struct elf_metag_dyn_reloc_entry *hdh_p;
2316 struct elf_metag_dyn_reloc_entry **hdh_head;
2319 htab->etab.dynobj = dynobj = abfd;
2321 /* When creating a shared object, we must copy these
2322 relocs into the output file. We create a reloc
2323 section in dynobj and make room for the reloc. */
2326 sreloc = _bfd_elf_make_dynamic_reloc_section
2327 (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE);
2331 bfd_set_error (bfd_error_bad_value);
2335 elf_section_data (sec)->sreloc = sreloc;
2338 /* If this is a global symbol, we count the number of
2339 relocations we need for this symbol. */
2341 hdh_head = &((struct elf_metag_link_hash_entry *) hh)->dyn_relocs;
2344 /* Track dynamic relocs needed for local syms too. */
2348 sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
2352 vpp = &elf_section_data (sr)->local_dynrel;
2353 hdh_head = (struct elf_metag_dyn_reloc_entry **) vpp;
2357 if (hdh_p == NULL || hdh_p->sec != sec)
2359 hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2360 bfd_alloc (dynobj, sizeof *hdh_p));
2363 hdh_p->hdh_next = *hdh_head;
2367 hdh_p->relative_count = 0;
2371 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
2372 hdh_p->relative_count += 1;
2376 /* This relocation describes the C++ object vtable hierarchy.
2377 Reconstruct it for later use during GC. */
2378 case R_METAG_GNU_VTINHERIT:
2379 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh,
2384 /* This relocation describes which C++ vtable entries are actually
2385 used. Record for later use during GC. */
2386 case R_METAG_GNU_VTENTRY:
2387 BFD_ASSERT (hh != NULL);
2389 && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend))
2398 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2401 elf_metag_copy_indirect_symbol (struct bfd_link_info *info,
2402 struct elf_link_hash_entry *eh_dir,
2403 struct elf_link_hash_entry *eh_ind)
2405 struct elf_metag_link_hash_entry *hh_dir, *hh_ind;
2407 hh_dir = metag_elf_hash_entry (eh_dir);
2408 hh_ind = metag_elf_hash_entry (eh_ind);
2410 if (hh_ind->dyn_relocs != NULL)
2412 if (hh_dir->dyn_relocs != NULL)
2414 struct elf_metag_dyn_reloc_entry **hdh_pp;
2415 struct elf_metag_dyn_reloc_entry *hdh_p;
2417 if (eh_ind->root.type == bfd_link_hash_indirect)
2420 /* Add reloc counts against the weak sym to the strong sym
2421 list. Merge any entries against the same section. */
2422 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2424 struct elf_metag_dyn_reloc_entry *hdh_q;
2426 for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL;
2427 hdh_q = hdh_q->hdh_next)
2428 if (hdh_q->sec == hdh_p->sec)
2430 hdh_q->relative_count += hdh_p->relative_count;
2431 hdh_q->count += hdh_p->count;
2432 *hdh_pp = hdh_p->hdh_next;
2436 hdh_pp = &hdh_p->hdh_next;
2438 *hdh_pp = hh_dir->dyn_relocs;
2441 hh_dir->dyn_relocs = hh_ind->dyn_relocs;
2442 hh_ind->dyn_relocs = NULL;
2445 if (eh_ind->root.type == bfd_link_hash_indirect
2446 && eh_dir->got.refcount <= 0)
2448 hh_dir->tls_type = hh_ind->tls_type;
2449 hh_ind->tls_type = GOT_UNKNOWN;
2452 _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
2455 /* Find dynamic relocs for H that apply to read-only sections. */
2458 readonly_dynrelocs (struct elf_link_hash_entry *h)
2460 struct elf_metag_dyn_reloc_entry *p;
2462 for (p = metag_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->hdh_next)
2464 asection *s = p->sec->output_section;
2466 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2472 /* Adjust a symbol defined by a dynamic object and referenced by a
2473 regular object. The current definition is in some section of the
2474 dynamic object, but we're not including those sections. We have to
2475 change the definition to something the rest of the link can
2479 elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info,
2480 struct elf_link_hash_entry *eh)
2482 struct elf_metag_link_hash_table *htab;
2483 struct elf_metag_link_hash_entry *hh;
2484 struct elf_metag_dyn_reloc_entry *hdh_p;
2487 /* If this is a function, put it in the procedure linkage table. We
2488 will fill in the contents of the procedure linkage table later,
2489 when we know the address of the .got section. */
2490 if (eh->type == STT_FUNC
2493 if (eh->plt.refcount <= 0
2494 || SYMBOL_CALLS_LOCAL (info, eh)
2495 || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
2496 && eh->root.type == bfd_link_hash_undefweak))
2498 /* This case can occur if we saw a PLT reloc in an input
2499 file, but the symbol was never referred to by a dynamic
2500 object. In such a case, we don't actually need to build
2501 a procedure linkage table, and we can just do a PCREL
2503 eh->plt.offset = (bfd_vma) -1;
2510 eh->plt.offset = (bfd_vma) -1;
2512 /* If this is a weak symbol, and there is a real definition, the
2513 processor independent code will have arranged for us to see the
2514 real definition first, and we can just use the same value. */
2515 if (eh->is_weakalias)
2517 struct elf_link_hash_entry *def = weakdef (eh);
2518 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2519 eh->root.u.def.section = def->root.u.def.section;
2520 eh->root.u.def.value = def->root.u.def.value;
2521 eh->non_got_ref = def->non_got_ref;
2525 /* This is a reference to a symbol defined by a dynamic object which
2526 is not a function. */
2528 /* If we are creating a shared library, we must presume that the
2529 only references to the symbol are via the global offset table.
2530 For such cases we need not do anything here; the relocations will
2531 be handled correctly by relocate_section. */
2532 if (bfd_link_pic (info))
2535 /* If there are no references to this symbol that do not use the
2536 GOT, we don't need to generate a copy reloc. */
2537 if (!eh->non_got_ref)
2540 /* If -z nocopyreloc was given, we won't generate them either. */
2541 if (info->nocopyreloc)
2543 eh->non_got_ref = 0;
2547 hh = (struct elf_metag_link_hash_entry *) eh;
2548 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2550 s = hdh_p->sec->output_section;
2551 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2555 /* If we didn't find any dynamic relocs in read-only sections, then
2556 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2559 eh->non_got_ref = 0;
2563 /* We must allocate the symbol in our .dynbss section, which will
2564 become part of the .bss section of the executable. There will be
2565 an entry for this symbol in the .dynsym section. The dynamic
2566 object will contain position independent code, so all references
2567 from the dynamic object to this symbol will go through the global
2568 offset table. The dynamic linker will use the .dynsym entry to
2569 determine the address it must put in the global offset table, so
2570 both the dynamic object and the regular object will refer to the
2571 same memory location for the variable. */
2573 htab = metag_link_hash_table (info);
2575 /* We must generate a COPY reloc to tell the dynamic linker to
2576 copy the initial value out of the dynamic object and into the
2577 runtime process image. */
2578 if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
2580 s = htab->etab.sdynrelro;
2581 srel = htab->etab.sreldynrelro;
2585 s = htab->etab.sdynbss;
2586 srel = htab->etab.srelbss;
2588 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
2590 srel->size += sizeof (Elf32_External_Rela);
2594 return _bfd_elf_adjust_dynamic_copy (info, eh, s);
2597 /* Allocate space in .plt, .got and associated reloc sections for
2601 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2603 struct bfd_link_info *info;
2604 struct elf_metag_link_hash_table *htab;
2605 struct elf_metag_link_hash_entry *hh;
2606 struct elf_metag_dyn_reloc_entry *hdh_p;
2608 if (eh->root.type == bfd_link_hash_indirect)
2611 if (eh->root.type == bfd_link_hash_warning)
2612 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2615 htab = metag_link_hash_table (info);
2617 if (htab->etab.dynamic_sections_created
2618 && eh->plt.refcount > 0)
2620 /* Make sure this symbol is output as a dynamic symbol.
2621 Undefined weak syms won't yet be marked as dynamic. */
2622 if (eh->dynindx == -1
2623 && !eh->forced_local)
2625 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2629 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
2631 asection *s = htab->etab.splt;
2633 /* If this is the first .plt entry, make room for the special
2636 s->size += PLT_ENTRY_SIZE;
2638 eh->plt.offset = s->size;
2640 /* If this symbol is not defined in a regular file, and we are
2641 not generating a shared library, then set the symbol to this
2642 location in the .plt. This is required to make function
2643 pointers compare as equal between the normal executable and
2644 the shared library. */
2645 if (! bfd_link_pic (info)
2646 && !eh->def_regular)
2648 eh->root.u.def.section = s;
2649 eh->root.u.def.value = eh->plt.offset;
2652 /* Make room for this entry. */
2653 s->size += PLT_ENTRY_SIZE;
2655 /* We also need to make an entry in the .got.plt section, which
2656 will be placed in the .got section by the linker script. */
2657 htab->etab.sgotplt->size += 4;
2659 /* We also need to make an entry in the .rel.plt section. */
2660 htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
2664 eh->plt.offset = (bfd_vma) -1;
2670 eh->plt.offset = (bfd_vma) -1;
2674 if (eh->got.refcount > 0)
2678 int tls_type = metag_elf_hash_entry (eh)->tls_type;
2680 /* Make sure this symbol is output as a dynamic symbol.
2681 Undefined weak syms won't yet be marked as dynamic. */
2682 if (eh->dynindx == -1
2683 && !eh->forced_local)
2685 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2689 s = htab->etab.sgot;
2691 eh->got.offset = s->size;
2693 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
2694 if (tls_type == GOT_TLS_GD)
2696 dyn = htab->etab.dynamic_sections_created;
2697 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2698 R_METAG_TLS_GD needs one if local symbol and two if global. */
2699 if ((tls_type == GOT_TLS_GD && eh->dynindx == -1)
2700 || (tls_type == GOT_TLS_IE && dyn))
2701 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2702 else if (tls_type == GOT_TLS_GD)
2703 htab->etab.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2704 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2705 bfd_link_pic (info),
2707 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2710 eh->got.offset = (bfd_vma) -1;
2712 hh = (struct elf_metag_link_hash_entry *) eh;
2713 if (hh->dyn_relocs == NULL)
2716 /* If this is a -Bsymbolic shared link, then we need to discard all
2717 space allocated for dynamic pc-relative relocs against symbols
2718 defined in a regular object. For the normal shared case, discard
2719 space for relocs that have become local due to symbol visibility
2721 if (bfd_link_pic (info))
2723 if (SYMBOL_CALLS_LOCAL (info, eh))
2725 struct elf_metag_dyn_reloc_entry **hdh_pp;
2727 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2729 hdh_p->count -= hdh_p->relative_count;
2730 hdh_p->relative_count = 0;
2731 if (hdh_p->count == 0)
2732 *hdh_pp = hdh_p->hdh_next;
2734 hdh_pp = &hdh_p->hdh_next;
2738 /* Also discard relocs on undefined weak syms with non-default
2740 if (hh->dyn_relocs != NULL
2741 && eh->root.type == bfd_link_hash_undefweak)
2743 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2744 hh->dyn_relocs = NULL;
2746 /* Make sure undefined weak symbols are output as a dynamic
2748 else if (eh->dynindx == -1
2749 && !eh->forced_local)
2751 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2758 /* For the non-shared case, discard space for relocs against
2759 symbols which turn out to need copy relocs or are not
2761 if (!eh->non_got_ref
2762 && ((eh->def_dynamic
2763 && !eh->def_regular)
2764 || (htab->etab.dynamic_sections_created
2765 && (eh->root.type == bfd_link_hash_undefweak
2766 || eh->root.type == bfd_link_hash_undefined))))
2768 /* Make sure this symbol is output as a dynamic symbol.
2769 Undefined weak syms won't yet be marked as dynamic. */
2770 if (eh->dynindx == -1
2771 && !eh->forced_local)
2773 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2777 /* If that succeeded, we know we'll be keeping all the
2779 if (eh->dynindx != -1)
2783 hh->dyn_relocs = NULL;
2789 /* Finally, allocate space. */
2790 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2792 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2793 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2799 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2800 read-only sections. */
2803 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2807 if (h->root.type == bfd_link_hash_indirect)
2810 sec = readonly_dynrelocs (h);
2813 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2815 info->flags |= DF_TEXTREL;
2816 info->callbacks->minfo
2817 (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
2818 sec->owner, h->root.root.string, sec);
2820 /* Not an error, just cut short the traversal. */
2826 /* Set the sizes of the dynamic sections. */
2829 elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2830 struct bfd_link_info *info)
2832 struct elf_metag_link_hash_table *htab;
2838 htab = metag_link_hash_table (info);
2839 dynobj = htab->etab.dynobj;
2843 if (htab->etab.dynamic_sections_created)
2845 /* Set the contents of the .interp section to the interpreter. */
2846 if (bfd_link_executable (info) && !info->nointerp)
2848 s = bfd_get_linker_section (dynobj, ".interp");
2851 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2852 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2856 /* Set up .got offsets for local syms, and space for local dynamic
2858 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2860 bfd_signed_vma *local_got;
2861 bfd_signed_vma *end_local_got;
2862 bfd_size_type locsymcount;
2863 Elf_Internal_Shdr *symtab_hdr;
2865 char *local_tls_type;
2867 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2870 for (s = ibfd->sections; s != NULL; s = s->next)
2872 struct elf_metag_dyn_reloc_entry *hdh_p;
2874 for (hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2875 elf_section_data (s)->local_dynrel);
2877 hdh_p = hdh_p->hdh_next)
2879 if (!bfd_is_abs_section (hdh_p->sec)
2880 && bfd_is_abs_section (hdh_p->sec->output_section))
2882 /* Input section has been discarded, either because
2883 it is a copy of a linkonce section or due to
2884 linker script /DISCARD/, so we'll be discarding
2887 else if (hdh_p->count != 0)
2889 srel = elf_section_data (hdh_p->sec)->sreloc;
2890 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2891 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2892 info->flags |= DF_TEXTREL;
2897 local_got = elf_local_got_refcounts (ibfd);
2901 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2902 locsymcount = symtab_hdr->sh_info;
2903 end_local_got = local_got + locsymcount;
2904 local_tls_type = metag_elf_local_got_tls_type (ibfd);
2905 s = htab->etab.sgot;
2906 srel = htab->etab.srelgot;
2907 for (; local_got < end_local_got; ++local_got)
2911 *local_got = s->size;
2912 s->size += GOT_ENTRY_SIZE;
2913 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
2914 if (*local_tls_type == GOT_TLS_GD)
2916 if (bfd_link_pic (info))
2917 srel->size += sizeof (Elf32_External_Rela);
2920 *local_got = (bfd_vma) -1;
2925 if (htab->tls_ldm_got.refcount > 0)
2927 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2929 htab->tls_ldm_got.offset = htab->etab.sgot->size;
2930 htab->etab.sgot->size += 8;
2931 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2934 htab->tls_ldm_got.offset = -1;
2936 /* Allocate global sym .plt and .got entries, and space for global
2937 sym dynamic relocs. */
2938 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2940 /* We now have determined the sizes of the various dynamic sections.
2941 Allocate memory for them. */
2943 for (s = dynobj->sections; s != NULL; s = s->next)
2945 bfd_boolean reloc_section = FALSE;
2947 if ((s->flags & SEC_LINKER_CREATED) == 0)
2950 if (s == htab->etab.splt
2951 || s == htab->etab.sgot
2952 || s == htab->etab.sgotplt
2953 || s == htab->etab.sdynbss
2954 || s == htab->etab.sdynrelro)
2956 /* Strip this section if we don't need it; see the
2959 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2961 if (s->size != 0 && s != htab->etab.srelplt)
2964 /* We use the reloc_count field as a counter if we need
2965 to copy relocs into the output file. */
2967 reloc_section = TRUE;
2971 /* It's not one of our sections, so don't allocate space. */
2977 /* If we don't need this section, strip it from the
2978 output file. This is mostly to handle .rela.bss and
2979 .rela.plt. We must create both sections in
2980 create_dynamic_sections, because they must be created
2981 before the linker maps input sections to output
2982 sections. The linker does that before
2983 adjust_dynamic_symbol is called, and it is that
2984 function which decides whether anything needs to go
2985 into these sections. */
2986 s->flags |= SEC_EXCLUDE;
2990 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2993 /* Allocate memory for the section contents. */
2994 s->contents = bfd_zalloc (dynobj, s->size);
2995 if (s->contents == NULL)
2997 else if (reloc_section)
2999 unsigned char *contents = s->contents;
3000 Elf32_External_Rela reloc;
3002 /* Fill the reloc section with a R_METAG_NONE type reloc. */
3003 memset(&reloc, 0, sizeof(Elf32_External_Rela));
3004 reloc.r_info[0] = R_METAG_NONE;
3005 for (; contents < (s->contents + s->size);
3006 contents += sizeof(Elf32_External_Rela))
3008 memcpy(contents, &reloc, sizeof(Elf32_External_Rela));
3013 if (htab->etab.dynamic_sections_created)
3015 /* Add some entries to the .dynamic section. We fill in the
3016 values later, in elf_metag_finish_dynamic_sections, but we
3017 must add the entries now so that we get the correct size for
3018 the .dynamic section. The DT_DEBUG entry is filled in by the
3019 dynamic linker and used by the debugger. */
3020 #define add_dynamic_entry(TAG, VAL) \
3021 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3023 if (!add_dynamic_entry (DT_PLTGOT, 0))
3026 if (bfd_link_executable (info))
3028 if (!add_dynamic_entry (DT_DEBUG, 0))
3032 if (htab->etab.srelplt->size != 0)
3034 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3035 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3036 || !add_dynamic_entry (DT_JMPREL, 0))
3042 if (!add_dynamic_entry (DT_RELA, 0)
3043 || !add_dynamic_entry (DT_RELASZ, 0)
3044 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3047 /* If any dynamic relocs apply to a read-only section,
3048 then we need a DT_TEXTREL entry. */
3049 if ((info->flags & DF_TEXTREL) == 0)
3050 elf_link_hash_traverse (&htab->etab, maybe_set_textrel, info);
3052 if ((info->flags & DF_TEXTREL) != 0)
3054 if (!add_dynamic_entry (DT_TEXTREL, 0))
3059 #undef add_dynamic_entry
3064 /* Finish up dynamic symbol handling. We set the contents of various
3065 dynamic sections here. */
3068 elf_metag_finish_dynamic_symbol (bfd *output_bfd,
3069 struct bfd_link_info *info,
3070 struct elf_link_hash_entry *eh,
3071 Elf_Internal_Sym *sym)
3073 struct elf_metag_link_hash_table *htab;
3074 Elf_Internal_Rela rel;
3077 htab = metag_link_hash_table (info);
3079 if (eh->plt.offset != (bfd_vma) -1)
3089 if (eh->plt.offset & 1)
3092 BFD_ASSERT (eh->dynindx != -1);
3094 splt = htab->etab.splt;
3095 sgot = htab->etab.sgotplt;
3096 srela = htab->etab.srelplt;
3097 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3099 /* Get the index in the procedure linkage table which
3100 corresponds to this symbol. This is the index of this symbol
3101 in all the symbols for which we are making plt entries. The
3102 first entry in the procedure linkage table is reserved. */
3103 plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1;
3105 /* Get the offset into the .got.plt table of the entry that
3106 corresponds to this function. */
3107 got_offset = plt_index * GOT_ENTRY_SIZE;
3109 BFD_ASSERT (got_offset < (1 << 16));
3111 got_entry = sgot->output_section->vma
3112 + sgot->output_offset
3115 BFD_ASSERT (plt_index < (1 << 16));
3117 /* Fill in the entry in the procedure linkage table. */
3118 if (! bfd_link_pic (info))
3120 bfd_put_32 (output_bfd,
3122 | (((got_entry >> 16) & 0xffff) << 3)),
3123 splt->contents + eh->plt.offset);
3124 bfd_put_32 (output_bfd,
3126 | ((got_entry & 0xffff) << 3)),
3127 splt->contents + eh->plt.offset + 4);
3128 bfd_put_32 (output_bfd, plt_entry[2],
3129 splt->contents + eh->plt.offset + 8);
3130 bfd_put_32 (output_bfd,
3131 (plt_entry[3] | (plt_index << 3)),
3132 splt->contents + eh->plt.offset + 12);
3133 bfd_put_32 (output_bfd,
3135 | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3136 splt->contents + eh->plt.offset + 16);
3140 bfd_vma addr = got_entry - (splt->output_section->vma +
3141 splt->output_offset + eh->plt.offset);
3143 bfd_put_32 (output_bfd,
3144 plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3145 splt->contents + eh->plt.offset);
3146 bfd_put_32 (output_bfd,
3147 plt_pic_entry[1] | ((addr & 0xffff) << 3),
3148 splt->contents + eh->plt.offset + 4);
3149 bfd_put_32 (output_bfd, plt_pic_entry[2],
3150 splt->contents + eh->plt.offset + 8);
3151 bfd_put_32 (output_bfd,
3152 (plt_pic_entry[3] | (plt_index << 3)),
3153 splt->contents + eh->plt.offset + 12);
3154 bfd_put_32 (output_bfd,
3156 + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3157 splt->contents + eh->plt.offset + 16);
3160 /* Fill in the entry in the global offset table. */
3161 bfd_put_32 (output_bfd,
3162 (splt->output_section->vma
3163 + splt->output_offset
3165 + 12), /* offset within PLT entry */
3166 sgot->contents + got_offset);
3168 /* Fill in the entry in the .rela.plt section. */
3169 rel.r_offset = (sgot->output_section->vma
3170 + sgot->output_offset
3172 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT);
3174 loc = htab->etab.srelplt->contents;
3175 loc += plt_index * sizeof(Elf32_External_Rela);
3176 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3178 if (!eh->def_regular)
3180 /* Mark the symbol as undefined, rather than as defined in
3181 the .plt section. Leave the value alone. */
3182 sym->st_shndx = SHN_UNDEF;
3186 if (eh->got.offset != (bfd_vma) -1
3187 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
3188 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
3190 /* This symbol has an entry in the global offset table. Set it
3193 rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
3194 + htab->etab.sgot->output_offset
3195 + htab->etab.sgot->output_section->vma);
3197 /* If this is a -Bsymbolic link and the symbol is defined
3198 locally or was forced to be local because of a version file,
3199 we just want to emit a RELATIVE reloc. The entry in the
3200 global offset table will already have been initialized in the
3201 relocate_section function. */
3202 if (bfd_link_pic (info)
3203 && (info->symbolic || eh->dynindx == -1)
3206 rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
3207 rel.r_addend = (eh->root.u.def.value
3208 + eh->root.u.def.section->output_offset
3209 + eh->root.u.def.section->output_section->vma);
3213 if ((eh->got.offset & 1) != 0)
3215 bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + eh->got.offset);
3216 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT);
3220 loc = htab->etab.srelgot->contents;
3221 loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3222 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3229 /* This symbol needs a copy reloc. Set it up. */
3231 if (! (eh->dynindx != -1
3232 && (eh->root.type == bfd_link_hash_defined
3233 || eh->root.type == bfd_link_hash_defweak)))
3236 rel.r_offset = (eh->root.u.def.value
3237 + eh->root.u.def.section->output_offset
3238 + eh->root.u.def.section->output_section->vma);
3240 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY);
3241 if (eh->root.u.def.section == htab->etab.sdynrelro)
3242 s = htab->etab.sreldynrelro;
3244 s = htab->etab.srelbss;
3245 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3246 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3249 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3250 if (eh->root.root.string[0] == '_'
3251 && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
3252 || eh == htab->etab.hgot))
3254 sym->st_shndx = SHN_ABS;
3260 /* Set the Meta ELF ABI version. */
3263 elf_metag_post_process_headers (bfd * abfd, struct bfd_link_info * link_info)
3265 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
3267 _bfd_elf_post_process_headers (abfd, link_info);
3268 i_ehdrp = elf_elfheader (abfd);
3269 i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION;
3272 /* Used to decide how to sort relocs in an optimal manner for the
3273 dynamic linker, before writing them out. */
3275 static enum elf_reloc_type_class
3276 elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3277 const asection *rel_sec ATTRIBUTE_UNUSED,
3278 const Elf_Internal_Rela *rela)
3280 switch ((int) ELF32_R_TYPE (rela->r_info))
3282 case R_METAG_RELATIVE:
3283 return reloc_class_relative;
3284 case R_METAG_JMP_SLOT:
3285 return reloc_class_plt;
3287 return reloc_class_copy;
3289 return reloc_class_normal;
3293 /* Finish up the dynamic sections. */
3296 elf_metag_finish_dynamic_sections (bfd *output_bfd,
3297 struct bfd_link_info *info)
3300 struct elf_metag_link_hash_table *htab;
3303 htab = metag_link_hash_table (info);
3304 dynobj = htab->etab.dynobj;
3306 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3308 if (htab->etab.dynamic_sections_created)
3311 Elf32_External_Dyn *dyncon, *dynconend;
3316 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3317 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3318 for (; dyncon < dynconend; dyncon++)
3320 Elf_Internal_Dyn dyn;
3323 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3331 s = htab->etab.sgot;
3332 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3333 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3337 s = htab->etab.srelplt;
3338 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3339 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3343 s = htab->etab.srelplt;
3344 dyn.d_un.d_val = s->size;
3345 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3351 /* Fill in the first entry in the procedure linkage table. */
3352 splt = htab->etab.splt;
3353 if (splt && splt->size > 0)
3356 /* addr = .got + 4 */
3357 addr = (htab->etab.sgot->output_section->vma
3358 + htab->etab.sgot->output_offset + 4);
3359 if (bfd_link_pic (info))
3361 addr -= splt->output_section->vma + splt->output_offset;
3362 bfd_put_32 (output_bfd,
3363 plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3365 bfd_put_32 (output_bfd,
3366 plt0_pic_entry[1] | ((addr & 0xffff) << 3),
3367 splt->contents + 4);
3368 bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8);
3369 bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12);
3370 bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16);
3374 bfd_put_32 (output_bfd,
3375 plt0_entry[0] | (((addr >> 16) & 0xffff) << 3),
3377 bfd_put_32 (output_bfd,
3378 plt0_entry[1] | ((addr & 0xffff) << 3),
3379 splt->contents + 4);
3380 bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
3381 bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
3382 bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16);
3385 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3390 if (htab->etab.sgot != NULL && htab->etab.sgot->size != 0)
3392 /* Fill in the first entry in the global offset table.
3393 We use it to point to our dynamic section, if we have one. */
3394 bfd_put_32 (output_bfd,
3395 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
3396 htab->etab.sgot->contents);
3398 /* The second entry is reserved for use by the dynamic linker. */
3399 memset (htab->etab.sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
3401 /* Set .got entry size. */
3402 elf_section_data (htab->etab.sgot->output_section)
3403 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3409 /* Return the section that should be marked against GC for a given
3413 elf_metag_gc_mark_hook (asection *sec,
3414 struct bfd_link_info *info,
3415 Elf_Internal_Rela *rela,
3416 struct elf_link_hash_entry *hh,
3417 Elf_Internal_Sym *sym)
3420 switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
3422 case R_METAG_GNU_VTINHERIT:
3423 case R_METAG_GNU_VTENTRY:
3427 return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
3430 /* Determine the type of stub needed, if any, for a call. */
3432 static enum elf_metag_stub_type
3433 metag_type_of_stub (asection *input_sec,
3434 const Elf_Internal_Rela *rel,
3435 struct elf_metag_link_hash_entry *hh,
3436 bfd_vma destination,
3437 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3440 bfd_vma branch_offset;
3441 bfd_vma max_branch_offset;
3444 !(hh->eh.root.type == bfd_link_hash_defined
3445 || hh->eh.root.type == bfd_link_hash_defweak))
3446 return metag_stub_none;
3448 /* Determine where the call point is. */
3449 location = (input_sec->output_offset
3450 + input_sec->output_section->vma
3453 branch_offset = destination - location;
3455 /* Determine if a long branch stub is needed. Meta branch offsets
3456 are signed 19 bits 4 byte aligned. */
3457 max_branch_offset = (1 << (BRANCH_BITS-1)) << 2;
3459 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
3461 if (bfd_link_pic (info))
3462 return metag_stub_long_branch_shared;
3464 return metag_stub_long_branch;
3467 return metag_stub_none;
3470 #define MOVT_A0_3 0x82180005
3471 #define JUMP_A0_3 0xac180003
3473 #define MOVT_A1LBP 0x83080005
3474 #define ADD_A1LBP 0x83080000
3476 #define ADDT_A0_3_CPC 0x82980001
3477 #define ADD_A0_3_A0_3 0x82180000
3478 #define MOV_PC_A0_3 0xa3180ca0
3481 metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3483 struct elf_metag_stub_hash_entry *hsh;
3490 /* Massage our args to the form they really have. */
3491 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3493 stub_sec = hsh->stub_sec;
3495 /* Make a note of the offset within the stubs for this entry. */
3496 hsh->stub_offset = stub_sec->size;
3497 loc = stub_sec->contents + hsh->stub_offset;
3499 stub_bfd = stub_sec->owner;
3501 switch (hsh->stub_type)
3503 case metag_stub_long_branch_shared:
3504 /* A PIC long branch stub is an ADDT and an ADD instruction used to
3505 calculate the jump target using A0.3 as a temporary. Then a MOV
3506 to PC carries out the jump. */
3507 sym_value = (hsh->target_value
3508 + hsh->target_section->output_offset
3509 + hsh->target_section->output_section->vma
3512 sym_value -= (hsh->stub_offset
3513 + stub_sec->output_offset
3514 + stub_sec->output_section->vma);
3516 bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3),
3519 bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3),
3522 bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8);
3526 case metag_stub_long_branch:
3527 /* A standard long branch stub is a MOVT instruction followed by a
3528 JUMP instruction using the A0.3 register as a temporary. This is
3529 the same method used by the LDLK linker (patch.c). */
3530 sym_value = (hsh->target_value
3531 + hsh->target_section->output_offset
3532 + hsh->target_section->output_section->vma
3535 bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3),
3538 bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4);
3547 stub_sec->size += size;
3551 /* As above, but don't actually build the stub. Just bump offset so
3552 we know stub section sizes. */
3555 metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3557 struct elf_metag_stub_hash_entry *hsh;
3560 /* Massage our args to the form they really have. */
3561 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3563 if (hsh->stub_type == metag_stub_long_branch)
3565 else if (hsh->stub_type == metag_stub_long_branch_shared)
3568 hsh->stub_sec->size += size;
3572 /* Set up various things so that we can make a list of input sections
3573 for each output section included in the link. Returns -1 on error,
3574 0 when no stubs will be needed, and 1 on success. */
3577 elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3580 unsigned int bfd_count;
3581 unsigned int top_id, top_index;
3583 asection **input_list, **list;
3585 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3587 /* Count the number of input BFDs and find the top input section id. */
3588 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3590 input_bfd = input_bfd->link.next)
3593 for (section = input_bfd->sections;
3595 section = section->next)
3597 if (top_id < section->id)
3598 top_id = section->id;
3602 htab->bfd_count = bfd_count;
3604 amt = sizeof (struct map_stub) * (top_id + 1);
3605 htab->stub_group = bfd_zmalloc (amt);
3606 if (htab->stub_group == NULL)
3609 /* We can't use output_bfd->section_count here to find the top output
3610 section index as some sections may have been removed, and
3611 strip_excluded_output_sections doesn't renumber the indices. */
3612 for (section = output_bfd->sections, top_index = 0;
3614 section = section->next)
3616 if (top_index < section->index)
3617 top_index = section->index;
3620 htab->top_index = top_index;
3621 amt = sizeof (asection *) * (top_index + 1);
3622 input_list = bfd_malloc (amt);
3623 htab->input_list = input_list;
3624 if (input_list == NULL)
3627 /* For sections we aren't interested in, mark their entries with a
3628 value we can check later. */
3629 list = input_list + top_index;
3631 *list = bfd_abs_section_ptr;
3632 while (list-- != input_list);
3634 for (section = output_bfd->sections;
3636 section = section->next)
3638 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3639 * have PC relative relocs in them but no code flag set. */
3640 if (((section->flags & SEC_CODE) != 0) ||
3641 strcmp(".ctors", section->name) ||
3642 strcmp(".dtors", section->name))
3643 input_list[section->index] = NULL;
3649 /* The linker repeatedly calls this function for each input section,
3650 in the order that input sections are linked into output sections.
3651 Build lists of input sections to determine groupings between which
3652 we may insert linker stubs. */
3655 elf_metag_next_input_section (struct bfd_link_info *info, asection *isec)
3657 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3659 if (isec->output_section->index <= htab->top_index)
3661 asection **list = htab->input_list + isec->output_section->index;
3662 if (*list != bfd_abs_section_ptr)
3664 /* Steal the link_sec pointer for our list. */
3665 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3666 /* This happens to make the list in reverse order,
3667 which is what we want. */
3668 PREV_SEC (isec) = *list;
3674 /* See whether we can group stub sections together. Grouping stub
3675 sections may result in fewer stubs. More importantly, we need to
3676 put all .init* and .fini* stubs at the beginning of the .init or
3677 .fini output sections respectively, because glibc splits the
3678 _init and _fini functions into multiple parts. Putting a stub in
3679 the middle of a function is not a good idea. */
3682 group_sections (struct elf_metag_link_hash_table *htab,
3683 bfd_size_type stub_group_size,
3684 bfd_boolean stubs_always_before_branch)
3686 asection **list = htab->input_list + htab->top_index;
3689 asection *tail = *list;
3690 if (tail == bfd_abs_section_ptr)
3692 while (tail != NULL)
3696 bfd_size_type total;
3697 bfd_boolean big_sec;
3701 big_sec = total >= stub_group_size;
3703 while ((prev = PREV_SEC (curr)) != NULL
3704 && ((total += curr->output_offset - prev->output_offset)
3708 /* OK, the size from the start of CURR to the end is less
3709 than stub_group_size bytes and thus can be handled by one stub
3710 section. (or the tail section is itself larger than
3711 stub_group_size bytes, in which case we may be toast.)
3712 We should really be keeping track of the total size of
3713 stubs added here, as stubs contribute to the final output
3717 prev = PREV_SEC (tail);
3718 /* Set up this stub group. */
3719 htab->stub_group[tail->id].link_sec = curr;
3721 while (tail != curr && (tail = prev) != NULL);
3723 /* But wait, there's more! Input sections up to stub_group_size
3724 bytes before the stub section can be handled by it too.
3725 Don't do this if we have a really large section after the
3726 stubs, as adding more stubs increases the chance that
3727 branches may not reach into the stub section. */
3728 if (!stubs_always_before_branch && !big_sec)
3732 && ((total += tail->output_offset - prev->output_offset)
3736 prev = PREV_SEC (tail);
3737 htab->stub_group[tail->id].link_sec = curr;
3743 while (list-- != htab->input_list);
3744 free (htab->input_list);
3748 /* Read in all local syms for all input bfds.
3749 Returns -1 on error, 0 otherwise. */
3752 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
3753 struct bfd_link_info *info)
3755 unsigned int bfd_indx;
3756 Elf_Internal_Sym *local_syms, **all_local_syms;
3757 int stub_changed = 0;
3758 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3760 /* We want to read in symbol extension records only once. To do this
3761 we need to read in the local symbols in parallel and save them for
3762 later use; so hold pointers to the local symbols in an array. */
3763 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3764 all_local_syms = bfd_zmalloc (amt);
3765 htab->all_local_syms = all_local_syms;
3766 if (all_local_syms == NULL)
3769 /* Walk over all the input BFDs, swapping in local symbols. */
3772 input_bfd = input_bfd->link.next, bfd_indx++)
3774 Elf_Internal_Shdr *symtab_hdr;
3776 /* We'll need the symbol table in a second. */
3777 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3778 if (symtab_hdr->sh_info == 0)
3781 /* We need an array of the local symbols attached to the input bfd. */
3782 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3783 if (local_syms == NULL)
3785 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3786 symtab_hdr->sh_info, 0,
3788 /* Cache them for elf_link_input_bfd. */
3789 symtab_hdr->contents = (unsigned char *) local_syms;
3791 if (local_syms == NULL)
3794 all_local_syms[bfd_indx] = local_syms;
3797 return stub_changed;
3800 /* Determine and set the size of the stub section for a final link.
3802 The basic idea here is to examine all the relocations looking for
3803 PC-relative calls to a target that is unreachable with a "CALLR"
3806 /* See elf32-hppa.c and elf64-ppc.c. */
3809 elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd,
3810 struct bfd_link_info *info,
3811 bfd_signed_vma group_size,
3812 asection * (*add_stub_section) (const char *, asection *),
3813 void (*layout_sections_again) (void))
3815 bfd_size_type stub_group_size;
3816 bfd_boolean stubs_always_before_branch;
3817 bfd_boolean stub_changed;
3818 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3820 /* Stash our params away. */
3821 htab->stub_bfd = stub_bfd;
3822 htab->add_stub_section = add_stub_section;
3823 htab->layout_sections_again = layout_sections_again;
3824 stubs_always_before_branch = group_size < 0;
3826 stub_group_size = -group_size;
3828 stub_group_size = group_size;
3829 if (stub_group_size == 1)
3831 /* Default values. */
3832 /* FIXME: not sure what these values should be */
3833 if (stubs_always_before_branch)
3835 stub_group_size = (1 << BRANCH_BITS);
3839 stub_group_size = (1 << BRANCH_BITS);
3843 group_sections (htab, stub_group_size, stubs_always_before_branch);
3845 switch (get_local_syms (output_bfd, info->input_bfds, info))
3848 if (htab->all_local_syms)
3849 goto error_ret_free_local;
3853 stub_changed = FALSE;
3857 stub_changed = TRUE;
3864 unsigned int bfd_indx;
3867 for (input_bfd = info->input_bfds, bfd_indx = 0;
3869 input_bfd = input_bfd->link.next, bfd_indx++)
3871 Elf_Internal_Shdr *symtab_hdr;
3873 Elf_Internal_Sym *local_syms;
3875 /* We'll need the symbol table in a second. */
3876 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3877 if (symtab_hdr->sh_info == 0)
3880 local_syms = htab->all_local_syms[bfd_indx];
3882 /* Walk over each section attached to the input bfd. */
3883 for (section = input_bfd->sections;
3885 section = section->next)
3887 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3889 /* If there aren't any relocs, then there's nothing more
3891 if ((section->flags & SEC_RELOC) == 0
3892 || section->reloc_count == 0)
3895 /* If this section is a link-once section that will be
3896 discarded, then don't create any stubs. */
3897 if (section->output_section == NULL
3898 || section->output_section->owner != output_bfd)
3901 /* Get the relocs. */
3903 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3905 if (internal_relocs == NULL)
3906 goto error_ret_free_local;
3908 /* Now examine each relocation. */
3909 irela = internal_relocs;
3910 irelaend = irela + section->reloc_count;
3911 for (; irela < irelaend; irela++)
3913 unsigned int r_type, r_indx;
3914 enum elf_metag_stub_type stub_type;
3915 struct elf_metag_stub_hash_entry *hsh;
3918 bfd_vma destination;
3919 struct elf_metag_link_hash_entry *hh;
3921 const asection *id_sec;
3923 r_type = ELF32_R_TYPE (irela->r_info);
3924 r_indx = ELF32_R_SYM (irela->r_info);
3926 if (r_type >= (unsigned int) R_METAG_MAX)
3928 bfd_set_error (bfd_error_bad_value);
3929 error_ret_free_internal:
3930 if (elf_section_data (section)->relocs == NULL)
3931 free (internal_relocs);
3932 goto error_ret_free_local;
3935 /* Only look for stubs on CALLR and B instructions. */
3936 if (!(r_type == (unsigned int) R_METAG_RELBRANCH ||
3937 r_type == (unsigned int) R_METAG_RELBRANCH_PLT))
3940 /* Now determine the call target, its name, value,
3946 if (r_indx < symtab_hdr->sh_info)
3948 /* It's a local symbol. */
3949 Elf_Internal_Sym *sym;
3950 Elf_Internal_Shdr *hdr;
3953 sym = local_syms + r_indx;
3954 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3955 sym_value = sym->st_value;
3956 shndx = sym->st_shndx;
3957 if (shndx < elf_numsections (input_bfd))
3959 hdr = elf_elfsections (input_bfd)[shndx];
3960 sym_sec = hdr->bfd_section;
3961 destination = (sym_value + irela->r_addend
3962 + sym_sec->output_offset
3963 + sym_sec->output_section->vma);
3968 /* It's an external symbol. */
3971 e_indx = r_indx - symtab_hdr->sh_info;
3972 hh = ((struct elf_metag_link_hash_entry *)
3973 elf_sym_hashes (input_bfd)[e_indx]);
3975 while (hh->eh.root.type == bfd_link_hash_indirect
3976 || hh->eh.root.type == bfd_link_hash_warning)
3977 hh = ((struct elf_metag_link_hash_entry *)
3978 hh->eh.root.u.i.link);
3980 if (hh->eh.root.type == bfd_link_hash_defined
3981 || hh->eh.root.type == bfd_link_hash_defweak)
3983 sym_sec = hh->eh.root.u.def.section;
3984 sym_value = hh->eh.root.u.def.value;
3985 if (hh->eh.plt.offset != (bfd_vma) -1
3986 && hh->eh.dynindx != -1
3987 && r_type == (unsigned int) R_METAG_RELBRANCH_PLT)
3989 sym_sec = htab->etab.splt;
3990 sym_value = hh->eh.plt.offset;
3993 if (sym_sec->output_section != NULL)
3994 destination = (sym_value + irela->r_addend
3995 + sym_sec->output_offset
3996 + sym_sec->output_section->vma);
4000 else if (hh->eh.root.type == bfd_link_hash_undefweak)
4002 if (! bfd_link_pic (info))
4005 else if (hh->eh.root.type == bfd_link_hash_undefined)
4007 if (! (info->unresolved_syms_in_objects == RM_IGNORE
4008 && (ELF_ST_VISIBILITY (hh->eh.other)
4014 bfd_set_error (bfd_error_bad_value);
4015 goto error_ret_free_internal;
4019 /* Determine what (if any) linker stub is needed. */
4020 stub_type = metag_type_of_stub (section, irela, hh,
4022 if (stub_type == metag_stub_none)
4025 /* Support for grouping stub sections. */
4026 id_sec = htab->stub_group[section->id].link_sec;
4028 /* Get the name of this stub. */
4029 stub_name = metag_stub_name (id_sec, sym_sec, hh, irela);
4031 goto error_ret_free_internal;
4033 hsh = metag_stub_hash_lookup (&htab->bstab,
4038 /* The proper stub has already been created. */
4043 hsh = metag_add_stub (stub_name, section, htab);
4047 goto error_ret_free_internal;
4049 hsh->target_value = sym_value;
4050 hsh->target_section = sym_sec;
4051 hsh->stub_type = stub_type;
4053 hsh->addend = irela->r_addend;
4054 stub_changed = TRUE;
4057 /* We're done with the internal relocs, free them. */
4058 if (elf_section_data (section)->relocs == NULL)
4059 free (internal_relocs);
4066 /* OK, we've added some stubs. Find out the new size of the
4068 for (stub_sec = htab->stub_bfd->sections;
4070 stub_sec = stub_sec->next)
4073 bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab);
4075 /* Ask the linker to do its stuff. */
4076 (*htab->layout_sections_again) ();
4077 stub_changed = FALSE;
4080 free (htab->all_local_syms);
4083 error_ret_free_local:
4084 free (htab->all_local_syms);
4088 /* Build all the stubs associated with the current output file. The
4089 stubs are kept in a hash table attached to the main linker hash
4090 table. This function is called via metagelf_finish in the linker. */
4093 elf_metag_build_stubs (struct bfd_link_info *info)
4096 struct bfd_hash_table *table;
4097 struct elf_metag_link_hash_table *htab;
4099 htab = metag_link_hash_table (info);
4101 for (stub_sec = htab->stub_bfd->sections;
4103 stub_sec = stub_sec->next)
4107 /* Allocate memory to hold the linker stubs. */
4108 size = stub_sec->size;
4109 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4110 if (stub_sec->contents == NULL && size != 0)
4115 /* Build the stubs as directed by the stub hash table. */
4116 table = &htab->bstab;
4117 bfd_hash_traverse (table, metag_build_one_stub, info);
4122 /* Return TRUE if SYM represents a local label symbol. */
4125 elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
4127 if (name[0] == '$' && name[1] == 'L')
4129 return _bfd_elf_is_local_label_name (abfd, name);
4132 /* Return address for Ith PLT stub in section PLT, for relocation REL
4133 or (bfd_vma) -1 if it should not be included. */
4136 elf_metag_plt_sym_val (bfd_vma i, const asection *plt,
4137 const arelent *rel ATTRIBUTE_UNUSED)
4139 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4142 #define ELF_ARCH bfd_arch_metag
4143 #define ELF_TARGET_ID METAG_ELF_DATA
4144 #define ELF_MACHINE_CODE EM_METAG
4145 #define ELF_MINPAGESIZE 0x1000
4146 #define ELF_MAXPAGESIZE 0x4000
4147 #define ELF_COMMONPAGESIZE 0x1000
4149 #define TARGET_LITTLE_SYM metag_elf32_vec
4150 #define TARGET_LITTLE_NAME "elf32-metag"
4152 #define elf_symbol_leading_char '_'
4154 #define elf_info_to_howto_rel NULL
4155 #define elf_info_to_howto metag_info_to_howto_rela
4157 #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
4158 #define bfd_elf32_bfd_link_hash_table_create \
4159 elf_metag_link_hash_table_create
4160 #define elf_backend_relocate_section elf_metag_relocate_section
4161 #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
4162 #define elf_backend_check_relocs elf_metag_check_relocs
4163 #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
4164 #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
4165 #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
4166 #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
4167 #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
4168 #define elf_backend_omit_section_dynsym \
4169 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4170 #define elf_backend_post_process_headers elf_metag_post_process_headers
4171 #define elf_backend_reloc_type_class elf_metag_reloc_type_class
4172 #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
4173 #define elf_backend_plt_sym_val elf_metag_plt_sym_val
4175 #define elf_backend_can_gc_sections 1
4176 #define elf_backend_can_refcount 1
4177 #define elf_backend_rela_normal 1
4178 #define elf_backend_want_got_plt 1
4179 #define elf_backend_want_got_sym 0
4180 #define elf_backend_want_plt_sym 0
4181 #define elf_backend_plt_readonly 1
4182 #define elf_backend_dtrel_excludes_plt 1
4183 #define elf_backend_want_dynrelro 1
4185 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4186 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4188 #include "elf32-target.h"