1 /* Meta support for 32-bit ELF
2 Copyright (C) 2013-2018 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 Always remember to use GNU Coding Style. */
91 #define PLT_ENTRY_SIZE sizeof(plt_entry)
93 static reloc_howto_type elf_metag_howto_table[] =
95 /* High order 16 bit absolute. */
96 HOWTO (R_METAG_HIADDR16, /* type */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
100 FALSE, /* pc_relative */
102 complain_overflow_dont, /* complain_on_overflow */
103 bfd_elf_generic_reloc, /* special_function */
104 "R_METAG_HIADDR16", /* name */
105 FALSE, /* partial_inplace */
107 0x0007fff8, /* dst_mask */
108 FALSE), /* pcrel_offset */
110 /* Low order 16 bit absolute. */
111 HOWTO (R_METAG_LOADDR16, /* type */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
115 FALSE, /* pc_relative */
117 complain_overflow_dont,/* complain_on_overflow */
118 bfd_elf_generic_reloc, /* special_function */
119 "R_METAG_LOADDR16", /* name */
120 FALSE, /* partial_inplace */
122 0x0007fff8, /* dst_mask */
123 FALSE), /* pcrel_offset */
125 /* 32 bit absolute. */
126 HOWTO (R_METAG_ADDR32, /* type */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
130 FALSE, /* pc_relative */
132 complain_overflow_bitfield, /* complain_on_overflow */
133 bfd_elf_generic_reloc, /* special_function */
134 "R_METAG_ADDR32", /* name */
135 FALSE, /* partial_inplace */
136 0x00000000, /* src_mask */
137 0xffffffff, /* dst_mask */
138 FALSE), /* pcrel_offset */
141 HOWTO (R_METAG_NONE, /* type */
143 3, /* size (0 = byte, 1 = short, 2 = long) */
145 FALSE, /* pc_relative */
147 complain_overflow_dont, /* complain_on_overflow */
148 bfd_elf_generic_reloc, /* special_function */
149 "R_METAG_NONE", /* name */
150 FALSE, /* partial_inplace */
153 FALSE), /* pcrel_offset */
155 /* 19 bit pc relative */
156 HOWTO (R_METAG_RELBRANCH, /* type */
158 2, /* size (0 = byte, 1 = short, 2 = long) */
160 TRUE, /* pc_relative */
162 complain_overflow_signed, /* complain_on_overflow */
163 bfd_elf_generic_reloc, /* special_function */
164 "R_METAG_RELBRANCH", /* name */
165 FALSE, /* partial_inplace */
167 0x00ffffe0, /* dst_mask */
168 FALSE), /* pcrel_offset */
171 HOWTO (R_METAG_GETSETOFF, /* type */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
175 FALSE, /* pc_relative */
177 complain_overflow_dont, /* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_METAG_GETSETOFF", /* name */
180 FALSE, /* partial_inplace */
183 FALSE), /* pcrel_offset */
210 HOWTO (R_METAG_GNU_VTINHERIT, /* type */
212 2, /* size (0 = byte, 1 = short, 2 = long) */
214 FALSE, /* pc_relative */
216 complain_overflow_dont, /* complain_on_overflow */
217 NULL, /* special_function */
218 "R_METAG_GNU_VTINHERIT", /* name */
219 FALSE, /* partial_inplace */
222 FALSE), /* pcrel_offset */
224 HOWTO (R_METAG_GNU_VTENTRY, /* type */
226 2, /* size (0 = byte, 1 = short, 2 = long) */
228 FALSE, /* pc_relative */
230 complain_overflow_dont, /* complain_on_overflow */
231 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
232 "R_METAG_GNU_VTENTRY", /* name */
233 FALSE, /* partial_inplace */
236 FALSE), /* pcrel_offset */
238 /* High order 16 bit GOT offset */
239 HOWTO (R_METAG_HI16_GOTOFF, /* type */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
243 FALSE, /* pc_relative */
245 complain_overflow_dont, /* complain_on_overflow */
246 bfd_elf_generic_reloc, /* special_function */
247 "R_METAG_HI16_GOTOFF", /* name */
248 FALSE, /* partial_inplace */
250 0x0007fff8, /* dst_mask */
251 FALSE), /* pcrel_offset */
253 /* Low order 16 bit GOT offset */
254 HOWTO (R_METAG_LO16_GOTOFF, /* type */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
258 FALSE, /* pc_relative */
260 complain_overflow_dont, /* complain_on_overflow */
261 bfd_elf_generic_reloc, /* special_function */
262 "R_METAG_LO16_GOTOFF", /* name */
263 FALSE, /* partial_inplace */
265 0x0007fff8, /* dst_mask */
266 FALSE), /* pcrel_offset */
268 /* GET/SET GOT offset */
269 HOWTO (R_METAG_GETSET_GOTOFF, /* type */
271 1, /* size (0 = byte, 1 = short, 2 = long) */
273 FALSE, /* pc_relative */
275 complain_overflow_dont, /* complain_on_overflow */
276 bfd_elf_generic_reloc, /* special_function */
277 "R_METAG_GETSET_GOTOFF", /* name */
278 FALSE, /* partial_inplace */
281 FALSE), /* pcrel_offset */
283 /* GET/SET GOT relative */
284 HOWTO (R_METAG_GETSET_GOT, /* type */
286 1, /* size (0 = byte, 1 = short, 2 = long) */
288 FALSE, /* pc_relative */
290 complain_overflow_dont, /* complain_on_overflow */
291 bfd_elf_generic_reloc, /* special_function */
292 "R_METAG_GETSET_GOT", /* name */
293 FALSE, /* partial_inplace */
296 FALSE), /* pcrel_offset */
298 /* High order 16 bit GOT reference */
299 HOWTO (R_METAG_HI16_GOTPC, /* type */
301 2, /* size (0 = byte, 1 = short, 2 = long) */
303 FALSE, /* pc_relative */
305 complain_overflow_dont, /* complain_on_overflow */
306 bfd_elf_generic_reloc, /* special_function */
307 "R_METAG_HI16_GOTPC", /* name */
308 FALSE, /* partial_inplace */
310 0x0007fff8, /* dst_mask */
311 FALSE), /* pcrel_offset */
313 /* Low order 16 bit GOT reference */
314 HOWTO (R_METAG_LO16_GOTPC, /* type */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
318 FALSE, /* pc_relative */
320 complain_overflow_dont, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_METAG_LO16_GOTPC", /* name */
323 FALSE, /* partial_inplace */
325 0x0007fff8, /* dst_mask */
326 FALSE), /* pcrel_offset */
328 /* High order 16 bit PLT */
329 HOWTO (R_METAG_HI16_PLT, /* type */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
333 FALSE, /* pc_relative */
335 complain_overflow_dont, /* complain_on_overflow */
336 bfd_elf_generic_reloc, /* special_function */
337 "R_METAG_HI16_PLT", /* name */
338 FALSE, /* partial_inplace */
340 0x0007fff8, /* dst_mask */
341 FALSE), /* pcrel_offset */
343 /* Low order 16 bit PLT */
344 HOWTO (R_METAG_LO16_PLT, /* type */
346 2, /* size (0 = byte, 1 = short, 2 = long) */
348 FALSE, /* pc_relative */
350 complain_overflow_dont, /* complain_on_overflow */
351 bfd_elf_generic_reloc, /* special_function */
352 "R_METAG_LO16_PLT", /* name */
353 FALSE, /* partial_inplace */
355 0xffffffff, /* dst_mask */
356 FALSE), /* pcrel_offset */
358 HOWTO (R_METAG_RELBRANCH_PLT, /* type */
360 2, /* size (0 = byte, 1 = short, 2 = long) */
362 TRUE, /* pc_relative */
364 complain_overflow_signed, /* complain_on_overflow */
365 bfd_elf_generic_reloc, /* special_function */
366 "R_METAG_RELBRANCH_PLT", /* name */
367 FALSE, /* partial_inplace */
369 0x00ffffe0, /* dst_mask */
370 FALSE), /* pcrel_offset */
372 /* Dummy relocs used by the linker internally. */
373 HOWTO (R_METAG_GOTOFF, /* type */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
377 FALSE, /* pc_relative */
379 complain_overflow_bitfield, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_METAG_GOTOFF", /* name */
382 FALSE, /* partial_inplace */
383 0xffffffff, /* src_mask */
384 0xffffffff, /* dst_mask */
385 FALSE), /* pcrel_offset */
387 HOWTO (R_METAG_PLT, /* type */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
391 FALSE, /* pc_relative */
393 complain_overflow_bitfield, /* complain_on_overflow */
394 bfd_elf_generic_reloc, /* special_function */
395 "R_METAG_GOTOFF", /* name */
396 FALSE, /* partial_inplace */
397 0xffffffff, /* src_mask */
398 0xffffffff, /* dst_mask */
399 FALSE), /* pcrel_offset */
401 /* This is used only by the dynamic linker. The symbol should exist
402 both in the object being run and in some shared library. The
403 dynamic linker copies the data addressed by the symbol from the
404 shared library into the object, because the object being
405 run has to have the data at some particular address. */
406 HOWTO (R_METAG_COPY, /* type */
408 2, /* size (0 = byte, 1 = short, 2 = long) */
410 FALSE, /* pc_relative */
412 complain_overflow_bitfield, /* complain_on_overflow */
413 bfd_elf_generic_reloc, /* special_function */
414 "R_METAG_COPY", /* name */
415 FALSE, /* partial_inplace */
416 0xffffffff, /* src_mask */
417 0xffffffff, /* dst_mask */
418 FALSE), /* pcrel_offset */
420 /* Marks a procedure linkage table entry for a symbol. */
421 HOWTO (R_METAG_JMP_SLOT, /* type */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
425 FALSE, /* pc_relative */
427 complain_overflow_bitfield, /* complain_on_overflow */
428 bfd_elf_generic_reloc, /* special_function */
429 "R_METAG_JMP_SLOT", /* name */
430 FALSE, /* partial_inplace */
431 0xffffffff, /* src_mask */
432 0xffffffff, /* dst_mask */
433 FALSE), /* pcrel_offset */
435 /* Used only by the dynamic linker. When the object is run, this
436 longword is set to the load address of the object, plus the
438 HOWTO (R_METAG_RELATIVE, /* type */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
442 FALSE, /* pc_relative */
444 complain_overflow_bitfield, /* complain_on_overflow */
445 bfd_elf_generic_reloc, /* special_function */
446 "R_METAG_RELATIVE", /* name */
447 FALSE, /* partial_inplace */
448 0xffffffff, /* src_mask */
449 0xffffffff, /* dst_mask */
450 FALSE), /* pcrel_offset */
452 HOWTO (R_METAG_GLOB_DAT, /* type */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
456 FALSE, /* pc_relative */
458 complain_overflow_bitfield, /* complain_on_overflow */
459 bfd_elf_generic_reloc, /* special_function */
460 "R_METAG_GLOB_DAT", /* name */
461 FALSE, /* partial_inplace */
462 0xffffffff, /* src_mask */
463 0xffffffff, /* dst_mask */
464 FALSE), /* pcrel_offset */
466 HOWTO (R_METAG_TLS_GD, /* type */
468 2, /* size (0 = byte, 1 = short, 2 = long) */
470 FALSE, /* pc_relative */
472 complain_overflow_dont, /* complain_on_overflow */
473 bfd_elf_generic_reloc, /* special_function */
474 "R_METAG_TLS_GD", /* name */
475 FALSE, /* partial_inplace */
477 0x0007fff8, /* dst_mask */
478 FALSE), /* pcrel_offset */
480 HOWTO (R_METAG_TLS_LDM, /* type */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
484 FALSE, /* pc_relative */
486 complain_overflow_bitfield, /* complain_on_overflow */
487 bfd_elf_generic_reloc, /* special_function */
488 "R_METAG_TLS_LDM", /* name */
489 FALSE, /* partial_inplace */
491 0x0007fff8, /* dst_mask */
492 FALSE), /* pcrel_offset */
494 HOWTO (R_METAG_TLS_LDO_HI16, /* type */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
498 FALSE, /* pc_relative */
500 complain_overflow_bitfield, /* complain_on_overflow */
501 bfd_elf_generic_reloc, /* special_function */
502 "R_METAG_TLS_LDO_HI16", /* name */
503 FALSE, /* partial_inplace */
505 0x0007fff8, /* dst_mask */
506 FALSE), /* pcrel_offset */
508 HOWTO (R_METAG_TLS_LDO_LO16, /* type */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
512 FALSE, /* pc_relative */
514 complain_overflow_bitfield, /* complain_on_overflow */
515 bfd_elf_generic_reloc, /* special_function */
516 "R_METAG_TLS_LDO_LO16", /* name */
517 FALSE, /* partial_inplace */
519 0x0007fff8, /* dst_mask */
520 FALSE), /* pcrel_offset */
522 /* Dummy reloc used by the linker internally. */
523 HOWTO (R_METAG_TLS_LDO, /* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 FALSE, /* pc_relative */
529 complain_overflow_bitfield, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_METAG_TLS_LDO", /* name */
532 FALSE, /* partial_inplace */
534 0x0007fff8, /* dst_mask */
535 FALSE), /* pcrel_offset */
537 HOWTO (R_METAG_TLS_IE, /* type */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
541 FALSE, /* pc_relative */
543 complain_overflow_dont, /* complain_on_overflow */
544 bfd_elf_generic_reloc, /* special_function */
545 "R_METAG_TLS_IE", /* name */
546 FALSE, /* partial_inplace */
548 0x0007ff80, /* dst_mask */
549 FALSE), /* pcrel_offset */
551 /* Dummy reloc used by the linker internally. */
552 HOWTO (R_METAG_TLS_IENONPIC, /* type */
554 2, /* size (0 = byte, 1 = short, 2 = long) */
556 FALSE, /* pc_relative */
558 complain_overflow_dont, /* complain_on_overflow */
559 bfd_elf_generic_reloc, /* special_function */
560 "R_METAG_TLS_IENONPIC", /* name */
561 FALSE, /* partial_inplace */
563 0x0007fff8, /* dst_mask */
564 FALSE), /* pcrel_offset */
566 HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
570 FALSE, /* pc_relative */
572 complain_overflow_dont, /* complain_on_overflow */
573 bfd_elf_generic_reloc, /* special_function */
574 "R_METAG_TLS_IENONPIC_HI16", /* name */
575 FALSE, /* partial_inplace */
577 0x0007fff8, /* dst_mask */
578 FALSE), /* pcrel_offset */
580 HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */
582 2, /* size (0 = byte, 1 = short, 2 = long) */
584 FALSE, /* pc_relative */
586 complain_overflow_dont, /* complain_on_overflow */
587 bfd_elf_generic_reloc, /* special_function */
588 "R_METAG_TLS_IENONPIC_LO16", /* name */
589 FALSE, /* partial_inplace */
591 0x0007fff8, /* dst_mask */
592 FALSE), /* pcrel_offset */
594 HOWTO (R_METAG_TLS_TPOFF, /* type */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
598 FALSE, /* pc_relative */
600 complain_overflow_bitfield, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* special_function */
602 "R_METAG_TLS_TPOFF", /* name */
603 FALSE, /* partial_inplace */
605 0xffffffff, /* dst_mask */
606 FALSE), /* pcrel_offset */
608 HOWTO (R_METAG_TLS_DTPMOD, /* type */
610 2, /* size (0 = byte, 1 = short, 2 = long) */
612 FALSE, /* pc_relative */
614 complain_overflow_bitfield, /* complain_on_overflow */
615 bfd_elf_generic_reloc, /* special_function */
616 "R_METAG_TLS_DTPMOD", /* name */
617 FALSE, /* partial_inplace */
619 0xffffffff, /* dst_mask */
620 FALSE), /* pcrel_offset */
622 HOWTO (R_METAG_TLS_DTPOFF, /* type */
624 2, /* size (0 = byte, 1 = short, 2 = long) */
626 FALSE, /* pc_relative */
628 complain_overflow_bitfield, /* complain_on_overflow */
629 bfd_elf_generic_reloc, /* special_function */
630 "R_METAG_TLS_DTPOFF", /* name */
631 FALSE, /* partial_inplace */
633 0xffffffff, /* dst_mask */
634 FALSE), /* pcrel_offset */
636 /* Dummy reloc used by the linker internally. */
637 HOWTO (R_METAG_TLS_LE, /* type */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
641 FALSE, /* pc_relative */
643 complain_overflow_bitfield, /* complain_on_overflow */
644 bfd_elf_generic_reloc, /* special_function */
645 "R_METAG_TLS_LE", /* name */
646 FALSE, /* partial_inplace */
648 0xffffffff, /* dst_mask */
649 FALSE), /* pcrel_offset */
651 HOWTO (R_METAG_TLS_LE_HI16, /* type */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
655 FALSE, /* pc_relative */
657 complain_overflow_dont, /* complain_on_overflow */
658 bfd_elf_generic_reloc, /* special_function */
659 "R_METAG_TLS_LE_HI16", /* name */
660 FALSE, /* partial_inplace */
662 0x0007fff8, /* dst_mask */
663 FALSE), /* pcrel_offset */
665 HOWTO (R_METAG_TLS_LE_LO16, /* type */
667 2, /* size (0 = byte, 1 = short, 2 = long) */
669 FALSE, /* pc_relative */
671 complain_overflow_dont, /* complain_on_overflow */
672 bfd_elf_generic_reloc, /* special_function */
673 "R_METAG_TLS_LE_LO16", /* name */
674 FALSE, /* partial_inplace */
676 0x0007fff8, /* dst_mask */
677 FALSE), /* pcrel_offset */
681 #define BRANCH_BITS 19
683 /* The GOT is typically accessed using a [GS]ETD instruction. The size of the
684 immediate offset which can be used in such instructions therefore limits
685 the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
686 is pointing to the base of the GOT then the size is limited to the maximum
687 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
688 in a [GS]ETD instruction is signed, so by setting the base address register
689 to an offset of that 0x2000 byte maximum unsigned offset from the base of
690 the GOT we can use negative offsets in addition to positive. This
691 effectively doubles the usable GOT size to 0x4000 bytes. */
692 #define GOT_REG_OFFSET 0x2000
694 struct metag_reloc_map
696 bfd_reloc_code_real_type bfd_reloc_val;
697 unsigned int metag_reloc_val;
700 static const struct metag_reloc_map metag_reloc_map [] =
702 { BFD_RELOC_NONE, R_METAG_NONE },
703 { BFD_RELOC_32, R_METAG_ADDR32 },
704 { BFD_RELOC_METAG_HIADDR16, R_METAG_HIADDR16 },
705 { BFD_RELOC_METAG_LOADDR16, R_METAG_LOADDR16 },
706 { BFD_RELOC_METAG_RELBRANCH, R_METAG_RELBRANCH },
707 { BFD_RELOC_METAG_GETSETOFF, R_METAG_GETSETOFF },
708 { BFD_RELOC_VTABLE_INHERIT, R_METAG_GNU_VTINHERIT },
709 { BFD_RELOC_VTABLE_ENTRY, R_METAG_GNU_VTENTRY },
710 { BFD_RELOC_METAG_REL8, R_METAG_REL8 },
711 { BFD_RELOC_METAG_REL16, R_METAG_REL16 },
712 { BFD_RELOC_METAG_HI16_GOTOFF, R_METAG_HI16_GOTOFF },
713 { BFD_RELOC_METAG_LO16_GOTOFF, R_METAG_LO16_GOTOFF },
714 { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF },
715 { BFD_RELOC_METAG_GETSET_GOT, R_METAG_GETSET_GOT },
716 { BFD_RELOC_METAG_HI16_GOTPC, R_METAG_HI16_GOTPC },
717 { BFD_RELOC_METAG_LO16_GOTPC, R_METAG_LO16_GOTPC },
718 { BFD_RELOC_METAG_HI16_PLT, R_METAG_HI16_PLT },
719 { BFD_RELOC_METAG_LO16_PLT, R_METAG_LO16_PLT },
720 { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT },
721 { BFD_RELOC_METAG_GOTOFF, R_METAG_GOTOFF },
722 { BFD_RELOC_METAG_PLT, R_METAG_PLT },
723 { BFD_RELOC_METAG_COPY, R_METAG_COPY },
724 { BFD_RELOC_METAG_JMP_SLOT, R_METAG_JMP_SLOT },
725 { BFD_RELOC_METAG_RELATIVE, R_METAG_RELATIVE },
726 { BFD_RELOC_METAG_GLOB_DAT, R_METAG_GLOB_DAT },
727 { BFD_RELOC_METAG_TLS_GD, R_METAG_TLS_GD },
728 { BFD_RELOC_METAG_TLS_LDM, R_METAG_TLS_LDM },
729 { BFD_RELOC_METAG_TLS_LDO_HI16, R_METAG_TLS_LDO_HI16 },
730 { BFD_RELOC_METAG_TLS_LDO_LO16, R_METAG_TLS_LDO_LO16 },
731 { BFD_RELOC_METAG_TLS_LDO, R_METAG_TLS_LDO },
732 { BFD_RELOC_METAG_TLS_IE, R_METAG_TLS_IE },
733 { BFD_RELOC_METAG_TLS_IENONPIC, R_METAG_TLS_IENONPIC },
734 { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 },
735 { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 },
736 { BFD_RELOC_METAG_TLS_TPOFF, R_METAG_TLS_TPOFF },
737 { BFD_RELOC_METAG_TLS_DTPMOD, R_METAG_TLS_DTPMOD },
738 { BFD_RELOC_METAG_TLS_DTPOFF, R_METAG_TLS_DTPOFF },
739 { BFD_RELOC_METAG_TLS_LE, R_METAG_TLS_LE },
740 { BFD_RELOC_METAG_TLS_LE_HI16, R_METAG_TLS_LE_HI16 },
741 { BFD_RELOC_METAG_TLS_LE_LO16, R_METAG_TLS_LE_LO16 },
744 enum elf_metag_stub_type
746 metag_stub_long_branch,
747 metag_stub_long_branch_shared,
751 struct elf_metag_stub_hash_entry
753 /* Base hash table entry structure. */
754 struct bfd_hash_entry bh_root;
756 /* The stub section. */
759 /* Offset within stub_sec of the beginning of this stub. */
762 /* Given the symbol's value and its section we can determine its final
763 value when building the stubs (so the stub knows where to jump. */
764 bfd_vma target_value;
765 asection *target_section;
767 enum elf_metag_stub_type stub_type;
769 /* The symbol table entry, if any, that this was derived from. */
770 struct elf_metag_link_hash_entry *hh;
772 /* And the reloc addend that this was derived from. */
775 /* Where this stub is being called from, or, in the case of combined
776 stub sections, the first input section in the group. */
780 struct elf_metag_link_hash_entry
782 struct elf_link_hash_entry eh;
784 /* A pointer to the most recently used stub hash entry against this
786 struct elf_metag_stub_hash_entry *hsh_cache;
788 /* Used to count relocations for delayed sizing of relocation
790 struct elf_dyn_relocs *dyn_relocs;
794 GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8
798 struct elf_metag_link_hash_table
800 /* The main hash table. */
801 struct elf_link_hash_table etab;
803 /* The stub hash table. */
804 struct bfd_hash_table bstab;
806 /* Linker stub bfd. */
809 /* Linker call-backs. */
810 asection * (*add_stub_section) (const char *, asection *);
811 void (*layout_sections_again) (void);
813 /* Array to keep track of which stub sections have been created, and
814 information on stub grouping. */
817 /* This is the section to which stubs in the group will be
820 /* The stub section. */
824 /* Assorted information used by elf_metag_size_stubs. */
825 unsigned int bfd_count;
826 unsigned int top_index;
827 asection **input_list;
828 Elf_Internal_Sym **all_local_syms;
830 /* Small local sym cache. */
831 struct sym_cache sym_cache;
833 /* Data for LDM relocations. */
836 bfd_signed_vma refcount;
841 /* Return the base vma address which should be subtracted from the
842 real address when resolving a dtpoff relocation. This is PT_TLS
845 dtpoff_base (struct bfd_link_info *info)
847 /* If tls_sec is NULL, we should have signalled an error already. */
848 if (elf_hash_table (info)->tls_sec == NULL)
850 return elf_hash_table (info)->tls_sec->vma;
853 /* Return the relocation value for R_METAG_TLS_IE */
855 tpoff (struct bfd_link_info *info, bfd_vma address)
857 /* If tls_sec is NULL, we should have signalled an error already. */
858 if (elf_hash_table (info)->tls_sec == NULL)
860 /* METAG TLS ABI is variant I and static TLS blocks start just after
861 tcbhead structure which has 2 pointer fields. */
862 return (address - elf_hash_table (info)->tls_sec->vma
863 + align_power ((bfd_vma) 8,
864 elf_hash_table (info)->tls_sec->alignment_power));
868 metag_info_to_howto_rela (bfd *abfd,
870 Elf_Internal_Rela *dst)
874 r_type = ELF32_R_TYPE (dst->r_info);
875 if (r_type >= (unsigned int) R_METAG_MAX)
877 /* xgettext:c-format */
878 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
882 cache_ptr->howto = & elf_metag_howto_table [r_type];
885 static reloc_howto_type *
886 metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
887 bfd_reloc_code_real_type code)
891 for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++)
892 if (metag_reloc_map [i].bfd_reloc_val == code)
893 return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val];
898 static reloc_howto_type *
899 metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
904 for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++)
905 if (elf_metag_howto_table[i].name != NULL
906 && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0)
907 return &elf_metag_howto_table[i];
912 /* Various hash macros and functions. */
913 #define metag_link_hash_table(p) \
914 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
915 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
917 #define metag_elf_hash_entry(ent) \
918 ((struct elf_metag_link_hash_entry *)(ent))
920 #define metag_stub_hash_entry(ent) \
921 ((struct elf_metag_stub_hash_entry *)(ent))
923 #define metag_stub_hash_lookup(table, string, create, copy) \
924 ((struct elf_metag_stub_hash_entry *) \
925 bfd_hash_lookup ((table), (string), (create), (copy)))
927 #define metag_elf_local_got_tls_type(abfd) \
928 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
930 /* Assorted hash table functions. */
932 /* Initialize an entry in the stub hash table. */
934 static struct bfd_hash_entry *
935 stub_hash_newfunc (struct bfd_hash_entry *entry,
936 struct bfd_hash_table *table,
939 /* Allocate the structure if it has not already been allocated by a
943 entry = bfd_hash_allocate (table,
944 sizeof (struct elf_metag_stub_hash_entry));
949 /* Call the allocation method of the superclass. */
950 entry = bfd_hash_newfunc (entry, table, string);
953 struct elf_metag_stub_hash_entry *hsh;
955 /* Initialize the local fields. */
956 hsh = (struct elf_metag_stub_hash_entry *) entry;
957 hsh->stub_sec = NULL;
958 hsh->stub_offset = 0;
959 hsh->target_value = 0;
960 hsh->target_section = NULL;
961 hsh->stub_type = metag_stub_long_branch;
969 /* Initialize an entry in the link hash table. */
971 static struct bfd_hash_entry *
972 metag_link_hash_newfunc (struct bfd_hash_entry *entry,
973 struct bfd_hash_table *table,
976 /* Allocate the structure if it has not already been allocated by a
980 entry = bfd_hash_allocate (table,
981 sizeof (struct elf_metag_link_hash_entry));
986 /* Call the allocation method of the superclass. */
987 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
990 struct elf_metag_link_hash_entry *hh;
992 /* Initialize the local fields. */
993 hh = (struct elf_metag_link_hash_entry *) entry;
994 hh->hsh_cache = NULL;
995 hh->dyn_relocs = NULL;
996 hh->tls_type = GOT_UNKNOWN;
1002 /* Free the derived linker hash table. */
1005 elf_metag_link_hash_table_free (bfd *obfd)
1007 struct elf_metag_link_hash_table *htab
1008 = (struct elf_metag_link_hash_table *) obfd->link.hash;
1010 bfd_hash_table_free (&htab->bstab);
1011 _bfd_elf_link_hash_table_free (obfd);
1014 /* Create the derived linker hash table. The Meta ELF port uses the derived
1015 hash table to keep information specific to the Meta ELF linker (without
1016 using static variables). */
1018 static struct bfd_link_hash_table *
1019 elf_metag_link_hash_table_create (bfd *abfd)
1021 struct elf_metag_link_hash_table *htab;
1022 bfd_size_type amt = sizeof (*htab);
1024 htab = bfd_zmalloc (amt);
1028 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
1029 metag_link_hash_newfunc,
1030 sizeof (struct elf_metag_link_hash_entry),
1037 /* Init the stub hash table too. */
1038 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
1039 sizeof (struct elf_metag_stub_hash_entry)))
1041 _bfd_elf_link_hash_table_free (abfd);
1044 htab->etab.root.hash_table_free = elf_metag_link_hash_table_free;
1046 return &htab->etab.root;
1049 /* Section name for stubs is the associated section name plus this
1051 #define STUB_SUFFIX ".stub"
1053 /* Build a name for an entry in the stub hash table. */
1056 metag_stub_name (const asection *input_section,
1057 const asection *sym_sec,
1058 const struct elf_metag_link_hash_entry *hh,
1059 const Elf_Internal_Rela *rel)
1066 len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1;
1067 stub_name = bfd_malloc (len);
1068 if (stub_name != NULL)
1070 sprintf (stub_name, "%08x_%s+%x",
1071 input_section->id & 0xffffffff,
1072 hh->eh.root.root.string,
1073 (int) rel->r_addend & 0xffffffff);
1078 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1079 stub_name = bfd_malloc (len);
1080 if (stub_name != NULL)
1082 sprintf (stub_name, "%08x_%x:%x+%x",
1083 input_section->id & 0xffffffff,
1084 sym_sec->id & 0xffffffff,
1085 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1086 (int) rel->r_addend & 0xffffffff);
1092 /* Look up an entry in the stub hash. Stub entries are cached because
1093 creating the stub name takes a bit of time. */
1095 static struct elf_metag_stub_hash_entry *
1096 metag_get_stub_entry (const asection *input_section,
1097 const asection *sym_sec,
1098 struct elf_metag_link_hash_entry *hh,
1099 const Elf_Internal_Rela *rel,
1100 struct elf_metag_link_hash_table *htab)
1102 struct elf_metag_stub_hash_entry *hsh;
1103 const asection *id_sec;
1105 /* If this input section is part of a group of sections sharing one
1106 stub section, then use the id of the first section in the group.
1107 Stub names need to include a section id, as there may well be
1108 more than one stub used to reach say, printf, and we need to
1109 distinguish between them. */
1110 id_sec = htab->stub_group[input_section->id].link_sec;
1112 if (hh != NULL && hh->hsh_cache != NULL
1113 && hh->hsh_cache->hh == hh
1114 && hh->hsh_cache->id_sec == id_sec)
1116 hsh = hh->hsh_cache;
1122 stub_name = metag_stub_name (id_sec, sym_sec, hh, rel);
1123 if (stub_name == NULL)
1126 hsh = metag_stub_hash_lookup (&htab->bstab,
1127 stub_name, FALSE, FALSE);
1130 hh->hsh_cache = hsh;
1138 /* Add a new stub entry to the stub hash. Not all fields of the new
1139 stub entry are initialised. */
1141 static struct elf_metag_stub_hash_entry *
1142 metag_add_stub (const char *stub_name,
1144 struct elf_metag_link_hash_table *htab)
1148 struct elf_metag_stub_hash_entry *hsh;
1150 link_sec = htab->stub_group[section->id].link_sec;
1151 stub_sec = htab->stub_group[section->id].stub_sec;
1152 if (stub_sec == NULL)
1154 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1155 if (stub_sec == NULL)
1161 namelen = strlen (link_sec->name);
1162 len = namelen + sizeof (STUB_SUFFIX);
1163 s_name = bfd_alloc (htab->stub_bfd, len);
1167 memcpy (s_name, link_sec->name, namelen);
1168 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1170 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1171 if (stub_sec == NULL)
1173 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1175 htab->stub_group[section->id].stub_sec = stub_sec;
1178 /* Enter this entry into the linker stub hash table. */
1179 hsh = metag_stub_hash_lookup (&htab->bstab, stub_name,
1183 /* xgettext:c-format */
1184 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
1185 section->owner, stub_name);
1189 hsh->stub_sec = stub_sec;
1190 hsh->stub_offset = 0;
1191 hsh->id_sec = link_sec;
1195 /* Check a signed integer value can be represented in the given number
1199 within_signed_range (int value, unsigned int bits)
1201 int min_val = -(1 << (bits - 1));
1202 int max_val = (1 << (bits - 1)) - 1;
1203 return (value <= max_val) && (value >= min_val);
1206 /* Perform a relocation as part of a final link. */
1208 static bfd_reloc_status_type
1209 metag_final_link_relocate (reloc_howto_type *howto,
1211 asection *input_section,
1213 Elf_Internal_Rela *rel,
1215 struct elf_metag_link_hash_entry *hh,
1216 struct elf_metag_link_hash_table *htab,
1219 bfd_reloc_status_type r = bfd_reloc_ok;
1220 bfd_byte *hit_data = contents + rel->r_offset;
1221 int opcode, op_shift, op_extended, l1, l2;
1222 bfd_signed_vma srel, addend = rel->r_addend;
1223 struct elf_metag_stub_hash_entry *hsh = NULL;
1226 /* Find out where we are and where we're going. */
1227 location = (rel->r_offset +
1228 input_section->output_offset +
1229 input_section->output_section->vma);
1231 switch (howto->type)
1233 case R_METAG_RELBRANCH:
1234 case R_METAG_RELBRANCH_PLT:
1235 /* Make it a pc relative offset. */
1236 relocation -= location;
1238 case R_METAG_TLS_GD:
1239 case R_METAG_TLS_IE:
1240 relocation -= elf_gp (input_section->output_section->owner);
1246 switch (howto->type)
1248 case R_METAG_RELBRANCH_PLT:
1249 case R_METAG_RELBRANCH:
1250 opcode = bfd_get_32 (input_bfd, hit_data);
1252 srel = (bfd_signed_vma) relocation;
1255 /* If the branch is out of reach, then redirect the
1256 call to the local stub for this function. */
1257 if (srel > ((1 << (BRANCH_BITS + 1)) - 1) ||
1258 (srel < - (1 << (BRANCH_BITS + 1))))
1260 if (sym_sec == NULL)
1263 hsh = metag_get_stub_entry (input_section, sym_sec,
1266 return bfd_reloc_undefined;
1268 /* Munge up the value and addend so that we call the stub
1269 rather than the procedure directly. */
1270 srel = (hsh->stub_offset
1271 + hsh->stub_sec->output_offset
1272 + hsh->stub_sec->output_section->vma);
1278 if (!within_signed_range (srel, BRANCH_BITS))
1280 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1283 return bfd_reloc_overflow;
1286 opcode &= ~(0x7ffff << 5);
1287 opcode |= ((srel & 0x7ffff) << 5);
1289 bfd_put_32 (input_bfd, opcode, hit_data);
1291 case R_METAG_GETSETOFF:
1292 case R_METAG_GETSET_GOT:
1293 case R_METAG_GETSET_GOTOFF:
1294 opcode = bfd_get_32 (input_bfd, hit_data);
1296 srel = (bfd_signed_vma) relocation;
1299 /* Is this a standard or extended GET/SET? */
1300 if ((opcode & 0xf0000000) == 0xa0000000)
1302 /* Extended GET/SET. */
1309 /* Standard GET/SET. */
1310 l1 = opcode & 0x01000000;
1311 l2 = opcode & 0x04000000;
1315 /* Calculate the width of the GET/SET and how much we need to
1316 shift the result by. */
1328 /* GET/SET offsets are scaled by the width of the transfer. */
1329 srel = srel >> op_shift;
1331 /* Extended GET/SET has signed 12 bits of offset, standard has
1335 if (!within_signed_range (srel, 12))
1337 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1340 return bfd_reloc_overflow;
1342 opcode &= ~(0xfff << 7);
1343 opcode |= ((srel & 0xfff) << 7);
1347 if (!within_signed_range (srel, 5))
1349 if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1352 return bfd_reloc_overflow;
1354 opcode &= ~(0x3f << 8);
1355 opcode |= ((srel & 0x3f) << 8);
1358 bfd_put_32 (input_bfd, opcode, hit_data);
1360 case R_METAG_TLS_GD:
1361 case R_METAG_TLS_LDM:
1362 opcode = bfd_get_32 (input_bfd, hit_data);
1364 if ((bfd_signed_vma)relocation < 0)
1366 /* sign extend immediate */
1367 if ((opcode & 0xf2000001) == 0x02000000)
1369 /* ADD De.e,Dx.r,#I16 */
1373 return bfd_reloc_overflow;
1376 bfd_put_32 (input_bfd, opcode, hit_data);
1378 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1379 contents, rel->r_offset,
1380 relocation, rel->r_addend);
1383 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1384 contents, rel->r_offset,
1385 relocation, rel->r_addend);
1391 /* This is defined because R_METAG_NONE != 0...
1392 See RELOC_AGAINST_DISCARDED_SECTION for details. */
1393 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1394 rel, relend, howto, contents) \
1396 _bfd_clear_contents (howto, input_bfd, input_section, \
1397 contents + rel->r_offset); \
1399 if (bfd_link_relocatable (info) \
1400 && (input_section->flags & SEC_DEBUGGING)) \
1402 /* Only remove relocations in debug sections since other \
1403 sections may require relocations. */ \
1404 Elf_Internal_Shdr *rel_hdr; \
1406 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1408 /* Avoid empty output section. */ \
1409 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
1411 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1412 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
1413 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1415 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
1417 input_section->reloc_count--; \
1424 rel->r_info = R_METAG_NONE; \
1425 rel->r_addend = 0; \
1429 /* Relocate a META ELF section.
1431 The RELOCATE_SECTION function is called by the new ELF backend linker
1432 to handle the relocations for a section.
1434 The relocs are always passed as Rela structures; if the section
1435 actually uses Rel structures, the r_addend field will always be
1438 This function is responsible for adjusting the section contents as
1439 necessary, and (if using Rela relocs and generating a relocatable
1440 output file) adjusting the reloc addend as necessary.
1442 This function does not have to worry about setting the reloc
1443 address or the reloc symbol index.
1445 LOCAL_SYMS is a pointer to the swapped in local symbols.
1447 LOCAL_SECTIONS is an array giving the section in the input file
1448 corresponding to the st_shndx field of each local symbol.
1450 The global hash table entry for the global symbols can be found
1451 via elf_sym_hashes (input_bfd).
1453 When generating relocatable output, this function must handle
1454 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1455 going to be the section symbol corresponding to the output
1456 section, which means that the addend must be adjusted
1460 elf_metag_relocate_section (bfd *output_bfd,
1461 struct bfd_link_info *info,
1463 asection *input_section,
1465 Elf_Internal_Rela *relocs,
1466 Elf_Internal_Sym *local_syms,
1467 asection **local_sections)
1469 bfd_vma *local_got_offsets;
1470 Elf_Internal_Shdr *symtab_hdr;
1471 struct elf_link_hash_entry **eh_syms;
1472 struct elf_metag_link_hash_table *htab;
1473 Elf_Internal_Rela *rel;
1474 Elf_Internal_Rela *relend;
1477 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1478 eh_syms = elf_sym_hashes (input_bfd);
1479 relend = relocs + input_section->reloc_count;
1481 htab = metag_link_hash_table (info);
1482 local_got_offsets = elf_local_got_offsets (input_bfd);
1486 for (rel = relocs; rel < relend; rel ++)
1488 reloc_howto_type *howto;
1489 unsigned long r_symndx;
1490 Elf_Internal_Sym *sym;
1492 struct elf_metag_link_hash_entry *hh;
1494 bfd_reloc_status_type r;
1498 r_type = ELF32_R_TYPE (rel->r_info);
1500 if (r_type == R_METAG_GNU_VTINHERIT
1501 || r_type == R_METAG_GNU_VTENTRY
1502 || r_type == R_METAG_NONE)
1505 r_symndx = ELF32_R_SYM (rel->r_info);
1507 howto = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info);
1512 if (r_symndx < symtab_hdr->sh_info)
1514 sym = local_syms + r_symndx;
1515 sec = local_sections [r_symndx];
1516 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1518 name = bfd_elf_string_from_elf_section
1519 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1520 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1524 struct elf_link_hash_entry *eh;
1525 bfd_boolean unresolved_reloc, warned, ignored;
1527 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1528 r_symndx, symtab_hdr, eh_syms,
1529 eh, sec, relocation,
1530 unresolved_reloc, warned, ignored);
1532 name = eh->root.root.string;
1533 hh = (struct elf_metag_link_hash_entry *) eh;
1536 if (sec != NULL && discarded_section (sec))
1537 METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1538 rel, relend, howto, contents);
1540 if (bfd_link_relocatable (info))
1545 case R_METAG_ADDR32:
1546 case R_METAG_RELBRANCH:
1547 if ((input_section->flags & SEC_ALLOC) == 0)
1550 if ((bfd_link_pic (info)
1551 && r_symndx != STN_UNDEF
1552 && (input_section->flags & SEC_ALLOC) != 0
1553 && (r_type != R_METAG_RELBRANCH
1554 || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
1555 || (!bfd_link_pic (info)
1557 && hh->eh.dynindx != -1
1558 && !hh->eh.non_got_ref
1559 && ((hh->eh.def_dynamic
1560 && !hh->eh.def_regular)
1561 || hh->eh.root.type == bfd_link_hash_undefweak
1562 || hh->eh.root.type == bfd_link_hash_undefined)))
1564 Elf_Internal_Rela outrel;
1565 bfd_boolean skip, relocate;
1568 /* When generating a shared object, these relocations
1569 are copied into the output file to be resolved at run
1572 sreloc = elf_section_data (input_section)->sreloc;
1573 BFD_ASSERT (sreloc != NULL);
1578 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1582 if (outrel.r_offset == (bfd_vma) -1)
1584 else if (outrel.r_offset == (bfd_vma) -2)
1585 skip = TRUE, relocate = TRUE;
1586 outrel.r_offset += (input_section->output_section->vma
1587 + input_section->output_offset);
1591 memset (&outrel, 0, sizeof outrel);
1592 outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE);
1594 else if (r_type == R_METAG_RELBRANCH)
1596 BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1);
1597 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1598 outrel.r_addend = rel->r_addend;
1602 /* h->dynindx may be -1 if this symbol was marked to
1605 || ((info->symbolic || hh->eh.dynindx == -1)
1606 && hh->eh.def_regular))
1609 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1610 outrel.r_addend = relocation + rel->r_addend;
1614 BFD_ASSERT (hh->eh.dynindx != -1);
1615 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1616 outrel.r_addend = rel->r_addend;
1620 loc = sreloc->contents;
1621 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
1622 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
1623 ++sreloc->reloc_count;
1625 /* If this reloc is against an external symbol, we do
1626 not want to fiddle with the addend. Otherwise, we
1627 need to include the symbol value so that it becomes
1628 an addend for the dynamic reloc. */
1634 case R_METAG_RELBRANCH_PLT:
1635 /* Relocation is to the entry for this symbol in the
1636 procedure linkage table. */
1641 if (hh->eh.forced_local)
1644 if (hh->eh.plt.offset == (bfd_vma) -1 || htab->etab.splt == NULL)
1646 /* We didn't make a PLT entry for this symbol. This
1647 happens when statically linking PIC code, or when
1648 using -Bsymbolic. */
1652 relocation = (htab->etab.splt->output_section->vma
1653 + htab->etab.splt->output_offset
1654 + hh->eh.plt.offset);
1656 case R_METAG_HI16_GOTPC:
1657 case R_METAG_LO16_GOTPC:
1658 BFD_ASSERT (htab->etab.sgot != NULL);
1660 relocation = (htab->etab.sgot->output_section->vma +
1661 htab->etab.sgot->output_offset);
1662 relocation += GOT_REG_OFFSET;
1663 relocation -= (input_section->output_section->vma
1664 + input_section->output_offset
1667 case R_METAG_HI16_GOTOFF:
1668 case R_METAG_LO16_GOTOFF:
1669 case R_METAG_GETSET_GOTOFF:
1670 BFD_ASSERT (htab->etab.sgot != NULL);
1672 relocation -= (htab->etab.sgot->output_section->vma +
1673 htab->etab.sgot->output_offset);
1674 relocation -= GOT_REG_OFFSET;
1676 case R_METAG_GETSET_GOT:
1679 bfd_boolean do_got = 0;
1681 /* Relocation is to the entry for this symbol in the
1682 global offset table. */
1687 off = hh->eh.got.offset;
1688 dyn = htab->etab.dynamic_sections_created;
1689 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1690 bfd_link_pic (info),
1693 /* If we aren't going to call finish_dynamic_symbol,
1694 then we need to handle initialisation of the .got
1695 entry and create needed relocs here. Since the
1696 offset must always be a multiple of 4, we use the
1697 least significant bit to record whether we have
1698 initialised it already. */
1703 hh->eh.got.offset |= 1;
1710 /* Local symbol case. */
1711 if (local_got_offsets == NULL)
1714 off = local_got_offsets[r_symndx];
1716 /* The offset must always be a multiple of 4. We use
1717 the least significant bit to record whether we have
1718 already generated the necessary reloc. */
1723 local_got_offsets[r_symndx] |= 1;
1730 if (bfd_link_pic (info))
1732 /* Output a dynamic relocation for this GOT entry.
1733 In this case it is relative to the base of the
1734 object because the symbol index is zero. */
1735 Elf_Internal_Rela outrel;
1737 asection *s = htab->etab.srelgot;
1739 outrel.r_offset = (off
1740 + htab->etab.sgot->output_offset
1741 + htab->etab.sgot->output_section->vma);
1742 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1743 outrel.r_addend = relocation;
1745 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1746 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1749 bfd_put_32 (output_bfd, relocation,
1750 htab->etab.sgot->contents + off);
1753 if (off >= (bfd_vma) -2)
1756 relocation = off - GOT_REG_OFFSET;
1759 case R_METAG_TLS_GD:
1760 case R_METAG_TLS_IE:
1762 /* XXXMJF There is room here for optimisations. For example
1763 converting from GD->IE, etc. */
1768 if (htab->etab.sgot == NULL)
1775 dyn = htab->etab.dynamic_sections_created;
1777 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1778 bfd_link_pic (info),
1780 && (!bfd_link_pic (info)
1781 || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
1783 indx = hh->eh.dynindx;
1785 off = hh->eh.got.offset;
1786 tls_type = hh->tls_type;
1790 /* Local symbol case. */
1791 if (local_got_offsets == NULL)
1794 off = local_got_offsets[r_symndx];
1795 tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx];
1798 if (tls_type == GOT_UNKNOWN)
1805 bfd_boolean need_relocs = FALSE;
1806 Elf_Internal_Rela outrel;
1807 bfd_byte *loc = NULL;
1810 /* The GOT entries have not been initialized yet. Do it
1811 now, and emit any relocations. If both an IE GOT and a
1812 GD GOT are necessary, we emit the GD first. */
1814 if ((bfd_link_pic (info) || indx != 0)
1816 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
1817 || hh->eh.root.type != bfd_link_hash_undefweak))
1820 loc = htab->etab.srelgot->contents;
1821 /* FIXME (CAO): Should this be reloc_count++ ? */
1822 loc += htab->etab.srelgot->reloc_count * sizeof (Elf32_External_Rela);
1825 if (tls_type & GOT_TLS_GD)
1829 outrel.r_offset = (cur_off
1830 + htab->etab.sgot->output_section->vma
1831 + htab->etab.sgot->output_offset);
1832 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD);
1833 outrel.r_addend = 0;
1834 bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + cur_off);
1836 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1837 htab->etab.srelgot->reloc_count++;
1838 loc += sizeof (Elf32_External_Rela);
1841 bfd_put_32 (output_bfd, 0,
1842 htab->etab.sgot->contents + cur_off + 4);
1845 bfd_put_32 (output_bfd, 0,
1846 htab->etab.sgot->contents + cur_off + 4);
1847 outrel.r_info = ELF32_R_INFO (indx,
1848 R_METAG_TLS_DTPOFF);
1849 outrel.r_offset += 4;
1850 bfd_elf32_swap_reloca_out (output_bfd,
1852 htab->etab.srelgot->reloc_count++;
1853 loc += sizeof (Elf32_External_Rela);
1858 /* We don't support changing the TLS model. */
1860 if (bfd_link_pic (info))
1861 _bfd_error_handler (_("%pB(%pA): multiple TLS models are not supported"),
1862 input_bfd, input_section);
1864 _bfd_error_handler (_("%pB(%pA): shared library symbol %s encountered whilst performing a static link"),
1865 input_bfd, input_section, name);
1872 if (tls_type & GOT_TLS_IE)
1876 outrel.r_offset = (cur_off
1877 + htab->etab.sgot->output_section->vma
1878 + htab->etab.sgot->output_offset);
1879 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF);
1882 outrel.r_addend = relocation - dtpoff_base (info);
1884 outrel.r_addend = 0;
1886 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1887 htab->etab.srelgot->reloc_count++;
1888 loc += sizeof (Elf32_External_Rela);
1891 bfd_put_32 (output_bfd, tpoff (info, relocation),
1892 htab->etab.sgot->contents + cur_off);
1898 hh->eh.got.offset |= 1;
1900 local_got_offsets[r_symndx] |= 1;
1903 /* Add the base of the GOT to the relocation value. */
1904 relocation = off - GOT_REG_OFFSET;
1909 case R_METAG_TLS_IENONPIC_HI16:
1910 case R_METAG_TLS_IENONPIC_LO16:
1911 case R_METAG_TLS_LE_HI16:
1912 case R_METAG_TLS_LE_LO16:
1913 if (bfd_link_pic (info))
1916 /* xgettext:c-format */
1917 (_("%pB(%pA+%#" PRIx64 "): "
1918 "%s relocation not permitted in shared object"),
1919 input_bfd, input_section, (uint64_t) rel->r_offset,
1924 relocation = tpoff (info, relocation);
1926 case R_METAG_TLS_LDO_HI16:
1927 case R_METAG_TLS_LDO_LO16:
1928 if (! bfd_link_pic (info))
1929 relocation = tpoff (info, relocation);
1931 relocation -= dtpoff_base (info);
1933 case R_METAG_TLS_LDM:
1937 if (htab->etab.sgot == NULL)
1939 off = htab->tls_ldm_got.offset;
1944 Elf_Internal_Rela outrel;
1947 outrel.r_offset = (off
1948 + htab->etab.sgot->output_section->vma
1949 + htab->etab.sgot->output_offset);
1951 outrel.r_addend = 0;
1952 outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD);
1953 loc = htab->etab.srelgot->contents;
1954 loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1955 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1956 htab->tls_ldm_got.offset |= 1;
1959 relocation = off - GOT_REG_OFFSET;
1966 r = metag_final_link_relocate (howto, input_bfd, input_section,
1967 contents, rel, relocation, hh, htab,
1970 if (r != bfd_reloc_ok)
1972 const char * msg = (const char *) NULL;
1976 case bfd_reloc_overflow:
1977 (*info->callbacks->reloc_overflow)
1978 (info, (hh ? &hh->eh.root : NULL), name, howto->name,
1979 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1982 case bfd_reloc_undefined:
1983 (*info->callbacks->undefined_symbol)
1984 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1987 case bfd_reloc_outofrange:
1988 msg = _("internal error: out of range error");
1991 case bfd_reloc_notsupported:
1992 msg = _("internal error: unsupported relocation error");
1995 case bfd_reloc_dangerous:
1996 msg = _("internal error: dangerous relocation");
2000 msg = _("internal error: unknown error");
2005 (*info->callbacks->warning) (info, msg, name, input_bfd,
2006 input_section, rel->r_offset);
2013 /* Create the .plt and .got sections, and set up our hash table
2014 short-cuts to various dynamic sections. */
2017 elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2019 struct elf_metag_link_hash_table *htab;
2020 struct elf_link_hash_entry *eh;
2021 struct bfd_link_hash_entry *bh;
2022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2024 /* Don't try to create the .plt and .got twice. */
2025 htab = metag_link_hash_table (info);
2026 if (htab->etab.splt != NULL)
2029 /* Call the generic code to do most of the work. */
2030 if (! _bfd_elf_create_dynamic_sections (abfd, info))
2033 /* The header goes at the start of the dynamic .got section, which
2034 is placed after the dynamic .got.plt section. ie. The header is
2035 not necessarily at the start of the output .got section. */
2036 htab->etab.sgot->size += 12;
2038 /* Define the symbol __GLOBAL_OFFSET_TABLE__ on the header. */
2040 if (!(_bfd_generic_link_add_one_symbol
2041 (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->etab.sgot,
2042 (bfd_vma) 0, NULL, FALSE, bed->collect, &bh)))
2044 eh = (struct elf_link_hash_entry *) bh;
2045 eh->def_regular = 1;
2046 eh->type = STT_OBJECT;
2047 eh->other = STV_HIDDEN;
2049 if (! bfd_link_executable (info)
2050 && ! bfd_elf_link_record_dynamic_symbol (info, eh))
2053 htab->etab.hgot = eh;
2058 /* Look through the relocs for a section during the first phase, and
2059 calculate needed space in the global offset table, procedure linkage
2060 table, and dynamic reloc sections. At this point we haven't
2061 necessarily read all the input files. */
2064 elf_metag_check_relocs (bfd *abfd,
2065 struct bfd_link_info *info,
2067 const Elf_Internal_Rela *relocs)
2069 Elf_Internal_Shdr *symtab_hdr;
2070 struct elf_link_hash_entry **eh_syms;
2071 const Elf_Internal_Rela *rel;
2072 const Elf_Internal_Rela *rel_end;
2073 struct elf_metag_link_hash_table *htab;
2076 int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
2078 if (bfd_link_relocatable (info))
2081 htab = metag_link_hash_table (info);
2082 dynobj = htab->etab.dynobj;
2083 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2084 eh_syms = elf_sym_hashes (abfd);
2090 rel_end = relocs + sec->reloc_count;
2091 for (rel = relocs; rel < rel_end; rel++)
2094 struct elf_metag_link_hash_entry *hh;
2095 Elf_Internal_Sym *isym;
2096 unsigned long r_symndx;
2098 r_symndx = ELF32_R_SYM (rel->r_info);
2099 r_type = ELF32_R_TYPE (rel->r_info);
2100 if (r_symndx < symtab_hdr->sh_info)
2102 /* A local symbol. */
2103 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2114 hh = (struct elf_metag_link_hash_entry *)
2115 eh_syms[r_symndx - symtab_hdr->sh_info];
2116 while (hh->eh.root.type == bfd_link_hash_indirect
2117 || hh->eh.root.type == bfd_link_hash_warning)
2118 hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link;
2121 /* Some relocs require a global offset table. */
2122 if (htab->etab.sgot == NULL)
2126 case R_METAG_TLS_GD:
2127 case R_METAG_TLS_LDM:
2128 case R_METAG_TLS_IE:
2129 if (bfd_link_pic (info))
2130 info->flags |= DF_STATIC_TLS;
2133 case R_METAG_HI16_GOTOFF:
2134 case R_METAG_LO16_GOTOFF:
2135 case R_METAG_GETSET_GOTOFF:
2136 case R_METAG_GETSET_GOT:
2137 case R_METAG_HI16_GOTPC:
2138 case R_METAG_LO16_GOTPC:
2140 htab->etab.dynobj = dynobj = abfd;
2141 if (!elf_metag_create_dynamic_sections (dynobj, info))
2152 case R_METAG_TLS_IE:
2153 case R_METAG_TLS_GD:
2154 case R_METAG_GETSET_GOT:
2158 tls_type = GOT_NORMAL;
2160 case R_METAG_TLS_IE:
2161 tls_type = GOT_TLS_IE;
2163 case R_METAG_TLS_GD:
2164 tls_type = GOT_TLS_GD;
2170 hh->eh.got.refcount += 1;
2171 old_tls_type = hh->tls_type;
2175 bfd_signed_vma *local_got_refcounts;
2177 /* This is a global offset table entry for a local
2179 local_got_refcounts = elf_local_got_refcounts (abfd);
2180 if (local_got_refcounts == NULL)
2184 size = symtab_hdr->sh_info;
2185 size *= sizeof (bfd_signed_vma);
2186 /* Add in space to store the local GOT TLS types. */
2187 size += symtab_hdr->sh_info;
2188 local_got_refcounts = ((bfd_signed_vma *)
2189 bfd_zalloc (abfd, size));
2190 if (local_got_refcounts == NULL)
2192 elf_local_got_refcounts (abfd) = local_got_refcounts;
2193 memset (metag_elf_local_got_tls_type (abfd),
2194 GOT_UNKNOWN, symtab_hdr->sh_info);
2196 local_got_refcounts[r_symndx] += 1;
2197 old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx];
2200 if (old_tls_type != tls_type)
2204 hh->tls_type = tls_type;
2208 metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
2214 case R_METAG_TLS_LDM:
2215 metag_link_hash_table (info)->tls_ldm_got.refcount += 1;
2218 case R_METAG_RELBRANCH_PLT:
2219 /* This symbol requires a procedure linkage table entry. We
2220 actually build the entry in adjust_dynamic_symbol,
2221 because this might be a case of linking PIC code without
2222 linking in any dynamic objects, in which case we don't
2223 need to generate a procedure linkage table after all. */
2225 /* If this is a local symbol, we resolve it directly without
2226 creating a procedure linkage table entry. */
2230 if (hh->eh.forced_local)
2233 hh->eh.needs_plt = 1;
2234 hh->eh.plt.refcount += 1;
2237 case R_METAG_HIADDR16:
2238 case R_METAG_LOADDR16:
2239 /* Let's help debug shared library creation. These relocs
2240 cannot be used in shared libs. Don't error out for
2241 sections we don't care about, such as debug sections or
2242 non-constant sections. */
2243 if (bfd_link_pic (info)
2244 && (sec->flags & SEC_ALLOC) != 0
2245 && (sec->flags & SEC_READONLY) != 0)
2250 name = hh->eh.root.root.string;
2252 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
2254 /* xgettext:c-format */
2255 (_("%pB: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2256 abfd, elf_metag_howto_table[r_type].name, name);
2257 bfd_set_error (bfd_error_bad_value);
2262 case R_METAG_ADDR32:
2263 case R_METAG_RELBRANCH:
2264 case R_METAG_GETSETOFF:
2265 if (hh != NULL && !bfd_link_pic (info))
2267 hh->eh.non_got_ref = 1;
2268 hh->eh.plt.refcount += 1;
2271 /* If we are creating a shared library, and this is a reloc
2272 against a global symbol, or a non PC relative reloc
2273 against a local symbol, then we need to copy the reloc
2274 into the shared library. However, if we are linking with
2275 -Bsymbolic, we do not need to copy a reloc against a
2276 global symbol which is defined in an object we are
2277 including in the link (i.e., DEF_REGULAR is set). At
2278 this point we have not seen all the input files, so it is
2279 possible that DEF_REGULAR is not set now but will be set
2280 later (it is never cleared). We account for that
2281 possibility below by storing information in the
2282 dyn_relocs field of the hash table entry. A similar
2283 situation occurs when creating shared libraries and symbol
2284 visibility changes render the symbol local.
2286 If on the other hand, we are creating an executable, we
2287 may need to keep relocations for symbols satisfied by a
2288 dynamic library if we manage to avoid copy relocs for the
2290 if ((bfd_link_pic (info)
2291 && (sec->flags & SEC_ALLOC) != 0
2292 && (r_type != R_METAG_RELBRANCH
2294 && (! info->symbolic
2295 || hh->eh.root.type == bfd_link_hash_defweak
2296 || !hh->eh.def_regular))))
2297 || (!bfd_link_pic (info)
2298 && (sec->flags & SEC_ALLOC) != 0
2300 && (hh->eh.root.type == bfd_link_hash_defweak
2301 || !hh->eh.def_regular)))
2303 struct elf_dyn_relocs *hdh_p;
2304 struct elf_dyn_relocs **hdh_head;
2307 htab->etab.dynobj = dynobj = abfd;
2309 /* When creating a shared object, we must copy these
2310 relocs into the output file. We create a reloc
2311 section in dynobj and make room for the reloc. */
2314 sreloc = _bfd_elf_make_dynamic_reloc_section
2315 (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE);
2319 bfd_set_error (bfd_error_bad_value);
2323 elf_section_data (sec)->sreloc = sreloc;
2326 /* If this is a global symbol, we count the number of
2327 relocations we need for this symbol. */
2329 hdh_head = &((struct elf_metag_link_hash_entry *) hh)->dyn_relocs;
2332 /* Track dynamic relocs needed for local syms too. */
2336 sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
2340 vpp = &elf_section_data (sr)->local_dynrel;
2341 hdh_head = (struct elf_dyn_relocs **) vpp;
2345 if (hdh_p == NULL || hdh_p->sec != sec)
2347 hdh_p = ((struct elf_dyn_relocs *)
2348 bfd_alloc (dynobj, sizeof *hdh_p));
2351 hdh_p->next = *hdh_head;
2355 hdh_p->pc_count = 0;
2359 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
2360 hdh_p->pc_count += 1;
2364 /* This relocation describes the C++ object vtable hierarchy.
2365 Reconstruct it for later use during GC. */
2366 case R_METAG_GNU_VTINHERIT:
2367 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh,
2372 /* This relocation describes which C++ vtable entries are actually
2373 used. Record for later use during GC. */
2374 case R_METAG_GNU_VTENTRY:
2375 BFD_ASSERT (hh != NULL);
2377 && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend))
2386 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2389 elf_metag_copy_indirect_symbol (struct bfd_link_info *info,
2390 struct elf_link_hash_entry *eh_dir,
2391 struct elf_link_hash_entry *eh_ind)
2393 struct elf_metag_link_hash_entry *hh_dir, *hh_ind;
2395 hh_dir = metag_elf_hash_entry (eh_dir);
2396 hh_ind = metag_elf_hash_entry (eh_ind);
2398 if (hh_ind->dyn_relocs != NULL)
2400 if (hh_dir->dyn_relocs != NULL)
2402 struct elf_dyn_relocs **hdh_pp;
2403 struct elf_dyn_relocs *hdh_p;
2405 if (eh_ind->root.type == bfd_link_hash_indirect)
2408 /* Add reloc counts against the weak sym to the strong sym
2409 list. Merge any entries against the same section. */
2410 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2412 struct elf_dyn_relocs *hdh_q;
2414 for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL;
2415 hdh_q = hdh_q->next)
2416 if (hdh_q->sec == hdh_p->sec)
2418 hdh_q->pc_count += hdh_p->pc_count;
2419 hdh_q->count += hdh_p->count;
2420 *hdh_pp = hdh_p->next;
2424 hdh_pp = &hdh_p->next;
2426 *hdh_pp = hh_dir->dyn_relocs;
2429 hh_dir->dyn_relocs = hh_ind->dyn_relocs;
2430 hh_ind->dyn_relocs = NULL;
2433 if (eh_ind->root.type == bfd_link_hash_indirect
2434 && eh_dir->got.refcount <= 0)
2436 hh_dir->tls_type = hh_ind->tls_type;
2437 hh_ind->tls_type = GOT_UNKNOWN;
2440 _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
2443 /* Find dynamic relocs for H that apply to read-only sections. */
2446 readonly_dynrelocs (struct elf_link_hash_entry *h)
2448 struct elf_dyn_relocs *p;
2450 for (p = metag_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2452 asection *s = p->sec->output_section;
2454 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2460 /* Adjust a symbol defined by a dynamic object and referenced by a
2461 regular object. The current definition is in some section of the
2462 dynamic object, but we're not including those sections. We have to
2463 change the definition to something the rest of the link can
2467 elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info,
2468 struct elf_link_hash_entry *eh)
2470 struct elf_metag_link_hash_table *htab;
2473 /* If this is a function, put it in the procedure linkage table. We
2474 will fill in the contents of the procedure linkage table later,
2475 when we know the address of the .got section. */
2476 if (eh->type == STT_FUNC
2479 if (eh->plt.refcount <= 0
2480 || SYMBOL_CALLS_LOCAL (info, eh)
2481 || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
2482 && eh->root.type == bfd_link_hash_undefweak))
2484 /* This case can occur if we saw a PLT reloc in an input
2485 file, but the symbol was never referred to by a dynamic
2486 object. In such a case, we don't actually need to build
2487 a procedure linkage table, and we can just do a PCREL
2489 eh->plt.offset = (bfd_vma) -1;
2496 eh->plt.offset = (bfd_vma) -1;
2498 /* If this is a weak symbol, and there is a real definition, the
2499 processor independent code will have arranged for us to see the
2500 real definition first, and we can just use the same value. */
2501 if (eh->is_weakalias)
2503 struct elf_link_hash_entry *def = weakdef (eh);
2504 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2505 eh->root.u.def.section = def->root.u.def.section;
2506 eh->root.u.def.value = def->root.u.def.value;
2507 eh->non_got_ref = def->non_got_ref;
2511 /* This is a reference to a symbol defined by a dynamic object which
2512 is not a function. */
2514 /* If we are creating a shared library, we must presume that the
2515 only references to the symbol are via the global offset table.
2516 For such cases we need not do anything here; the relocations will
2517 be handled correctly by relocate_section. */
2518 if (bfd_link_pic (info))
2521 /* If there are no references to this symbol that do not use the
2522 GOT, we don't need to generate a copy reloc. */
2523 if (!eh->non_got_ref)
2526 /* If -z nocopyreloc was given, we won't generate them either. */
2527 if (info->nocopyreloc)
2529 eh->non_got_ref = 0;
2533 /* If we don't find any dynamic relocs in read-only sections, then
2534 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2535 if (!readonly_dynrelocs (eh))
2537 eh->non_got_ref = 0;
2541 /* We must allocate the symbol in our .dynbss section, which will
2542 become part of the .bss section of the executable. There will be
2543 an entry for this symbol in the .dynsym section. The dynamic
2544 object will contain position independent code, so all references
2545 from the dynamic object to this symbol will go through the global
2546 offset table. The dynamic linker will use the .dynsym entry to
2547 determine the address it must put in the global offset table, so
2548 both the dynamic object and the regular object will refer to the
2549 same memory location for the variable. */
2551 htab = metag_link_hash_table (info);
2553 /* We must generate a COPY reloc to tell the dynamic linker to
2554 copy the initial value out of the dynamic object and into the
2555 runtime process image. */
2556 if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
2558 s = htab->etab.sdynrelro;
2559 srel = htab->etab.sreldynrelro;
2563 s = htab->etab.sdynbss;
2564 srel = htab->etab.srelbss;
2566 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
2568 srel->size += sizeof (Elf32_External_Rela);
2572 return _bfd_elf_adjust_dynamic_copy (info, eh, s);
2575 /* Allocate space in .plt, .got and associated reloc sections for
2579 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2581 struct bfd_link_info *info;
2582 struct elf_metag_link_hash_table *htab;
2583 struct elf_metag_link_hash_entry *hh;
2584 struct elf_dyn_relocs *hdh_p;
2586 if (eh->root.type == bfd_link_hash_indirect)
2589 if (eh->root.type == bfd_link_hash_warning)
2590 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2593 htab = metag_link_hash_table (info);
2595 if (htab->etab.dynamic_sections_created
2596 && eh->plt.refcount > 0)
2598 /* Make sure this symbol is output as a dynamic symbol.
2599 Undefined weak syms won't yet be marked as dynamic. */
2600 if (eh->dynindx == -1
2601 && !eh->forced_local)
2603 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2607 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
2609 asection *s = htab->etab.splt;
2611 /* If this is the first .plt entry, make room for the special
2614 s->size += PLT_ENTRY_SIZE;
2616 eh->plt.offset = s->size;
2618 /* If this symbol is not defined in a regular file, and we are
2619 not generating a shared library, then set the symbol to this
2620 location in the .plt. This is required to make function
2621 pointers compare as equal between the normal executable and
2622 the shared library. */
2623 if (! bfd_link_pic (info)
2624 && !eh->def_regular)
2626 eh->root.u.def.section = s;
2627 eh->root.u.def.value = eh->plt.offset;
2630 /* Make room for this entry. */
2631 s->size += PLT_ENTRY_SIZE;
2633 /* We also need to make an entry in the .got.plt section, which
2634 will be placed in the .got section by the linker script. */
2635 htab->etab.sgotplt->size += 4;
2637 /* We also need to make an entry in the .rel.plt section. */
2638 htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
2642 eh->plt.offset = (bfd_vma) -1;
2648 eh->plt.offset = (bfd_vma) -1;
2652 if (eh->got.refcount > 0)
2656 int tls_type = metag_elf_hash_entry (eh)->tls_type;
2658 /* Make sure this symbol is output as a dynamic symbol.
2659 Undefined weak syms won't yet be marked as dynamic. */
2660 if (eh->dynindx == -1
2661 && !eh->forced_local)
2663 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2667 s = htab->etab.sgot;
2669 eh->got.offset = s->size;
2671 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
2672 if (tls_type == GOT_TLS_GD)
2674 dyn = htab->etab.dynamic_sections_created;
2675 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2676 R_METAG_TLS_GD needs one if local symbol and two if global. */
2677 if ((tls_type == GOT_TLS_GD && eh->dynindx == -1)
2678 || (tls_type == GOT_TLS_IE && dyn))
2679 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2680 else if (tls_type == GOT_TLS_GD)
2681 htab->etab.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2682 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2683 bfd_link_pic (info),
2685 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2688 eh->got.offset = (bfd_vma) -1;
2690 hh = (struct elf_metag_link_hash_entry *) eh;
2691 if (hh->dyn_relocs == NULL)
2694 /* If this is a -Bsymbolic shared link, then we need to discard all
2695 space allocated for dynamic pc-relative relocs against symbols
2696 defined in a regular object. For the normal shared case, discard
2697 space for relocs that have become local due to symbol visibility
2699 if (bfd_link_pic (info))
2701 if (SYMBOL_CALLS_LOCAL (info, eh))
2703 struct elf_dyn_relocs **hdh_pp;
2705 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2707 hdh_p->count -= hdh_p->pc_count;
2708 hdh_p->pc_count = 0;
2709 if (hdh_p->count == 0)
2710 *hdh_pp = hdh_p->next;
2712 hdh_pp = &hdh_p->next;
2716 /* Also discard relocs on undefined weak syms with non-default
2718 if (hh->dyn_relocs != NULL
2719 && eh->root.type == bfd_link_hash_undefweak)
2721 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2722 hh->dyn_relocs = NULL;
2724 /* Make sure undefined weak symbols are output as a dynamic
2726 else if (eh->dynindx == -1
2727 && !eh->forced_local)
2729 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2736 /* For the non-shared case, discard space for relocs against
2737 symbols which turn out to need copy relocs or are not
2739 if (!eh->non_got_ref
2740 && ((eh->def_dynamic
2741 && !eh->def_regular)
2742 || (htab->etab.dynamic_sections_created
2743 && (eh->root.type == bfd_link_hash_undefweak
2744 || eh->root.type == bfd_link_hash_undefined))))
2746 /* Make sure this symbol is output as a dynamic symbol.
2747 Undefined weak syms won't yet be marked as dynamic. */
2748 if (eh->dynindx == -1
2749 && !eh->forced_local)
2751 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2755 /* If that succeeded, we know we'll be keeping all the
2757 if (eh->dynindx != -1)
2761 hh->dyn_relocs = NULL;
2767 /* Finally, allocate space. */
2768 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->next)
2770 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2771 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2777 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2778 read-only sections. */
2781 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2785 if (h->root.type == bfd_link_hash_indirect)
2788 sec = readonly_dynrelocs (h);
2791 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2793 info->flags |= DF_TEXTREL;
2794 info->callbacks->minfo
2795 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2796 sec->owner, h->root.root.string, sec);
2798 /* Not an error, just cut short the traversal. */
2804 /* Set the sizes of the dynamic sections. */
2807 elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2808 struct bfd_link_info *info)
2810 struct elf_metag_link_hash_table *htab;
2816 htab = metag_link_hash_table (info);
2817 dynobj = htab->etab.dynobj;
2821 if (htab->etab.dynamic_sections_created)
2823 /* Set the contents of the .interp section to the interpreter. */
2824 if (bfd_link_executable (info) && !info->nointerp)
2826 s = bfd_get_linker_section (dynobj, ".interp");
2829 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2830 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2834 /* Set up .got offsets for local syms, and space for local dynamic
2836 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2838 bfd_signed_vma *local_got;
2839 bfd_signed_vma *end_local_got;
2840 bfd_size_type locsymcount;
2841 Elf_Internal_Shdr *symtab_hdr;
2843 char *local_tls_type;
2845 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2848 for (s = ibfd->sections; s != NULL; s = s->next)
2850 struct elf_dyn_relocs *hdh_p;
2852 for (hdh_p = ((struct elf_dyn_relocs *)
2853 elf_section_data (s)->local_dynrel);
2855 hdh_p = hdh_p->next)
2857 if (!bfd_is_abs_section (hdh_p->sec)
2858 && bfd_is_abs_section (hdh_p->sec->output_section))
2860 /* Input section has been discarded, either because
2861 it is a copy of a linkonce section or due to
2862 linker script /DISCARD/, so we'll be discarding
2865 else if (hdh_p->count != 0)
2867 srel = elf_section_data (hdh_p->sec)->sreloc;
2868 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2869 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2870 info->flags |= DF_TEXTREL;
2875 local_got = elf_local_got_refcounts (ibfd);
2879 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2880 locsymcount = symtab_hdr->sh_info;
2881 end_local_got = local_got + locsymcount;
2882 local_tls_type = metag_elf_local_got_tls_type (ibfd);
2883 s = htab->etab.sgot;
2884 srel = htab->etab.srelgot;
2885 for (; local_got < end_local_got; ++local_got)
2889 *local_got = s->size;
2890 s->size += GOT_ENTRY_SIZE;
2891 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
2892 if (*local_tls_type == GOT_TLS_GD)
2894 if (bfd_link_pic (info))
2895 srel->size += sizeof (Elf32_External_Rela);
2898 *local_got = (bfd_vma) -1;
2903 if (htab->tls_ldm_got.refcount > 0)
2905 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2907 htab->tls_ldm_got.offset = htab->etab.sgot->size;
2908 htab->etab.sgot->size += 8;
2909 htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
2912 htab->tls_ldm_got.offset = -1;
2914 /* Allocate global sym .plt and .got entries, and space for global
2915 sym dynamic relocs. */
2916 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2918 /* We now have determined the sizes of the various dynamic sections.
2919 Allocate memory for them. */
2921 for (s = dynobj->sections; s != NULL; s = s->next)
2923 bfd_boolean reloc_section = FALSE;
2925 if ((s->flags & SEC_LINKER_CREATED) == 0)
2928 if (s == htab->etab.splt
2929 || s == htab->etab.sgot
2930 || s == htab->etab.sgotplt
2931 || s == htab->etab.sdynbss
2932 || s == htab->etab.sdynrelro)
2934 /* Strip this section if we don't need it; see the
2937 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2939 if (s->size != 0 && s != htab->etab.srelplt)
2942 /* We use the reloc_count field as a counter if we need
2943 to copy relocs into the output file. */
2945 reloc_section = TRUE;
2949 /* It's not one of our sections, so don't allocate space. */
2955 /* If we don't need this section, strip it from the
2956 output file. This is mostly to handle .rela.bss and
2957 .rela.plt. We must create both sections in
2958 create_dynamic_sections, because they must be created
2959 before the linker maps input sections to output
2960 sections. The linker does that before
2961 adjust_dynamic_symbol is called, and it is that
2962 function which decides whether anything needs to go
2963 into these sections. */
2964 s->flags |= SEC_EXCLUDE;
2968 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2971 /* Allocate memory for the section contents. */
2972 s->contents = bfd_zalloc (dynobj, s->size);
2973 if (s->contents == NULL)
2975 else if (reloc_section)
2977 unsigned char *contents = s->contents;
2978 Elf32_External_Rela reloc;
2980 /* Fill the reloc section with a R_METAG_NONE type reloc. */
2981 memset(&reloc, 0, sizeof(Elf32_External_Rela));
2982 reloc.r_info[0] = R_METAG_NONE;
2983 for (; contents < (s->contents + s->size);
2984 contents += sizeof(Elf32_External_Rela))
2986 memcpy(contents, &reloc, sizeof(Elf32_External_Rela));
2991 if (htab->etab.dynamic_sections_created)
2993 /* Add some entries to the .dynamic section. We fill in the
2994 values later, in elf_metag_finish_dynamic_sections, but we
2995 must add the entries now so that we get the correct size for
2996 the .dynamic section. The DT_DEBUG entry is filled in by the
2997 dynamic linker and used by the debugger. */
2998 #define add_dynamic_entry(TAG, VAL) \
2999 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3001 if (!add_dynamic_entry (DT_PLTGOT, 0))
3004 if (bfd_link_executable (info))
3006 if (!add_dynamic_entry (DT_DEBUG, 0))
3010 if (htab->etab.srelplt->size != 0)
3012 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3013 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3014 || !add_dynamic_entry (DT_JMPREL, 0))
3020 if (!add_dynamic_entry (DT_RELA, 0)
3021 || !add_dynamic_entry (DT_RELASZ, 0)
3022 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3025 /* If any dynamic relocs apply to a read-only section,
3026 then we need a DT_TEXTREL entry. */
3027 if ((info->flags & DF_TEXTREL) == 0)
3028 elf_link_hash_traverse (&htab->etab, maybe_set_textrel, info);
3030 if ((info->flags & DF_TEXTREL) != 0)
3032 if (!add_dynamic_entry (DT_TEXTREL, 0))
3037 #undef add_dynamic_entry
3042 /* Finish up dynamic symbol handling. We set the contents of various
3043 dynamic sections here. */
3046 elf_metag_finish_dynamic_symbol (bfd *output_bfd,
3047 struct bfd_link_info *info,
3048 struct elf_link_hash_entry *eh,
3049 Elf_Internal_Sym *sym)
3051 struct elf_metag_link_hash_table *htab;
3052 Elf_Internal_Rela rel;
3055 htab = metag_link_hash_table (info);
3057 if (eh->plt.offset != (bfd_vma) -1)
3067 if (eh->plt.offset & 1)
3070 BFD_ASSERT (eh->dynindx != -1);
3072 splt = htab->etab.splt;
3073 sgot = htab->etab.sgotplt;
3074 srela = htab->etab.srelplt;
3075 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3077 /* Get the index in the procedure linkage table which
3078 corresponds to this symbol. This is the index of this symbol
3079 in all the symbols for which we are making plt entries. The
3080 first entry in the procedure linkage table is reserved. */
3081 plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1;
3083 /* Get the offset into the .got.plt table of the entry that
3084 corresponds to this function. */
3085 got_offset = plt_index * GOT_ENTRY_SIZE;
3087 BFD_ASSERT (got_offset < (1 << 16));
3089 got_entry = sgot->output_section->vma
3090 + sgot->output_offset
3093 BFD_ASSERT (plt_index < (1 << 16));
3095 /* Fill in the entry in the procedure linkage table. */
3096 if (! bfd_link_pic (info))
3098 bfd_put_32 (output_bfd,
3100 | (((got_entry >> 16) & 0xffff) << 3)),
3101 splt->contents + eh->plt.offset);
3102 bfd_put_32 (output_bfd,
3104 | ((got_entry & 0xffff) << 3)),
3105 splt->contents + eh->plt.offset + 4);
3106 bfd_put_32 (output_bfd, plt_entry[2],
3107 splt->contents + eh->plt.offset + 8);
3108 bfd_put_32 (output_bfd,
3109 (plt_entry[3] | (plt_index << 3)),
3110 splt->contents + eh->plt.offset + 12);
3111 bfd_put_32 (output_bfd,
3113 | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3114 splt->contents + eh->plt.offset + 16);
3118 bfd_vma addr = got_entry - (splt->output_section->vma +
3119 splt->output_offset + eh->plt.offset);
3121 bfd_put_32 (output_bfd,
3122 plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3123 splt->contents + eh->plt.offset);
3124 bfd_put_32 (output_bfd,
3125 plt_pic_entry[1] | ((addr & 0xffff) << 3),
3126 splt->contents + eh->plt.offset + 4);
3127 bfd_put_32 (output_bfd, plt_pic_entry[2],
3128 splt->contents + eh->plt.offset + 8);
3129 bfd_put_32 (output_bfd,
3130 (plt_pic_entry[3] | (plt_index << 3)),
3131 splt->contents + eh->plt.offset + 12);
3132 bfd_put_32 (output_bfd,
3134 + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3135 splt->contents + eh->plt.offset + 16);
3138 /* Fill in the entry in the global offset table. */
3139 bfd_put_32 (output_bfd,
3140 (splt->output_section->vma
3141 + splt->output_offset
3143 + 12), /* offset within PLT entry */
3144 sgot->contents + got_offset);
3146 /* Fill in the entry in the .rela.plt section. */
3147 rel.r_offset = (sgot->output_section->vma
3148 + sgot->output_offset
3150 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT);
3152 loc = htab->etab.srelplt->contents;
3153 loc += plt_index * sizeof(Elf32_External_Rela);
3154 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3156 if (!eh->def_regular)
3158 /* Mark the symbol as undefined, rather than as defined in
3159 the .plt section. Leave the value alone. */
3160 sym->st_shndx = SHN_UNDEF;
3164 if (eh->got.offset != (bfd_vma) -1
3165 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
3166 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
3168 /* This symbol has an entry in the global offset table. Set it
3171 rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
3172 + htab->etab.sgot->output_offset
3173 + htab->etab.sgot->output_section->vma);
3175 /* If this is a -Bsymbolic link and the symbol is defined
3176 locally or was forced to be local because of a version file,
3177 we just want to emit a RELATIVE reloc. The entry in the
3178 global offset table will already have been initialized in the
3179 relocate_section function. */
3180 if (bfd_link_pic (info)
3181 && (info->symbolic || eh->dynindx == -1)
3184 rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
3185 rel.r_addend = (eh->root.u.def.value
3186 + eh->root.u.def.section->output_offset
3187 + eh->root.u.def.section->output_section->vma);
3191 if ((eh->got.offset & 1) != 0)
3193 bfd_put_32 (output_bfd, 0, htab->etab.sgot->contents + eh->got.offset);
3194 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT);
3198 loc = htab->etab.srelgot->contents;
3199 loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3200 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3207 /* This symbol needs a copy reloc. Set it up. */
3209 if (! (eh->dynindx != -1
3210 && (eh->root.type == bfd_link_hash_defined
3211 || eh->root.type == bfd_link_hash_defweak)))
3214 rel.r_offset = (eh->root.u.def.value
3215 + eh->root.u.def.section->output_offset
3216 + eh->root.u.def.section->output_section->vma);
3218 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY);
3219 if (eh->root.u.def.section == htab->etab.sdynrelro)
3220 s = htab->etab.sreldynrelro;
3222 s = htab->etab.srelbss;
3223 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3224 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3227 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3228 if (eh->root.root.string[0] == '_'
3229 && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
3230 || eh == htab->etab.hgot))
3232 sym->st_shndx = SHN_ABS;
3238 /* Set the Meta ELF ABI version. */
3241 elf_metag_post_process_headers (bfd * abfd, struct bfd_link_info * link_info)
3243 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
3245 _bfd_elf_post_process_headers (abfd, link_info);
3246 i_ehdrp = elf_elfheader (abfd);
3247 i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION;
3250 /* Used to decide how to sort relocs in an optimal manner for the
3251 dynamic linker, before writing them out. */
3253 static enum elf_reloc_type_class
3254 elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3255 const asection *rel_sec ATTRIBUTE_UNUSED,
3256 const Elf_Internal_Rela *rela)
3258 switch ((int) ELF32_R_TYPE (rela->r_info))
3260 case R_METAG_RELATIVE:
3261 return reloc_class_relative;
3262 case R_METAG_JMP_SLOT:
3263 return reloc_class_plt;
3265 return reloc_class_copy;
3267 return reloc_class_normal;
3271 /* Finish up the dynamic sections. */
3274 elf_metag_finish_dynamic_sections (bfd *output_bfd,
3275 struct bfd_link_info *info)
3278 struct elf_metag_link_hash_table *htab;
3281 htab = metag_link_hash_table (info);
3282 dynobj = htab->etab.dynobj;
3284 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3286 if (htab->etab.dynamic_sections_created)
3289 Elf32_External_Dyn *dyncon, *dynconend;
3294 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3295 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3296 for (; dyncon < dynconend; dyncon++)
3298 Elf_Internal_Dyn dyn;
3301 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3309 s = htab->etab.sgot;
3310 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3311 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3315 s = htab->etab.srelplt;
3316 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3317 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3321 s = htab->etab.srelplt;
3322 dyn.d_un.d_val = s->size;
3323 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3329 /* Fill in the first entry in the procedure linkage table. */
3330 splt = htab->etab.splt;
3331 if (splt && splt->size > 0)
3334 /* addr = .got + 4 */
3335 addr = (htab->etab.sgot->output_section->vma
3336 + htab->etab.sgot->output_offset + 4);
3337 if (bfd_link_pic (info))
3339 addr -= splt->output_section->vma + splt->output_offset;
3340 bfd_put_32 (output_bfd,
3341 plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3343 bfd_put_32 (output_bfd,
3344 plt0_pic_entry[1] | ((addr & 0xffff) << 3),
3345 splt->contents + 4);
3346 bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8);
3347 bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12);
3348 bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16);
3352 bfd_put_32 (output_bfd,
3353 plt0_entry[0] | (((addr >> 16) & 0xffff) << 3),
3355 bfd_put_32 (output_bfd,
3356 plt0_entry[1] | ((addr & 0xffff) << 3),
3357 splt->contents + 4);
3358 bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
3359 bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
3360 bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16);
3363 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3368 if (htab->etab.sgot != NULL && htab->etab.sgot->size != 0)
3370 /* Fill in the first entry in the global offset table.
3371 We use it to point to our dynamic section, if we have one. */
3372 bfd_put_32 (output_bfd,
3373 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
3374 htab->etab.sgot->contents);
3376 /* The second entry is reserved for use by the dynamic linker. */
3377 memset (htab->etab.sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
3379 /* Set .got entry size. */
3380 elf_section_data (htab->etab.sgot->output_section)
3381 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3387 /* Return the section that should be marked against GC for a given
3391 elf_metag_gc_mark_hook (asection *sec,
3392 struct bfd_link_info *info,
3393 Elf_Internal_Rela *rela,
3394 struct elf_link_hash_entry *hh,
3395 Elf_Internal_Sym *sym)
3398 switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
3400 case R_METAG_GNU_VTINHERIT:
3401 case R_METAG_GNU_VTENTRY:
3405 return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
3408 /* Determine the type of stub needed, if any, for a call. */
3410 static enum elf_metag_stub_type
3411 metag_type_of_stub (asection *input_sec,
3412 const Elf_Internal_Rela *rel,
3413 struct elf_metag_link_hash_entry *hh,
3414 bfd_vma destination,
3415 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3418 bfd_vma branch_offset;
3419 bfd_vma max_branch_offset;
3422 !(hh->eh.root.type == bfd_link_hash_defined
3423 || hh->eh.root.type == bfd_link_hash_defweak))
3424 return metag_stub_none;
3426 /* Determine where the call point is. */
3427 location = (input_sec->output_offset
3428 + input_sec->output_section->vma
3431 branch_offset = destination - location;
3433 /* Determine if a long branch stub is needed. Meta branch offsets
3434 are signed 19 bits 4 byte aligned. */
3435 max_branch_offset = (1 << (BRANCH_BITS-1)) << 2;
3437 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
3439 if (bfd_link_pic (info))
3440 return metag_stub_long_branch_shared;
3442 return metag_stub_long_branch;
3445 return metag_stub_none;
3448 #define MOVT_A0_3 0x82180005
3449 #define JUMP_A0_3 0xac180003
3451 #define MOVT_A1LBP 0x83080005
3452 #define ADD_A1LBP 0x83080000
3454 #define ADDT_A0_3_CPC 0x82980001
3455 #define ADD_A0_3_A0_3 0x82180000
3456 #define MOV_PC_A0_3 0xa3180ca0
3459 metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3461 struct elf_metag_stub_hash_entry *hsh;
3468 /* Massage our args to the form they really have. */
3469 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3471 stub_sec = hsh->stub_sec;
3473 /* Make a note of the offset within the stubs for this entry. */
3474 hsh->stub_offset = stub_sec->size;
3475 loc = stub_sec->contents + hsh->stub_offset;
3477 stub_bfd = stub_sec->owner;
3479 switch (hsh->stub_type)
3481 case metag_stub_long_branch_shared:
3482 /* A PIC long branch stub is an ADDT and an ADD instruction used to
3483 calculate the jump target using A0.3 as a temporary. Then a MOV
3484 to PC carries out the jump. */
3485 sym_value = (hsh->target_value
3486 + hsh->target_section->output_offset
3487 + hsh->target_section->output_section->vma
3490 sym_value -= (hsh->stub_offset
3491 + stub_sec->output_offset
3492 + stub_sec->output_section->vma);
3494 bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3),
3497 bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3),
3500 bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8);
3504 case metag_stub_long_branch:
3505 /* A standard long branch stub is a MOVT instruction followed by a
3506 JUMP instruction using the A0.3 register as a temporary. This is
3507 the same method used by the LDLK linker (patch.c). */
3508 sym_value = (hsh->target_value
3509 + hsh->target_section->output_offset
3510 + hsh->target_section->output_section->vma
3513 bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3),
3516 bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4);
3525 stub_sec->size += size;
3529 /* As above, but don't actually build the stub. Just bump offset so
3530 we know stub section sizes. */
3533 metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3535 struct elf_metag_stub_hash_entry *hsh;
3538 /* Massage our args to the form they really have. */
3539 hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3541 if (hsh->stub_type == metag_stub_long_branch)
3543 else if (hsh->stub_type == metag_stub_long_branch_shared)
3546 hsh->stub_sec->size += size;
3550 /* Set up various things so that we can make a list of input sections
3551 for each output section included in the link. Returns -1 on error,
3552 0 when no stubs will be needed, and 1 on success. */
3555 elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3558 unsigned int bfd_count;
3559 unsigned int top_id, top_index;
3561 asection **input_list, **list;
3563 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3565 /* Count the number of input BFDs and find the top input section id. */
3566 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3568 input_bfd = input_bfd->link.next)
3571 for (section = input_bfd->sections;
3573 section = section->next)
3575 if (top_id < section->id)
3576 top_id = section->id;
3580 htab->bfd_count = bfd_count;
3582 amt = sizeof (struct map_stub) * (top_id + 1);
3583 htab->stub_group = bfd_zmalloc (amt);
3584 if (htab->stub_group == NULL)
3587 /* We can't use output_bfd->section_count here to find the top output
3588 section index as some sections may have been removed, and
3589 strip_excluded_output_sections doesn't renumber the indices. */
3590 for (section = output_bfd->sections, top_index = 0;
3592 section = section->next)
3594 if (top_index < section->index)
3595 top_index = section->index;
3598 htab->top_index = top_index;
3599 amt = sizeof (asection *) * (top_index + 1);
3600 input_list = bfd_malloc (amt);
3601 htab->input_list = input_list;
3602 if (input_list == NULL)
3605 /* For sections we aren't interested in, mark their entries with a
3606 value we can check later. */
3607 list = input_list + top_index;
3609 *list = bfd_abs_section_ptr;
3610 while (list-- != input_list);
3612 for (section = output_bfd->sections;
3614 section = section->next)
3616 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3617 * have PC relative relocs in them but no code flag set. */
3618 if (((section->flags & SEC_CODE) != 0) ||
3619 strcmp(".ctors", section->name) ||
3620 strcmp(".dtors", section->name))
3621 input_list[section->index] = NULL;
3627 /* The linker repeatedly calls this function for each input section,
3628 in the order that input sections are linked into output sections.
3629 Build lists of input sections to determine groupings between which
3630 we may insert linker stubs. */
3633 elf_metag_next_input_section (struct bfd_link_info *info, asection *isec)
3635 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3637 if (isec->output_section->index <= htab->top_index)
3639 asection **list = htab->input_list + isec->output_section->index;
3640 if (*list != bfd_abs_section_ptr)
3642 /* Steal the link_sec pointer for our list. */
3643 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3644 /* This happens to make the list in reverse order,
3645 which is what we want. */
3646 PREV_SEC (isec) = *list;
3652 /* See whether we can group stub sections together. Grouping stub
3653 sections may result in fewer stubs. More importantly, we need to
3654 put all .init* and .fini* stubs at the beginning of the .init or
3655 .fini output sections respectively, because glibc splits the
3656 _init and _fini functions into multiple parts. Putting a stub in
3657 the middle of a function is not a good idea. */
3660 group_sections (struct elf_metag_link_hash_table *htab,
3661 bfd_size_type stub_group_size,
3662 bfd_boolean stubs_always_before_branch)
3664 asection **list = htab->input_list + htab->top_index;
3667 asection *tail = *list;
3668 if (tail == bfd_abs_section_ptr)
3670 while (tail != NULL)
3674 bfd_size_type total;
3675 bfd_boolean big_sec;
3679 big_sec = total >= stub_group_size;
3681 while ((prev = PREV_SEC (curr)) != NULL
3682 && ((total += curr->output_offset - prev->output_offset)
3686 /* OK, the size from the start of CURR to the end is less
3687 than stub_group_size bytes and thus can be handled by one stub
3688 section. (or the tail section is itself larger than
3689 stub_group_size bytes, in which case we may be toast.)
3690 We should really be keeping track of the total size of
3691 stubs added here, as stubs contribute to the final output
3695 prev = PREV_SEC (tail);
3696 /* Set up this stub group. */
3697 htab->stub_group[tail->id].link_sec = curr;
3699 while (tail != curr && (tail = prev) != NULL);
3701 /* But wait, there's more! Input sections up to stub_group_size
3702 bytes before the stub section can be handled by it too.
3703 Don't do this if we have a really large section after the
3704 stubs, as adding more stubs increases the chance that
3705 branches may not reach into the stub section. */
3706 if (!stubs_always_before_branch && !big_sec)
3710 && ((total += tail->output_offset - prev->output_offset)
3714 prev = PREV_SEC (tail);
3715 htab->stub_group[tail->id].link_sec = curr;
3721 while (list-- != htab->input_list);
3722 free (htab->input_list);
3726 /* Read in all local syms for all input bfds.
3727 Returns -1 on error, 0 otherwise. */
3730 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
3731 struct bfd_link_info *info)
3733 unsigned int bfd_indx;
3734 Elf_Internal_Sym *local_syms, **all_local_syms;
3735 int stub_changed = 0;
3736 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3738 /* We want to read in symbol extension records only once. To do this
3739 we need to read in the local symbols in parallel and save them for
3740 later use; so hold pointers to the local symbols in an array. */
3741 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3742 all_local_syms = bfd_zmalloc (amt);
3743 htab->all_local_syms = all_local_syms;
3744 if (all_local_syms == NULL)
3747 /* Walk over all the input BFDs, swapping in local symbols. */
3750 input_bfd = input_bfd->link.next, bfd_indx++)
3752 Elf_Internal_Shdr *symtab_hdr;
3754 /* We'll need the symbol table in a second. */
3755 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3756 if (symtab_hdr->sh_info == 0)
3759 /* We need an array of the local symbols attached to the input bfd. */
3760 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3761 if (local_syms == NULL)
3763 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3764 symtab_hdr->sh_info, 0,
3766 /* Cache them for elf_link_input_bfd. */
3767 symtab_hdr->contents = (unsigned char *) local_syms;
3769 if (local_syms == NULL)
3772 all_local_syms[bfd_indx] = local_syms;
3775 return stub_changed;
3778 /* Determine and set the size of the stub section for a final link.
3780 The basic idea here is to examine all the relocations looking for
3781 PC-relative calls to a target that is unreachable with a "CALLR"
3784 /* See elf32-hppa.c and elf64-ppc.c. */
3787 elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd,
3788 struct bfd_link_info *info,
3789 bfd_signed_vma group_size,
3790 asection * (*add_stub_section) (const char *, asection *),
3791 void (*layout_sections_again) (void))
3793 bfd_size_type stub_group_size;
3794 bfd_boolean stubs_always_before_branch;
3795 bfd_boolean stub_changed;
3796 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3798 /* Stash our params away. */
3799 htab->stub_bfd = stub_bfd;
3800 htab->add_stub_section = add_stub_section;
3801 htab->layout_sections_again = layout_sections_again;
3802 stubs_always_before_branch = group_size < 0;
3804 stub_group_size = -group_size;
3806 stub_group_size = group_size;
3807 if (stub_group_size == 1)
3809 /* Default values. */
3810 /* FIXME: not sure what these values should be */
3811 if (stubs_always_before_branch)
3813 stub_group_size = (1 << BRANCH_BITS);
3817 stub_group_size = (1 << BRANCH_BITS);
3821 group_sections (htab, stub_group_size, stubs_always_before_branch);
3823 switch (get_local_syms (output_bfd, info->input_bfds, info))
3826 if (htab->all_local_syms)
3827 goto error_ret_free_local;
3831 stub_changed = FALSE;
3835 stub_changed = TRUE;
3842 unsigned int bfd_indx;
3845 for (input_bfd = info->input_bfds, bfd_indx = 0;
3847 input_bfd = input_bfd->link.next, bfd_indx++)
3849 Elf_Internal_Shdr *symtab_hdr;
3851 Elf_Internal_Sym *local_syms;
3853 /* We'll need the symbol table in a second. */
3854 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3855 if (symtab_hdr->sh_info == 0)
3858 local_syms = htab->all_local_syms[bfd_indx];
3860 /* Walk over each section attached to the input bfd. */
3861 for (section = input_bfd->sections;
3863 section = section->next)
3865 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3867 /* If there aren't any relocs, then there's nothing more
3869 if ((section->flags & SEC_RELOC) == 0
3870 || section->reloc_count == 0)
3873 /* If this section is a link-once section that will be
3874 discarded, then don't create any stubs. */
3875 if (section->output_section == NULL
3876 || section->output_section->owner != output_bfd)
3879 /* Get the relocs. */
3881 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3883 if (internal_relocs == NULL)
3884 goto error_ret_free_local;
3886 /* Now examine each relocation. */
3887 irela = internal_relocs;
3888 irelaend = irela + section->reloc_count;
3889 for (; irela < irelaend; irela++)
3891 unsigned int r_type, r_indx;
3892 enum elf_metag_stub_type stub_type;
3893 struct elf_metag_stub_hash_entry *hsh;
3896 bfd_vma destination;
3897 struct elf_metag_link_hash_entry *hh;
3899 const asection *id_sec;
3901 r_type = ELF32_R_TYPE (irela->r_info);
3902 r_indx = ELF32_R_SYM (irela->r_info);
3904 if (r_type >= (unsigned int) R_METAG_MAX)
3906 bfd_set_error (bfd_error_bad_value);
3907 error_ret_free_internal:
3908 if (elf_section_data (section)->relocs == NULL)
3909 free (internal_relocs);
3910 goto error_ret_free_local;
3913 /* Only look for stubs on CALLR and B instructions. */
3914 if (!(r_type == (unsigned int) R_METAG_RELBRANCH ||
3915 r_type == (unsigned int) R_METAG_RELBRANCH_PLT))
3918 /* Now determine the call target, its name, value,
3924 if (r_indx < symtab_hdr->sh_info)
3926 /* It's a local symbol. */
3927 Elf_Internal_Sym *sym;
3928 Elf_Internal_Shdr *hdr;
3931 sym = local_syms + r_indx;
3932 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3933 sym_value = sym->st_value;
3934 shndx = sym->st_shndx;
3935 if (shndx < elf_numsections (input_bfd))
3937 hdr = elf_elfsections (input_bfd)[shndx];
3938 sym_sec = hdr->bfd_section;
3939 destination = (sym_value + irela->r_addend
3940 + sym_sec->output_offset
3941 + sym_sec->output_section->vma);
3946 /* It's an external symbol. */
3949 e_indx = r_indx - symtab_hdr->sh_info;
3950 hh = ((struct elf_metag_link_hash_entry *)
3951 elf_sym_hashes (input_bfd)[e_indx]);
3953 while (hh->eh.root.type == bfd_link_hash_indirect
3954 || hh->eh.root.type == bfd_link_hash_warning)
3955 hh = ((struct elf_metag_link_hash_entry *)
3956 hh->eh.root.u.i.link);
3958 if (hh->eh.root.type == bfd_link_hash_defined
3959 || hh->eh.root.type == bfd_link_hash_defweak)
3961 sym_sec = hh->eh.root.u.def.section;
3962 sym_value = hh->eh.root.u.def.value;
3963 if (hh->eh.plt.offset != (bfd_vma) -1
3964 && hh->eh.dynindx != -1
3965 && r_type == (unsigned int) R_METAG_RELBRANCH_PLT)
3967 sym_sec = htab->etab.splt;
3968 sym_value = hh->eh.plt.offset;
3971 if (sym_sec->output_section != NULL)
3972 destination = (sym_value + irela->r_addend
3973 + sym_sec->output_offset
3974 + sym_sec->output_section->vma);
3978 else if (hh->eh.root.type == bfd_link_hash_undefweak)
3980 if (! bfd_link_pic (info))
3983 else if (hh->eh.root.type == bfd_link_hash_undefined)
3985 if (! (info->unresolved_syms_in_objects == RM_IGNORE
3986 && (ELF_ST_VISIBILITY (hh->eh.other)
3992 bfd_set_error (bfd_error_bad_value);
3993 goto error_ret_free_internal;
3997 /* Determine what (if any) linker stub is needed. */
3998 stub_type = metag_type_of_stub (section, irela, hh,
4000 if (stub_type == metag_stub_none)
4003 /* Support for grouping stub sections. */
4004 id_sec = htab->stub_group[section->id].link_sec;
4006 /* Get the name of this stub. */
4007 stub_name = metag_stub_name (id_sec, sym_sec, hh, irela);
4009 goto error_ret_free_internal;
4011 hsh = metag_stub_hash_lookup (&htab->bstab,
4016 /* The proper stub has already been created. */
4021 hsh = metag_add_stub (stub_name, section, htab);
4025 goto error_ret_free_internal;
4027 hsh->target_value = sym_value;
4028 hsh->target_section = sym_sec;
4029 hsh->stub_type = stub_type;
4031 hsh->addend = irela->r_addend;
4032 stub_changed = TRUE;
4035 /* We're done with the internal relocs, free them. */
4036 if (elf_section_data (section)->relocs == NULL)
4037 free (internal_relocs);
4044 /* OK, we've added some stubs. Find out the new size of the
4046 for (stub_sec = htab->stub_bfd->sections;
4048 stub_sec = stub_sec->next)
4051 bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab);
4053 /* Ask the linker to do its stuff. */
4054 (*htab->layout_sections_again) ();
4055 stub_changed = FALSE;
4058 free (htab->all_local_syms);
4061 error_ret_free_local:
4062 free (htab->all_local_syms);
4066 /* Build all the stubs associated with the current output file. The
4067 stubs are kept in a hash table attached to the main linker hash
4068 table. This function is called via metagelf_finish in the linker. */
4071 elf_metag_build_stubs (struct bfd_link_info *info)
4074 struct bfd_hash_table *table;
4075 struct elf_metag_link_hash_table *htab;
4077 htab = metag_link_hash_table (info);
4079 for (stub_sec = htab->stub_bfd->sections;
4081 stub_sec = stub_sec->next)
4085 /* Allocate memory to hold the linker stubs. */
4086 size = stub_sec->size;
4087 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4088 if (stub_sec->contents == NULL && size != 0)
4093 /* Build the stubs as directed by the stub hash table. */
4094 table = &htab->bstab;
4095 bfd_hash_traverse (table, metag_build_one_stub, info);
4100 /* Return TRUE if SYM represents a local label symbol. */
4103 elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
4105 if (name[0] == '$' && name[1] == 'L')
4107 return _bfd_elf_is_local_label_name (abfd, name);
4110 /* Return address for Ith PLT stub in section PLT, for relocation REL
4111 or (bfd_vma) -1 if it should not be included. */
4114 elf_metag_plt_sym_val (bfd_vma i, const asection *plt,
4115 const arelent *rel ATTRIBUTE_UNUSED)
4117 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4120 #define ELF_ARCH bfd_arch_metag
4121 #define ELF_TARGET_ID METAG_ELF_DATA
4122 #define ELF_MACHINE_CODE EM_METAG
4123 #define ELF_MINPAGESIZE 0x1000
4124 #define ELF_MAXPAGESIZE 0x4000
4125 #define ELF_COMMONPAGESIZE 0x1000
4127 #define TARGET_LITTLE_SYM metag_elf32_vec
4128 #define TARGET_LITTLE_NAME "elf32-metag"
4130 #define elf_symbol_leading_char '_'
4132 #define elf_info_to_howto_rel NULL
4133 #define elf_info_to_howto metag_info_to_howto_rela
4135 #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
4136 #define bfd_elf32_bfd_link_hash_table_create \
4137 elf_metag_link_hash_table_create
4138 #define elf_backend_relocate_section elf_metag_relocate_section
4139 #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
4140 #define elf_backend_check_relocs elf_metag_check_relocs
4141 #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
4142 #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
4143 #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
4144 #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
4145 #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
4146 #define elf_backend_omit_section_dynsym \
4147 _bfd_elf_omit_section_dynsym_all
4148 #define elf_backend_post_process_headers elf_metag_post_process_headers
4149 #define elf_backend_reloc_type_class elf_metag_reloc_type_class
4150 #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
4151 #define elf_backend_plt_sym_val elf_metag_plt_sym_val
4153 #define elf_backend_can_gc_sections 1
4154 #define elf_backend_can_refcount 1
4155 #define elf_backend_rela_normal 1
4156 #define elf_backend_want_got_plt 1
4157 #define elf_backend_want_got_sym 0
4158 #define elf_backend_want_plt_sym 0
4159 #define elf_backend_plt_readonly 1
4160 #define elf_backend_dtrel_excludes_plt 1
4161 #define elf_backend_want_dynrelro 1
4163 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4164 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4166 #include "elf32-target.h"