1 /* Meta support for 32-bit ELF
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
3 Contributed by Imagination Technologies Ltd.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "elf32-metag.h"
27 #include "elf/metag.h"
29 #define GOT_ENTRY_SIZE 4
30 #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
34 1 - with GOT offset */
35 #define METAG_ELF_ABI_VERSION 1
37 static const unsigned int plt0_entry
[] =
39 0x02000005, /* MOVT D0Re0, #HI(GOT+4) */
40 0x02000000, /* ADD D0Re0, D0Re0, #LO(GOT+4) */
41 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
42 0xc600012a, /* GETD PC, [D0Re0+#4] */
46 static const unsigned int plt0_pic_entry
[] =
48 0x82900001, /* ADDT A0.2, CPC0, #0 */
49 0x82100000, /* ADD A0.2, A0.2, #0 */
50 0xa3100c20, /* MOV D0Re0, A0.2 */
51 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
52 0xc600012a, /* GETD PC, [D0Re0+#4] */
55 static const unsigned int plt_entry
[] =
57 0x82100005, /* MOVT A0.2, #HI(GOT+off) */
58 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
59 0xc600806a, /* GETD PC, [A0.2] */
60 0x03000004, /* MOV D1Re0, #LO(offset) */
61 0xa0000000 /* B PLT0 */
64 static const unsigned int plt_pic_entry
[] =
66 0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */
67 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */
68 0xc600806a, /* GETD PC, [A0.2] */
69 0x03000004, /* MOV D1Re0, #LO(offset) */
70 0xa0000000 /* B PLT0 */
73 /* Variable names follow a coding style.
74 Please follow this (Apps Hungarian) style:
76 Structure/Variable Prefix
77 elf_link_hash_table "etab"
78 elf_link_hash_entry "eh"
80 elf_metag_link_hash_table "htab"
81 elf_metag_link_hash_entry "hh"
83 bfd_link_hash_table "btab"
84 bfd_link_hash_entry "bh"
86 bfd_hash_table containing stubs "bstab"
87 elf_metag_stub_hash_entry "hsh"
89 elf_metag_dyn_reloc_entry "hdh"
91 Always remember to use GNU Coding Style. */
93 #define PLT_ENTRY_SIZE sizeof(plt_entry)
95 static reloc_howto_type elf_metag_howto_table
[] =
97 /* High order 16 bit absolute. */
98 HOWTO (R_METAG_HIADDR16
, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 FALSE
, /* pc_relative */
104 complain_overflow_dont
, /* complain_on_overflow */
105 bfd_elf_generic_reloc
, /* special_function */
106 "R_METAG_HIADDR16", /* name */
107 FALSE
, /* partial_inplace */
109 0x0007fff8, /* dst_mask */
110 FALSE
), /* pcrel_offset */
112 /* Low order 16 bit absolute. */
113 HOWTO (R_METAG_LOADDR16
, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE
, /* pc_relative */
119 complain_overflow_dont
,/* complain_on_overflow */
120 bfd_elf_generic_reloc
, /* special_function */
121 "R_METAG_LOADDR16", /* name */
122 FALSE
, /* partial_inplace */
124 0x0007fff8, /* dst_mask */
125 FALSE
), /* pcrel_offset */
127 /* 32 bit absolute. */
128 HOWTO (R_METAG_ADDR32
, /* type */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
132 FALSE
, /* pc_relative */
134 complain_overflow_bitfield
, /* complain_on_overflow */
135 bfd_elf_generic_reloc
, /* special_function */
136 "R_METAG_ADDR32", /* name */
137 FALSE
, /* partial_inplace */
138 0x00000000, /* src_mask */
139 0xffffffff, /* dst_mask */
140 FALSE
), /* pcrel_offset */
143 HOWTO (R_METAG_NONE
, /* type */
145 3, /* size (0 = byte, 1 = short, 2 = long) */
147 FALSE
, /* pc_relative */
149 complain_overflow_dont
, /* complain_on_overflow */
150 bfd_elf_generic_reloc
, /* special_function */
151 "R_METAG_NONE", /* name */
152 FALSE
, /* partial_inplace */
155 FALSE
), /* pcrel_offset */
157 /* 19 bit pc relative */
158 HOWTO (R_METAG_RELBRANCH
, /* type */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
162 TRUE
, /* pc_relative */
164 complain_overflow_signed
, /* complain_on_overflow */
165 bfd_elf_generic_reloc
, /* special_function */
166 "R_METAG_RELBRANCH", /* name */
167 FALSE
, /* partial_inplace */
169 0x00ffffe0, /* dst_mask */
170 FALSE
), /* pcrel_offset */
173 HOWTO (R_METAG_GETSETOFF
, /* type */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
177 FALSE
, /* pc_relative */
179 complain_overflow_dont
, /* complain_on_overflow */
180 bfd_elf_generic_reloc
, /* special_function */
181 "R_METAG_GETSETOFF", /* name */
182 FALSE
, /* partial_inplace */
185 FALSE
), /* pcrel_offset */
212 HOWTO (R_METAG_GNU_VTINHERIT
, /* type */
214 2, /* size (0 = byte, 1 = short, 2 = long) */
216 FALSE
, /* pc_relative */
218 complain_overflow_dont
, /* complain_on_overflow */
219 NULL
, /* special_function */
220 "R_METAG_GNU_VTINHERIT", /* name */
221 FALSE
, /* partial_inplace */
224 FALSE
), /* pcrel_offset */
226 HOWTO (R_METAG_GNU_VTENTRY
, /* type */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
230 FALSE
, /* pc_relative */
232 complain_overflow_dont
, /* complain_on_overflow */
233 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
234 "R_METAG_GNU_VTENTRY", /* name */
235 FALSE
, /* partial_inplace */
238 FALSE
), /* pcrel_offset */
240 /* High order 16 bit GOT offset */
241 HOWTO (R_METAG_HI16_GOTOFF
, /* type */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
245 FALSE
, /* pc_relative */
247 complain_overflow_dont
, /* complain_on_overflow */
248 bfd_elf_generic_reloc
, /* special_function */
249 "R_METAG_HI16_GOTOFF", /* name */
250 FALSE
, /* partial_inplace */
252 0x0007fff8, /* dst_mask */
253 FALSE
), /* pcrel_offset */
255 /* Low order 16 bit GOT offset */
256 HOWTO (R_METAG_LO16_GOTOFF
, /* type */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
260 FALSE
, /* pc_relative */
262 complain_overflow_dont
, /* complain_on_overflow */
263 bfd_elf_generic_reloc
, /* special_function */
264 "R_METAG_LO16_GOTOFF", /* name */
265 FALSE
, /* partial_inplace */
267 0x0007fff8, /* dst_mask */
268 FALSE
), /* pcrel_offset */
270 /* GET/SET GOT offset */
271 HOWTO (R_METAG_GETSET_GOTOFF
, /* type */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
275 FALSE
, /* pc_relative */
277 complain_overflow_dont
, /* complain_on_overflow */
278 bfd_elf_generic_reloc
, /* special_function */
279 "R_METAG_GETSET_GOTOFF", /* name */
280 FALSE
, /* partial_inplace */
283 FALSE
), /* pcrel_offset */
285 /* GET/SET GOT relative */
286 HOWTO (R_METAG_GETSET_GOT
, /* type */
288 1, /* size (0 = byte, 1 = short, 2 = long) */
290 FALSE
, /* pc_relative */
292 complain_overflow_dont
, /* complain_on_overflow */
293 bfd_elf_generic_reloc
, /* special_function */
294 "R_METAG_GETSET_GOT", /* name */
295 FALSE
, /* partial_inplace */
298 FALSE
), /* pcrel_offset */
300 /* High order 16 bit GOT reference */
301 HOWTO (R_METAG_HI16_GOTPC
, /* type */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
305 FALSE
, /* pc_relative */
307 complain_overflow_dont
, /* complain_on_overflow */
308 bfd_elf_generic_reloc
, /* special_function */
309 "R_METAG_HI16_GOTPC", /* name */
310 FALSE
, /* partial_inplace */
312 0x0007fff8, /* dst_mask */
313 FALSE
), /* pcrel_offset */
315 /* Low order 16 bit GOT reference */
316 HOWTO (R_METAG_LO16_GOTPC
, /* type */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
320 FALSE
, /* pc_relative */
322 complain_overflow_dont
, /* complain_on_overflow */
323 bfd_elf_generic_reloc
, /* special_function */
324 "R_METAG_LO16_GOTPC", /* name */
325 FALSE
, /* partial_inplace */
327 0x0007fff8, /* dst_mask */
328 FALSE
), /* pcrel_offset */
330 /* High order 16 bit PLT */
331 HOWTO (R_METAG_HI16_PLT
, /* type */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
335 FALSE
, /* pc_relative */
337 complain_overflow_dont
, /* complain_on_overflow */
338 bfd_elf_generic_reloc
, /* special_function */
339 "R_METAG_HI16_PLT", /* name */
340 FALSE
, /* partial_inplace */
342 0x0007fff8, /* dst_mask */
343 FALSE
), /* pcrel_offset */
345 /* Low order 16 bit PLT */
346 HOWTO (R_METAG_LO16_PLT
, /* type */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
350 FALSE
, /* pc_relative */
352 complain_overflow_dont
, /* complain_on_overflow */
353 bfd_elf_generic_reloc
, /* special_function */
354 "R_METAG_LO16_PLT", /* name */
355 FALSE
, /* partial_inplace */
357 0xffffffff, /* dst_mask */
358 FALSE
), /* pcrel_offset */
360 HOWTO (R_METAG_RELBRANCH_PLT
, /* type */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
364 TRUE
, /* pc_relative */
366 complain_overflow_signed
, /* complain_on_overflow */
367 bfd_elf_generic_reloc
, /* special_function */
368 "R_METAG_RELBRANCH_PLT", /* name */
369 FALSE
, /* partial_inplace */
371 0x00ffffe0, /* dst_mask */
372 FALSE
), /* pcrel_offset */
374 /* Dummy relocs used by the linker internally. */
375 HOWTO (R_METAG_GOTOFF
, /* type */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
379 FALSE
, /* pc_relative */
381 complain_overflow_bitfield
, /* complain_on_overflow */
382 bfd_elf_generic_reloc
, /* special_function */
383 "R_METAG_GOTOFF", /* name */
384 FALSE
, /* partial_inplace */
385 0xffffffff, /* src_mask */
386 0xffffffff, /* dst_mask */
387 FALSE
), /* pcrel_offset */
389 HOWTO (R_METAG_PLT
, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 FALSE
, /* pc_relative */
395 complain_overflow_bitfield
, /* complain_on_overflow */
396 bfd_elf_generic_reloc
, /* special_function */
397 "R_METAG_GOTOFF", /* name */
398 FALSE
, /* partial_inplace */
399 0xffffffff, /* src_mask */
400 0xffffffff, /* dst_mask */
401 FALSE
), /* pcrel_offset */
403 /* This is used only by the dynamic linker. The symbol should exist
404 both in the object being run and in some shared library. The
405 dynamic linker copies the data addressed by the symbol from the
406 shared library into the object, because the object being
407 run has to have the data at some particular address. */
408 HOWTO (R_METAG_COPY
, /* type */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
412 FALSE
, /* pc_relative */
414 complain_overflow_bitfield
, /* complain_on_overflow */
415 bfd_elf_generic_reloc
, /* special_function */
416 "R_METAG_COPY", /* name */
417 FALSE
, /* partial_inplace */
418 0xffffffff, /* src_mask */
419 0xffffffff, /* dst_mask */
420 FALSE
), /* pcrel_offset */
422 /* Marks a procedure linkage table entry for a symbol. */
423 HOWTO (R_METAG_JMP_SLOT
, /* type */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
427 FALSE
, /* pc_relative */
429 complain_overflow_bitfield
, /* complain_on_overflow */
430 bfd_elf_generic_reloc
, /* special_function */
431 "R_METAG_JMP_SLOT", /* name */
432 FALSE
, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE
), /* pcrel_offset */
437 /* Used only by the dynamic linker. When the object is run, this
438 longword is set to the load address of the object, plus the
440 HOWTO (R_METAG_RELATIVE
, /* type */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
444 FALSE
, /* pc_relative */
446 complain_overflow_bitfield
, /* complain_on_overflow */
447 bfd_elf_generic_reloc
, /* special_function */
448 "R_METAG_RELATIVE", /* name */
449 FALSE
, /* partial_inplace */
450 0xffffffff, /* src_mask */
451 0xffffffff, /* dst_mask */
452 FALSE
), /* pcrel_offset */
454 HOWTO (R_METAG_GLOB_DAT
, /* type */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
458 FALSE
, /* pc_relative */
460 complain_overflow_bitfield
, /* complain_on_overflow */
461 bfd_elf_generic_reloc
, /* special_function */
462 "R_METAG_GLOB_DAT", /* name */
463 FALSE
, /* partial_inplace */
464 0xffffffff, /* src_mask */
465 0xffffffff, /* dst_mask */
466 FALSE
), /* pcrel_offset */
468 HOWTO (R_METAG_TLS_GD
, /* type */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
472 FALSE
, /* pc_relative */
474 complain_overflow_dont
, /* complain_on_overflow */
475 bfd_elf_generic_reloc
, /* special_function */
476 "R_METAG_TLS_GD", /* name */
477 FALSE
, /* partial_inplace */
479 0x0007fff8, /* dst_mask */
480 FALSE
), /* pcrel_offset */
482 HOWTO (R_METAG_TLS_LDM
, /* type */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
486 FALSE
, /* pc_relative */
488 complain_overflow_bitfield
, /* complain_on_overflow */
489 bfd_elf_generic_reloc
, /* special_function */
490 "R_METAG_TLS_LDM", /* name */
491 FALSE
, /* partial_inplace */
493 0x0007fff8, /* dst_mask */
494 FALSE
), /* pcrel_offset */
496 HOWTO (R_METAG_TLS_LDO_HI16
, /* type */
498 2, /* size (0 = byte, 1 = short, 2 = long) */
500 FALSE
, /* pc_relative */
502 complain_overflow_bitfield
, /* complain_on_overflow */
503 bfd_elf_generic_reloc
, /* special_function */
504 "R_METAG_TLS_LDO_HI16", /* name */
505 FALSE
, /* partial_inplace */
507 0x0007fff8, /* dst_mask */
508 FALSE
), /* pcrel_offset */
510 HOWTO (R_METAG_TLS_LDO_LO16
, /* type */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
514 FALSE
, /* pc_relative */
516 complain_overflow_bitfield
, /* complain_on_overflow */
517 bfd_elf_generic_reloc
, /* special_function */
518 "R_METAG_TLS_LDO_LO16", /* name */
519 FALSE
, /* partial_inplace */
521 0x0007fff8, /* dst_mask */
522 FALSE
), /* pcrel_offset */
524 /* Dummy reloc used by the linker internally. */
525 HOWTO (R_METAG_TLS_LDO
, /* type */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
529 FALSE
, /* pc_relative */
531 complain_overflow_bitfield
, /* complain_on_overflow */
532 bfd_elf_generic_reloc
, /* special_function */
533 "R_METAG_TLS_LDO", /* name */
534 FALSE
, /* partial_inplace */
536 0x0007fff8, /* dst_mask */
537 FALSE
), /* pcrel_offset */
539 HOWTO (R_METAG_TLS_IE
, /* type */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
543 FALSE
, /* pc_relative */
545 complain_overflow_dont
, /* complain_on_overflow */
546 bfd_elf_generic_reloc
, /* special_function */
547 "R_METAG_TLS_IE", /* name */
548 FALSE
, /* partial_inplace */
550 0x0007ff80, /* dst_mask */
551 FALSE
), /* pcrel_offset */
553 /* Dummy reloc used by the linker internally. */
554 HOWTO (R_METAG_TLS_IENONPIC
, /* type */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
558 FALSE
, /* pc_relative */
560 complain_overflow_dont
, /* complain_on_overflow */
561 bfd_elf_generic_reloc
, /* special_function */
562 "R_METAG_TLS_IENONPIC", /* name */
563 FALSE
, /* partial_inplace */
565 0x0007fff8, /* dst_mask */
566 FALSE
), /* pcrel_offset */
568 HOWTO (R_METAG_TLS_IENONPIC_HI16
,/* type */
570 2, /* size (0 = byte, 1 = short, 2 = long) */
572 FALSE
, /* pc_relative */
574 complain_overflow_dont
, /* complain_on_overflow */
575 bfd_elf_generic_reloc
, /* special_function */
576 "R_METAG_TLS_IENONPIC_HI16", /* name */
577 FALSE
, /* partial_inplace */
579 0x0007fff8, /* dst_mask */
580 FALSE
), /* pcrel_offset */
582 HOWTO (R_METAG_TLS_IENONPIC_LO16
,/* type */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
586 FALSE
, /* pc_relative */
588 complain_overflow_dont
, /* complain_on_overflow */
589 bfd_elf_generic_reloc
, /* special_function */
590 "R_METAG_TLS_IENONPIC_LO16", /* name */
591 FALSE
, /* partial_inplace */
593 0x0007fff8, /* dst_mask */
594 FALSE
), /* pcrel_offset */
596 HOWTO (R_METAG_TLS_TPOFF
, /* type */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
600 FALSE
, /* pc_relative */
602 complain_overflow_bitfield
, /* complain_on_overflow */
603 bfd_elf_generic_reloc
, /* special_function */
604 "R_METAG_TLS_TPOFF", /* name */
605 FALSE
, /* partial_inplace */
607 0xffffffff, /* dst_mask */
608 FALSE
), /* pcrel_offset */
610 HOWTO (R_METAG_TLS_DTPMOD
, /* type */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
614 FALSE
, /* pc_relative */
616 complain_overflow_bitfield
, /* complain_on_overflow */
617 bfd_elf_generic_reloc
, /* special_function */
618 "R_METAG_TLS_DTPMOD", /* name */
619 FALSE
, /* partial_inplace */
621 0xffffffff, /* dst_mask */
622 FALSE
), /* pcrel_offset */
624 HOWTO (R_METAG_TLS_DTPOFF
, /* type */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
628 FALSE
, /* pc_relative */
630 complain_overflow_bitfield
, /* complain_on_overflow */
631 bfd_elf_generic_reloc
, /* special_function */
632 "R_METAG_TLS_DTPOFF", /* name */
633 FALSE
, /* partial_inplace */
635 0xffffffff, /* dst_mask */
636 FALSE
), /* pcrel_offset */
638 /* Dummy reloc used by the linker internally. */
639 HOWTO (R_METAG_TLS_LE
, /* type */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
643 FALSE
, /* pc_relative */
645 complain_overflow_bitfield
, /* complain_on_overflow */
646 bfd_elf_generic_reloc
, /* special_function */
647 "R_METAG_TLS_LE", /* name */
648 FALSE
, /* partial_inplace */
650 0xffffffff, /* dst_mask */
651 FALSE
), /* pcrel_offset */
653 HOWTO (R_METAG_TLS_LE_HI16
, /* type */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
657 FALSE
, /* pc_relative */
659 complain_overflow_dont
, /* complain_on_overflow */
660 bfd_elf_generic_reloc
, /* special_function */
661 "R_METAG_TLS_LE_HI16", /* name */
662 FALSE
, /* partial_inplace */
664 0x0007fff8, /* dst_mask */
665 FALSE
), /* pcrel_offset */
667 HOWTO (R_METAG_TLS_LE_LO16
, /* type */
669 2, /* size (0 = byte, 1 = short, 2 = long) */
671 FALSE
, /* pc_relative */
673 complain_overflow_dont
, /* complain_on_overflow */
674 bfd_elf_generic_reloc
, /* special_function */
675 "R_METAG_TLS_LE_LO16", /* name */
676 FALSE
, /* partial_inplace */
678 0x0007fff8, /* dst_mask */
679 FALSE
), /* pcrel_offset */
683 #define BRANCH_BITS 19
685 /* The GOT is typically accessed using a [GS]ETD instruction. The size of the
686 immediate offset which can be used in such instructions therefore limits
687 the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
688 is pointing to the base of the GOT then the size is limited to the maximum
689 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
690 in a [GS]ETD instruction is signed, so by setting the base address register
691 to an offset of that 0x2000 byte maximum unsigned offset from the base of
692 the GOT we can use negative offsets in addition to positive. This
693 effectively doubles the usable GOT size to 0x4000 bytes. */
694 #define GOT_REG_OFFSET 0x2000
696 struct metag_reloc_map
698 bfd_reloc_code_real_type bfd_reloc_val
;
699 unsigned int metag_reloc_val
;
702 static const struct metag_reloc_map metag_reloc_map
[] =
704 { BFD_RELOC_NONE
, R_METAG_NONE
},
705 { BFD_RELOC_32
, R_METAG_ADDR32
},
706 { BFD_RELOC_METAG_HIADDR16
, R_METAG_HIADDR16
},
707 { BFD_RELOC_METAG_LOADDR16
, R_METAG_LOADDR16
},
708 { BFD_RELOC_METAG_RELBRANCH
, R_METAG_RELBRANCH
},
709 { BFD_RELOC_METAG_GETSETOFF
, R_METAG_GETSETOFF
},
710 { BFD_RELOC_VTABLE_INHERIT
, R_METAG_GNU_VTINHERIT
},
711 { BFD_RELOC_VTABLE_ENTRY
, R_METAG_GNU_VTENTRY
},
712 { BFD_RELOC_METAG_REL8
, R_METAG_REL8
},
713 { BFD_RELOC_METAG_REL16
, R_METAG_REL16
},
714 { BFD_RELOC_METAG_HI16_GOTOFF
, R_METAG_HI16_GOTOFF
},
715 { BFD_RELOC_METAG_LO16_GOTOFF
, R_METAG_LO16_GOTOFF
},
716 { BFD_RELOC_METAG_GETSET_GOTOFF
, R_METAG_GETSET_GOTOFF
},
717 { BFD_RELOC_METAG_GETSET_GOT
, R_METAG_GETSET_GOT
},
718 { BFD_RELOC_METAG_HI16_GOTPC
, R_METAG_HI16_GOTPC
},
719 { BFD_RELOC_METAG_LO16_GOTPC
, R_METAG_LO16_GOTPC
},
720 { BFD_RELOC_METAG_HI16_PLT
, R_METAG_HI16_PLT
},
721 { BFD_RELOC_METAG_LO16_PLT
, R_METAG_LO16_PLT
},
722 { BFD_RELOC_METAG_RELBRANCH_PLT
, R_METAG_RELBRANCH_PLT
},
723 { BFD_RELOC_METAG_GOTOFF
, R_METAG_GOTOFF
},
724 { BFD_RELOC_METAG_PLT
, R_METAG_PLT
},
725 { BFD_RELOC_METAG_COPY
, R_METAG_COPY
},
726 { BFD_RELOC_METAG_JMP_SLOT
, R_METAG_JMP_SLOT
},
727 { BFD_RELOC_METAG_RELATIVE
, R_METAG_RELATIVE
},
728 { BFD_RELOC_METAG_GLOB_DAT
, R_METAG_GLOB_DAT
},
729 { BFD_RELOC_METAG_TLS_GD
, R_METAG_TLS_GD
},
730 { BFD_RELOC_METAG_TLS_LDM
, R_METAG_TLS_LDM
},
731 { BFD_RELOC_METAG_TLS_LDO_HI16
, R_METAG_TLS_LDO_HI16
},
732 { BFD_RELOC_METAG_TLS_LDO_LO16
, R_METAG_TLS_LDO_LO16
},
733 { BFD_RELOC_METAG_TLS_LDO
, R_METAG_TLS_LDO
},
734 { BFD_RELOC_METAG_TLS_IE
, R_METAG_TLS_IE
},
735 { BFD_RELOC_METAG_TLS_IENONPIC
, R_METAG_TLS_IENONPIC
},
736 { BFD_RELOC_METAG_TLS_IENONPIC_HI16
, R_METAG_TLS_IENONPIC_HI16
},
737 { BFD_RELOC_METAG_TLS_IENONPIC_LO16
, R_METAG_TLS_IENONPIC_LO16
},
738 { BFD_RELOC_METAG_TLS_TPOFF
, R_METAG_TLS_TPOFF
},
739 { BFD_RELOC_METAG_TLS_DTPMOD
, R_METAG_TLS_DTPMOD
},
740 { BFD_RELOC_METAG_TLS_DTPOFF
, R_METAG_TLS_DTPOFF
},
741 { BFD_RELOC_METAG_TLS_LE
, R_METAG_TLS_LE
},
742 { BFD_RELOC_METAG_TLS_LE_HI16
, R_METAG_TLS_LE_HI16
},
743 { BFD_RELOC_METAG_TLS_LE_LO16
, R_METAG_TLS_LE_LO16
},
746 enum elf_metag_stub_type
748 metag_stub_long_branch
,
749 metag_stub_long_branch_shared
,
753 struct elf_metag_stub_hash_entry
755 /* Base hash table entry structure. */
756 struct bfd_hash_entry bh_root
;
758 /* The stub section. */
761 /* Offset within stub_sec of the beginning of this stub. */
764 /* Given the symbol's value and its section we can determine its final
765 value when building the stubs (so the stub knows where to jump. */
766 bfd_vma target_value
;
767 asection
*target_section
;
769 enum elf_metag_stub_type stub_type
;
771 /* The symbol table entry, if any, that this was derived from. */
772 struct elf_metag_link_hash_entry
*hh
;
774 /* And the reloc addend that this was derived from. */
777 /* Where this stub is being called from, or, in the case of combined
778 stub sections, the first input section in the group. */
782 struct elf_metag_link_hash_entry
784 struct elf_link_hash_entry eh
;
786 /* A pointer to the most recently used stub hash entry against this
788 struct elf_metag_stub_hash_entry
*hsh_cache
;
790 /* Used to count relocations for delayed sizing of relocation
792 struct elf_metag_dyn_reloc_entry
{
794 /* Next relocation in the chain. */
795 struct elf_metag_dyn_reloc_entry
*hdh_next
;
797 /* The input section of the reloc. */
800 /* Number of relocs copied in this section. */
803 /* Number of relative relocs copied for the input section. */
804 bfd_size_type relative_count
;
809 GOT_UNKNOWN
= 0, GOT_NORMAL
= 1, GOT_TLS_IE
= 2, GOT_TLS_LDM
= 4, GOT_TLS_GD
= 8
813 struct elf_metag_link_hash_table
815 /* The main hash table. */
816 struct elf_link_hash_table etab
;
818 /* The stub hash table. */
819 struct bfd_hash_table bstab
;
821 /* Linker stub bfd. */
824 /* Linker call-backs. */
825 asection
* (*add_stub_section
) (const char *, asection
*);
826 void (*layout_sections_again
) (void);
828 /* Array to keep track of which stub sections have been created, and
829 information on stub grouping. */
832 /* This is the section to which stubs in the group will be
835 /* The stub section. */
839 /* Assorted information used by elf_metag_size_stubs. */
840 unsigned int bfd_count
;
841 unsigned int top_index
;
842 asection
**input_list
;
843 Elf_Internal_Sym
**all_local_syms
;
845 /* Short-cuts to get to dynamic linker sections. */
849 /* Small local sym cache. */
850 struct sym_cache sym_cache
;
852 /* Data for LDM relocations. */
855 bfd_signed_vma refcount
;
860 /* Return the base vma address which should be subtracted from the
861 real address when resolving a dtpoff relocation. This is PT_TLS
864 dtpoff_base (struct bfd_link_info
*info
)
866 /* If tls_sec is NULL, we should have signalled an error already. */
867 if (elf_hash_table (info
)->tls_sec
== NULL
)
869 return elf_hash_table (info
)->tls_sec
->vma
;
872 /* Return the relocation value for R_METAG_TLS_IE */
874 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
876 /* If tls_sec is NULL, we should have signalled an error already. */
877 if (elf_hash_table (info
)->tls_sec
== NULL
)
879 /* METAG TLS ABI is variant I and static TLS blocks start just after
880 tcbhead structure which has 2 pointer fields. */
881 return (address
- elf_hash_table (info
)->tls_sec
->vma
882 + align_power ((bfd_vma
) 8,
883 elf_hash_table (info
)->tls_sec
->alignment_power
));
887 metag_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
889 Elf_Internal_Rela
*dst
)
893 r_type
= ELF32_R_TYPE (dst
->r_info
);
894 if (r_type
>= (unsigned int) R_METAG_MAX
)
896 /* xgettext:c-format */
897 _bfd_error_handler (_("%B: invalid METAG reloc number: %d"), abfd
, r_type
);
900 cache_ptr
->howto
= & elf_metag_howto_table
[r_type
];
903 static reloc_howto_type
*
904 metag_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
905 bfd_reloc_code_real_type code
)
909 for (i
= 0; i
< sizeof (metag_reloc_map
) / sizeof (metag_reloc_map
[0]); i
++)
910 if (metag_reloc_map
[i
].bfd_reloc_val
== code
)
911 return & elf_metag_howto_table
[metag_reloc_map
[i
].metag_reloc_val
];
916 static reloc_howto_type
*
917 metag_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
922 for (i
= 0; i
< sizeof (elf_metag_howto_table
) / sizeof (elf_metag_howto_table
[0]); i
++)
923 if (elf_metag_howto_table
[i
].name
!= NULL
924 && strcasecmp (elf_metag_howto_table
[i
].name
, r_name
) == 0)
925 return &elf_metag_howto_table
[i
];
930 /* Various hash macros and functions. */
931 #define metag_link_hash_table(p) \
932 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
933 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
935 #define metag_elf_hash_entry(ent) \
936 ((struct elf_metag_link_hash_entry *)(ent))
938 #define metag_stub_hash_entry(ent) \
939 ((struct elf_metag_stub_hash_entry *)(ent))
941 #define metag_stub_hash_lookup(table, string, create, copy) \
942 ((struct elf_metag_stub_hash_entry *) \
943 bfd_hash_lookup ((table), (string), (create), (copy)))
945 #define metag_elf_local_got_tls_type(abfd) \
946 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
948 /* Assorted hash table functions. */
950 /* Initialize an entry in the stub hash table. */
952 static struct bfd_hash_entry
*
953 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
954 struct bfd_hash_table
*table
,
957 /* Allocate the structure if it has not already been allocated by a
961 entry
= bfd_hash_allocate (table
,
962 sizeof (struct elf_metag_stub_hash_entry
));
967 /* Call the allocation method of the superclass. */
968 entry
= bfd_hash_newfunc (entry
, table
, string
);
971 struct elf_metag_stub_hash_entry
*hsh
;
973 /* Initialize the local fields. */
974 hsh
= (struct elf_metag_stub_hash_entry
*) entry
;
975 hsh
->stub_sec
= NULL
;
976 hsh
->stub_offset
= 0;
977 hsh
->target_value
= 0;
978 hsh
->target_section
= NULL
;
979 hsh
->stub_type
= metag_stub_long_branch
;
987 /* Initialize an entry in the link hash table. */
989 static struct bfd_hash_entry
*
990 metag_link_hash_newfunc (struct bfd_hash_entry
*entry
,
991 struct bfd_hash_table
*table
,
994 /* Allocate the structure if it has not already been allocated by a
998 entry
= bfd_hash_allocate (table
,
999 sizeof (struct elf_metag_link_hash_entry
));
1004 /* Call the allocation method of the superclass. */
1005 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1008 struct elf_metag_link_hash_entry
*hh
;
1010 /* Initialize the local fields. */
1011 hh
= (struct elf_metag_link_hash_entry
*) entry
;
1012 hh
->hsh_cache
= NULL
;
1013 hh
->dyn_relocs
= NULL
;
1014 hh
->tls_type
= GOT_UNKNOWN
;
1020 /* Free the derived linker hash table. */
1023 elf_metag_link_hash_table_free (bfd
*obfd
)
1025 struct elf_metag_link_hash_table
*htab
1026 = (struct elf_metag_link_hash_table
*) obfd
->link
.hash
;
1028 bfd_hash_table_free (&htab
->bstab
);
1029 _bfd_elf_link_hash_table_free (obfd
);
1032 /* Create the derived linker hash table. The Meta ELF port uses the derived
1033 hash table to keep information specific to the Meta ELF linker (without
1034 using static variables). */
1036 static struct bfd_link_hash_table
*
1037 elf_metag_link_hash_table_create (bfd
*abfd
)
1039 struct elf_metag_link_hash_table
*htab
;
1040 bfd_size_type amt
= sizeof (*htab
);
1042 htab
= bfd_zmalloc (amt
);
1046 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
1047 metag_link_hash_newfunc
,
1048 sizeof (struct elf_metag_link_hash_entry
),
1055 /* Init the stub hash table too. */
1056 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
1057 sizeof (struct elf_metag_stub_hash_entry
)))
1059 _bfd_elf_link_hash_table_free (abfd
);
1062 htab
->etab
.root
.hash_table_free
= elf_metag_link_hash_table_free
;
1064 return &htab
->etab
.root
;
1067 /* Section name for stubs is the associated section name plus this
1069 #define STUB_SUFFIX ".stub"
1071 /* Build a name for an entry in the stub hash table. */
1074 metag_stub_name (const asection
*input_section
,
1075 const asection
*sym_sec
,
1076 const struct elf_metag_link_hash_entry
*hh
,
1077 const Elf_Internal_Rela
*rel
)
1084 len
= 8 + 1 + strlen (hh
->eh
.root
.root
.string
) + 1 + 8 + 1;
1085 stub_name
= bfd_malloc (len
);
1086 if (stub_name
!= NULL
)
1088 sprintf (stub_name
, "%08x_%s+%x",
1089 input_section
->id
& 0xffffffff,
1090 hh
->eh
.root
.root
.string
,
1091 (int) rel
->r_addend
& 0xffffffff);
1096 len
= 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1097 stub_name
= bfd_malloc (len
);
1098 if (stub_name
!= NULL
)
1100 sprintf (stub_name
, "%08x_%x:%x+%x",
1101 input_section
->id
& 0xffffffff,
1102 sym_sec
->id
& 0xffffffff,
1103 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1104 (int) rel
->r_addend
& 0xffffffff);
1110 /* Look up an entry in the stub hash. Stub entries are cached because
1111 creating the stub name takes a bit of time. */
1113 static struct elf_metag_stub_hash_entry
*
1114 metag_get_stub_entry (const asection
*input_section
,
1115 const asection
*sym_sec
,
1116 struct elf_metag_link_hash_entry
*hh
,
1117 const Elf_Internal_Rela
*rel
,
1118 struct elf_metag_link_hash_table
*htab
)
1120 struct elf_metag_stub_hash_entry
*hsh
;
1121 const asection
*id_sec
;
1123 /* If this input section is part of a group of sections sharing one
1124 stub section, then use the id of the first section in the group.
1125 Stub names need to include a section id, as there may well be
1126 more than one stub used to reach say, printf, and we need to
1127 distinguish between them. */
1128 id_sec
= htab
->stub_group
[input_section
->id
].link_sec
;
1130 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1131 && hh
->hsh_cache
->hh
== hh
1132 && hh
->hsh_cache
->id_sec
== id_sec
)
1134 hsh
= hh
->hsh_cache
;
1140 stub_name
= metag_stub_name (id_sec
, sym_sec
, hh
, rel
);
1141 if (stub_name
== NULL
)
1144 hsh
= metag_stub_hash_lookup (&htab
->bstab
,
1145 stub_name
, FALSE
, FALSE
);
1148 hh
->hsh_cache
= hsh
;
1156 /* Add a new stub entry to the stub hash. Not all fields of the new
1157 stub entry are initialised. */
1159 static struct elf_metag_stub_hash_entry
*
1160 metag_add_stub (const char *stub_name
,
1162 struct elf_metag_link_hash_table
*htab
)
1166 struct elf_metag_stub_hash_entry
*hsh
;
1168 link_sec
= htab
->stub_group
[section
->id
].link_sec
;
1169 stub_sec
= htab
->stub_group
[section
->id
].stub_sec
;
1170 if (stub_sec
== NULL
)
1172 stub_sec
= htab
->stub_group
[link_sec
->id
].stub_sec
;
1173 if (stub_sec
== NULL
)
1179 namelen
= strlen (link_sec
->name
);
1180 len
= namelen
+ sizeof (STUB_SUFFIX
);
1181 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1185 memcpy (s_name
, link_sec
->name
, namelen
);
1186 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1188 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
);
1189 if (stub_sec
== NULL
)
1191 htab
->stub_group
[link_sec
->id
].stub_sec
= stub_sec
;
1193 htab
->stub_group
[section
->id
].stub_sec
= stub_sec
;
1196 /* Enter this entry into the linker stub hash table. */
1197 hsh
= metag_stub_hash_lookup (&htab
->bstab
, stub_name
,
1201 /* xgettext:c-format */
1202 _bfd_error_handler (_("%B: cannot create stub entry %s"),
1203 section
->owner
, stub_name
);
1207 hsh
->stub_sec
= stub_sec
;
1208 hsh
->stub_offset
= 0;
1209 hsh
->id_sec
= link_sec
;
1213 /* Check a signed integer value can be represented in the given number
1217 within_signed_range (int value
, unsigned int bits
)
1219 int min_val
= -(1 << (bits
- 1));
1220 int max_val
= (1 << (bits
- 1)) - 1;
1221 return (value
<= max_val
) && (value
>= min_val
);
1224 /* Perform a relocation as part of a final link. */
1226 static bfd_reloc_status_type
1227 metag_final_link_relocate (reloc_howto_type
*howto
,
1229 asection
*input_section
,
1231 Elf_Internal_Rela
*rel
,
1233 struct elf_metag_link_hash_entry
*hh
,
1234 struct elf_metag_link_hash_table
*htab
,
1237 bfd_reloc_status_type r
= bfd_reloc_ok
;
1238 bfd_byte
*hit_data
= contents
+ rel
->r_offset
;
1239 int opcode
, op_shift
, op_extended
, l1
, l2
;
1240 bfd_signed_vma srel
, addend
= rel
->r_addend
;
1241 struct elf_metag_stub_hash_entry
*hsh
= NULL
;
1244 /* Find out where we are and where we're going. */
1245 location
= (rel
->r_offset
+
1246 input_section
->output_offset
+
1247 input_section
->output_section
->vma
);
1249 switch (howto
->type
)
1251 case R_METAG_RELBRANCH
:
1252 case R_METAG_RELBRANCH_PLT
:
1253 /* Make it a pc relative offset. */
1254 relocation
-= location
;
1256 case R_METAG_TLS_GD
:
1257 case R_METAG_TLS_IE
:
1258 relocation
-= elf_gp (input_section
->output_section
->owner
);
1264 switch (howto
->type
)
1266 case R_METAG_RELBRANCH_PLT
:
1267 case R_METAG_RELBRANCH
:
1268 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1270 srel
= (bfd_signed_vma
) relocation
;
1273 /* If the branch is out of reach, then redirect the
1274 call to the local stub for this function. */
1275 if (srel
> ((1 << (BRANCH_BITS
+ 1)) - 1) ||
1276 (srel
< - (1 << (BRANCH_BITS
+ 1))))
1278 if (sym_sec
== NULL
)
1281 hsh
= metag_get_stub_entry (input_section
, sym_sec
,
1284 return bfd_reloc_undefined
;
1286 /* Munge up the value and addend so that we call the stub
1287 rather than the procedure directly. */
1288 srel
= (hsh
->stub_offset
1289 + hsh
->stub_sec
->output_offset
1290 + hsh
->stub_sec
->output_section
->vma
);
1296 if (!within_signed_range (srel
, BRANCH_BITS
))
1298 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1301 return bfd_reloc_overflow
;
1304 opcode
&= ~(0x7ffff << 5);
1305 opcode
|= ((srel
& 0x7ffff) << 5);
1307 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1309 case R_METAG_GETSETOFF
:
1310 case R_METAG_GETSET_GOT
:
1311 case R_METAG_GETSET_GOTOFF
:
1312 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1314 srel
= (bfd_signed_vma
) relocation
;
1317 /* Is this a standard or extended GET/SET? */
1318 if ((opcode
& 0xf0000000) == 0xa0000000)
1320 /* Extended GET/SET. */
1327 /* Standard GET/SET. */
1328 l1
= opcode
& 0x01000000;
1329 l2
= opcode
& 0x04000000;
1333 /* Calculate the width of the GET/SET and how much we need to
1334 shift the result by. */
1346 /* GET/SET offsets are scaled by the width of the transfer. */
1347 srel
= srel
>> op_shift
;
1349 /* Extended GET/SET has signed 12 bits of offset, standard has
1353 if (!within_signed_range (srel
, 12))
1355 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1358 return bfd_reloc_overflow
;
1360 opcode
&= ~(0xfff << 7);
1361 opcode
|= ((srel
& 0xfff) << 7);
1365 if (!within_signed_range (srel
, 5))
1367 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1370 return bfd_reloc_overflow
;
1372 opcode
&= ~(0x3f << 8);
1373 opcode
|= ((srel
& 0x3f) << 8);
1376 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1378 case R_METAG_TLS_GD
:
1379 case R_METAG_TLS_LDM
:
1380 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1382 if ((bfd_signed_vma
)relocation
< 0)
1384 /* sign extend immediate */
1385 if ((opcode
& 0xf2000001) == 0x02000000)
1387 /* ADD De.e,Dx.r,#I16 */
1391 return bfd_reloc_overflow
;
1394 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1396 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1397 contents
, rel
->r_offset
,
1398 relocation
, rel
->r_addend
);
1401 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1402 contents
, rel
->r_offset
,
1403 relocation
, rel
->r_addend
);
1409 /* This is defined because R_METAG_NONE != 0...
1410 See RELOC_AGAINST_DISCARDED_SECTION for details. */
1411 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1412 rel, relend, howto, contents) \
1414 _bfd_clear_contents (howto, input_bfd, input_section, \
1415 contents + rel->r_offset); \
1417 if (bfd_link_relocatable (info) \
1418 && (input_section->flags & SEC_DEBUGGING)) \
1420 /* Only remove relocations in debug sections since other \
1421 sections may require relocations. */ \
1422 Elf_Internal_Shdr *rel_hdr; \
1424 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1426 /* Avoid empty output section. */ \
1427 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
1429 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1430 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
1431 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1433 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
1435 input_section->reloc_count--; \
1442 rel->r_info = R_METAG_NONE; \
1443 rel->r_addend = 0; \
1447 /* Relocate a META ELF section.
1449 The RELOCATE_SECTION function is called by the new ELF backend linker
1450 to handle the relocations for a section.
1452 The relocs are always passed as Rela structures; if the section
1453 actually uses Rel structures, the r_addend field will always be
1456 This function is responsible for adjusting the section contents as
1457 necessary, and (if using Rela relocs and generating a relocatable
1458 output file) adjusting the reloc addend as necessary.
1460 This function does not have to worry about setting the reloc
1461 address or the reloc symbol index.
1463 LOCAL_SYMS is a pointer to the swapped in local symbols.
1465 LOCAL_SECTIONS is an array giving the section in the input file
1466 corresponding to the st_shndx field of each local symbol.
1468 The global hash table entry for the global symbols can be found
1469 via elf_sym_hashes (input_bfd).
1471 When generating relocatable output, this function must handle
1472 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1473 going to be the section symbol corresponding to the output
1474 section, which means that the addend must be adjusted
1478 elf_metag_relocate_section (bfd
*output_bfd
,
1479 struct bfd_link_info
*info
,
1481 asection
*input_section
,
1483 Elf_Internal_Rela
*relocs
,
1484 Elf_Internal_Sym
*local_syms
,
1485 asection
**local_sections
)
1487 bfd_vma
*local_got_offsets
;
1488 Elf_Internal_Shdr
*symtab_hdr
;
1489 struct elf_link_hash_entry
**eh_syms
;
1490 struct elf_metag_link_hash_table
*htab
;
1491 Elf_Internal_Rela
*rel
;
1492 Elf_Internal_Rela
*relend
;
1495 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1496 eh_syms
= elf_sym_hashes (input_bfd
);
1497 relend
= relocs
+ input_section
->reloc_count
;
1499 htab
= metag_link_hash_table (info
);
1500 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1504 for (rel
= relocs
; rel
< relend
; rel
++)
1506 reloc_howto_type
*howto
;
1507 unsigned long r_symndx
;
1508 Elf_Internal_Sym
*sym
;
1510 struct elf_metag_link_hash_entry
*hh
;
1512 bfd_reloc_status_type r
;
1516 r_type
= ELF32_R_TYPE (rel
->r_info
);
1518 if (r_type
== R_METAG_GNU_VTINHERIT
1519 || r_type
== R_METAG_GNU_VTENTRY
1520 || r_type
== R_METAG_NONE
)
1523 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1525 howto
= elf_metag_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
1530 if (r_symndx
< symtab_hdr
->sh_info
)
1532 sym
= local_syms
+ r_symndx
;
1533 sec
= local_sections
[r_symndx
];
1534 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1536 name
= bfd_elf_string_from_elf_section
1537 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1538 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1542 struct elf_link_hash_entry
*eh
;
1543 bfd_boolean unresolved_reloc
, warned
, ignored
;
1545 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1546 r_symndx
, symtab_hdr
, eh_syms
,
1547 eh
, sec
, relocation
,
1548 unresolved_reloc
, warned
, ignored
);
1550 name
= eh
->root
.root
.string
;
1551 hh
= (struct elf_metag_link_hash_entry
*) eh
;
1554 if (sec
!= NULL
&& discarded_section (sec
))
1555 METAG_RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1556 rel
, relend
, howto
, contents
);
1558 if (bfd_link_relocatable (info
))
1563 case R_METAG_ADDR32
:
1564 case R_METAG_RELBRANCH
:
1565 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1568 if ((bfd_link_pic (info
)
1569 && r_symndx
!= STN_UNDEF
1570 && (input_section
->flags
& SEC_ALLOC
) != 0
1571 && (r_type
!= R_METAG_RELBRANCH
1572 || !SYMBOL_CALLS_LOCAL (info
, &hh
->eh
)))
1573 || (!bfd_link_pic (info
)
1575 && hh
->eh
.dynindx
!= -1
1576 && !hh
->eh
.non_got_ref
1577 && ((hh
->eh
.def_dynamic
1578 && !hh
->eh
.def_regular
)
1579 || hh
->eh
.root
.type
== bfd_link_hash_undefweak
1580 || hh
->eh
.root
.type
== bfd_link_hash_undefined
)))
1582 Elf_Internal_Rela outrel
;
1583 bfd_boolean skip
, relocate
;
1586 /* When generating a shared object, these relocations
1587 are copied into the output file to be resolved at run
1590 sreloc
= elf_section_data (input_section
)->sreloc
;
1591 BFD_ASSERT (sreloc
!= NULL
);
1596 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
1600 if (outrel
.r_offset
== (bfd_vma
) -1)
1602 else if (outrel
.r_offset
== (bfd_vma
) -2)
1603 skip
= TRUE
, relocate
= TRUE
;
1604 outrel
.r_offset
+= (input_section
->output_section
->vma
1605 + input_section
->output_offset
);
1609 memset (&outrel
, 0, sizeof outrel
);
1610 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_NONE
);
1612 else if (r_type
== R_METAG_RELBRANCH
)
1614 BFD_ASSERT (hh
!= NULL
&& hh
->eh
.dynindx
!= -1);
1615 outrel
.r_info
= ELF32_R_INFO (hh
->eh
.dynindx
, r_type
);
1616 outrel
.r_addend
= rel
->r_addend
;
1620 /* h->dynindx may be -1 if this symbol was marked to
1623 || ((info
->symbolic
|| hh
->eh
.dynindx
== -1)
1624 && hh
->eh
.def_regular
))
1627 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
1628 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1632 BFD_ASSERT (hh
->eh
.dynindx
!= -1);
1633 outrel
.r_info
= ELF32_R_INFO (hh
->eh
.dynindx
, r_type
);
1634 outrel
.r_addend
= rel
->r_addend
;
1638 loc
= sreloc
->contents
;
1639 loc
+= sreloc
->reloc_count
* sizeof(Elf32_External_Rela
);
1640 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,loc
);
1641 ++sreloc
->reloc_count
;
1643 /* If this reloc is against an external symbol, we do
1644 not want to fiddle with the addend. Otherwise, we
1645 need to include the symbol value so that it becomes
1646 an addend for the dynamic reloc. */
1652 case R_METAG_RELBRANCH_PLT
:
1653 /* Relocation is to the entry for this symbol in the
1654 procedure linkage table. */
1659 if (hh
->eh
.forced_local
)
1662 if (hh
->eh
.plt
.offset
== (bfd_vma
) -1 || htab
->etab
.splt
== NULL
)
1664 /* We didn't make a PLT entry for this symbol. This
1665 happens when statically linking PIC code, or when
1666 using -Bsymbolic. */
1670 relocation
= (htab
->etab
.splt
->output_section
->vma
1671 + htab
->etab
.splt
->output_offset
1672 + hh
->eh
.plt
.offset
);
1674 case R_METAG_HI16_GOTPC
:
1675 case R_METAG_LO16_GOTPC
:
1676 BFD_ASSERT (htab
->etab
.sgot
!= NULL
);
1678 relocation
= (htab
->etab
.sgot
->output_section
->vma
+
1679 htab
->etab
.sgot
->output_offset
);
1680 relocation
+= GOT_REG_OFFSET
;
1681 relocation
-= (input_section
->output_section
->vma
1682 + input_section
->output_offset
1685 case R_METAG_HI16_GOTOFF
:
1686 case R_METAG_LO16_GOTOFF
:
1687 case R_METAG_GETSET_GOTOFF
:
1688 BFD_ASSERT (htab
->etab
.sgot
!= NULL
);
1690 relocation
-= (htab
->etab
.sgot
->output_section
->vma
+
1691 htab
->etab
.sgot
->output_offset
);
1692 relocation
-= GOT_REG_OFFSET
;
1694 case R_METAG_GETSET_GOT
:
1697 bfd_boolean do_got
= 0;
1699 /* Relocation is to the entry for this symbol in the
1700 global offset table. */
1705 off
= hh
->eh
.got
.offset
;
1706 dyn
= htab
->etab
.dynamic_sections_created
;
1707 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
1708 bfd_link_pic (info
),
1711 /* If we aren't going to call finish_dynamic_symbol,
1712 then we need to handle initialisation of the .got
1713 entry and create needed relocs here. Since the
1714 offset must always be a multiple of 4, we use the
1715 least significant bit to record whether we have
1716 initialised it already. */
1721 hh
->eh
.got
.offset
|= 1;
1728 /* Local symbol case. */
1729 if (local_got_offsets
== NULL
)
1732 off
= local_got_offsets
[r_symndx
];
1734 /* The offset must always be a multiple of 4. We use
1735 the least significant bit to record whether we have
1736 already generated the necessary reloc. */
1741 local_got_offsets
[r_symndx
] |= 1;
1748 if (bfd_link_pic (info
))
1750 /* Output a dynamic relocation for this GOT entry.
1751 In this case it is relative to the base of the
1752 object because the symbol index is zero. */
1753 Elf_Internal_Rela outrel
;
1755 asection
*s
= htab
->etab
.srelgot
;
1757 outrel
.r_offset
= (off
1758 + htab
->etab
.sgot
->output_offset
1759 + htab
->etab
.sgot
->output_section
->vma
);
1760 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
1761 outrel
.r_addend
= relocation
;
1763 loc
+= s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
1764 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1767 bfd_put_32 (output_bfd
, relocation
,
1768 htab
->etab
.sgot
->contents
+ off
);
1771 if (off
>= (bfd_vma
) -2)
1774 relocation
= off
- GOT_REG_OFFSET
;
1777 case R_METAG_TLS_GD
:
1778 case R_METAG_TLS_IE
:
1780 /* XXXMJF There is room here for optimisations. For example
1781 converting from GD->IE, etc. */
1786 if (htab
->etab
.sgot
== NULL
)
1793 dyn
= htab
->etab
.dynamic_sections_created
;
1795 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
1796 bfd_link_pic (info
),
1798 && (!bfd_link_pic (info
)
1799 || !SYMBOL_REFERENCES_LOCAL (info
, &hh
->eh
)))
1801 indx
= hh
->eh
.dynindx
;
1803 off
= hh
->eh
.got
.offset
;
1804 tls_type
= hh
->tls_type
;
1808 /* Local symbol case. */
1809 if (local_got_offsets
== NULL
)
1812 off
= local_got_offsets
[r_symndx
];
1813 tls_type
= metag_elf_local_got_tls_type (input_bfd
) [r_symndx
];
1816 if (tls_type
== GOT_UNKNOWN
)
1823 bfd_boolean need_relocs
= FALSE
;
1824 Elf_Internal_Rela outrel
;
1825 bfd_byte
*loc
= NULL
;
1828 /* The GOT entries have not been initialized yet. Do it
1829 now, and emit any relocations. If both an IE GOT and a
1830 GD GOT are necessary, we emit the GD first. */
1832 if ((bfd_link_pic (info
) || indx
!= 0)
1834 || ELF_ST_VISIBILITY (hh
->eh
.other
) == STV_DEFAULT
1835 || hh
->eh
.root
.type
!= bfd_link_hash_undefweak
))
1838 loc
= htab
->etab
.srelgot
->contents
;
1839 /* FIXME (CAO): Should this be reloc_count++ ? */
1840 loc
+= htab
->etab
.srelgot
->reloc_count
* sizeof (Elf32_External_Rela
);
1843 if (tls_type
& GOT_TLS_GD
)
1847 outrel
.r_offset
= (cur_off
1848 + htab
->etab
.sgot
->output_section
->vma
1849 + htab
->etab
.sgot
->output_offset
);
1850 outrel
.r_info
= ELF32_R_INFO (indx
, R_METAG_TLS_DTPMOD
);
1851 outrel
.r_addend
= 0;
1852 bfd_put_32 (output_bfd
, 0, htab
->etab
.sgot
->contents
+ cur_off
);
1854 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1855 htab
->etab
.srelgot
->reloc_count
++;
1856 loc
+= sizeof (Elf32_External_Rela
);
1859 bfd_put_32 (output_bfd
, 0,
1860 htab
->etab
.sgot
->contents
+ cur_off
+ 4);
1863 bfd_put_32 (output_bfd
, 0,
1864 htab
->etab
.sgot
->contents
+ cur_off
+ 4);
1865 outrel
.r_info
= ELF32_R_INFO (indx
,
1866 R_METAG_TLS_DTPOFF
);
1867 outrel
.r_offset
+= 4;
1868 bfd_elf32_swap_reloca_out (output_bfd
,
1870 htab
->etab
.srelgot
->reloc_count
++;
1871 loc
+= sizeof (Elf32_External_Rela
);
1876 /* We don't support changing the TLS model. */
1878 if (bfd_link_pic (info
))
1879 _bfd_error_handler (_("%B(%A): multiple TLS models are not supported"),
1880 input_bfd
, input_section
, name
);
1882 _bfd_error_handler (_("%B(%A): shared library symbol %s encountered whilst performing a static link"),
1883 input_bfd
, input_section
, name
);
1890 if (tls_type
& GOT_TLS_IE
)
1894 outrel
.r_offset
= (cur_off
1895 + htab
->etab
.sgot
->output_section
->vma
1896 + htab
->etab
.sgot
->output_offset
);
1897 outrel
.r_info
= ELF32_R_INFO (indx
, R_METAG_TLS_TPOFF
);
1900 outrel
.r_addend
= relocation
- dtpoff_base (info
);
1902 outrel
.r_addend
= 0;
1904 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1905 htab
->etab
.srelgot
->reloc_count
++;
1906 loc
+= sizeof (Elf32_External_Rela
);
1909 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
1910 htab
->etab
.sgot
->contents
+ cur_off
);
1916 hh
->eh
.got
.offset
|= 1;
1918 local_got_offsets
[r_symndx
] |= 1;
1921 /* Add the base of the GOT to the relocation value. */
1922 relocation
= off
- GOT_REG_OFFSET
;
1927 case R_METAG_TLS_IENONPIC_HI16
:
1928 case R_METAG_TLS_IENONPIC_LO16
:
1929 case R_METAG_TLS_LE_HI16
:
1930 case R_METAG_TLS_LE_LO16
:
1931 if (bfd_link_pic (info
))
1934 /* xgettext:c-format */
1935 (_("%B(%A+0x%lx): R_METAG_TLS_LE/IENONPIC relocation not permitted in shared object"),
1936 input_bfd
, input_section
,
1937 (long) rel
->r_offset
, howto
->name
);
1941 relocation
= tpoff (info
, relocation
);
1943 case R_METAG_TLS_LDO_HI16
:
1944 case R_METAG_TLS_LDO_LO16
:
1945 if (! bfd_link_pic (info
))
1946 relocation
= tpoff (info
, relocation
);
1948 relocation
-= dtpoff_base (info
);
1950 case R_METAG_TLS_LDM
:
1954 if (htab
->etab
.sgot
== NULL
)
1956 off
= htab
->tls_ldm_got
.offset
;
1961 Elf_Internal_Rela outrel
;
1964 outrel
.r_offset
= (off
1965 + htab
->etab
.sgot
->output_section
->vma
1966 + htab
->etab
.sgot
->output_offset
);
1968 outrel
.r_addend
= 0;
1969 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_TLS_DTPMOD
);
1970 loc
= htab
->etab
.srelgot
->contents
;
1971 loc
+= htab
->etab
.srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
1972 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1973 htab
->tls_ldm_got
.offset
|= 1;
1976 relocation
= off
- GOT_REG_OFFSET
;
1983 r
= metag_final_link_relocate (howto
, input_bfd
, input_section
,
1984 contents
, rel
, relocation
, hh
, htab
,
1987 if (r
!= bfd_reloc_ok
)
1989 const char * msg
= (const char *) NULL
;
1993 case bfd_reloc_overflow
:
1994 (*info
->callbacks
->reloc_overflow
)
1995 (info
, (hh
? &hh
->eh
.root
: NULL
), name
, howto
->name
,
1996 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
1999 case bfd_reloc_undefined
:
2000 (*info
->callbacks
->undefined_symbol
)
2001 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
2004 case bfd_reloc_outofrange
:
2005 msg
= _("internal error: out of range error");
2008 case bfd_reloc_notsupported
:
2009 msg
= _("internal error: unsupported relocation error");
2012 case bfd_reloc_dangerous
:
2013 msg
= _("internal error: dangerous relocation");
2017 msg
= _("internal error: unknown error");
2022 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
2023 input_section
, rel
->r_offset
);
2030 /* Create the .plt and .got sections, and set up our hash table
2031 short-cuts to various dynamic sections. */
2034 elf_metag_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2036 struct elf_metag_link_hash_table
*htab
;
2037 struct elf_link_hash_entry
*eh
;
2038 struct bfd_link_hash_entry
*bh
;
2039 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2041 /* Don't try to create the .plt and .got twice. */
2042 htab
= metag_link_hash_table (info
);
2043 if (htab
->etab
.splt
!= NULL
)
2046 /* Call the generic code to do most of the work. */
2047 if (! _bfd_elf_create_dynamic_sections (abfd
, info
))
2050 /* The header goes at the start of the dynamic .got section, which
2051 is placed after the dynamic .got.plt section. ie. The header is
2052 not necessarily at the start of the output .got section. */
2053 htab
->etab
.sgot
->size
+= 12;
2055 /* Define the symbol __GLOBAL_OFFSET_TABLE__ on the header. */
2057 if (!(_bfd_generic_link_add_one_symbol
2058 (info
, abfd
, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL
, htab
->etab
.sgot
,
2059 (bfd_vma
) 0, NULL
, FALSE
, bed
->collect
, &bh
)))
2061 eh
= (struct elf_link_hash_entry
*) bh
;
2062 eh
->def_regular
= 1;
2063 eh
->type
= STT_OBJECT
;
2064 eh
->other
= STV_HIDDEN
;
2066 if (! bfd_link_executable (info
)
2067 && ! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2070 htab
->etab
.hgot
= eh
;
2072 htab
->sdynbss
= bfd_get_linker_section (abfd
, ".dynbss");
2073 htab
->srelbss
= bfd_get_linker_section (abfd
, ".rela.bss");
2078 /* Look through the relocs for a section during the first phase, and
2079 calculate needed space in the global offset table, procedure linkage
2080 table, and dynamic reloc sections. At this point we haven't
2081 necessarily read all the input files. */
2084 elf_metag_check_relocs (bfd
*abfd
,
2085 struct bfd_link_info
*info
,
2087 const Elf_Internal_Rela
*relocs
)
2089 Elf_Internal_Shdr
*symtab_hdr
;
2090 struct elf_link_hash_entry
**eh_syms
;
2091 const Elf_Internal_Rela
*rel
;
2092 const Elf_Internal_Rela
*rel_end
;
2093 struct elf_metag_link_hash_table
*htab
;
2096 int tls_type
= GOT_UNKNOWN
, old_tls_type
= GOT_UNKNOWN
;
2098 if (bfd_link_relocatable (info
))
2101 htab
= metag_link_hash_table (info
);
2102 dynobj
= htab
->etab
.dynobj
;
2103 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2104 eh_syms
= elf_sym_hashes (abfd
);
2110 rel_end
= relocs
+ sec
->reloc_count
;
2111 for (rel
= relocs
; rel
< rel_end
; rel
++)
2114 struct elf_metag_link_hash_entry
*hh
;
2115 Elf_Internal_Sym
*isym
;
2116 unsigned long r_symndx
;
2118 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2119 r_type
= ELF32_R_TYPE (rel
->r_info
);
2120 if (r_symndx
< symtab_hdr
->sh_info
)
2122 /* A local symbol. */
2123 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
2134 hh
= (struct elf_metag_link_hash_entry
*)
2135 eh_syms
[r_symndx
- symtab_hdr
->sh_info
];
2136 while (hh
->eh
.root
.type
== bfd_link_hash_indirect
2137 || hh
->eh
.root
.type
== bfd_link_hash_warning
)
2138 hh
= (struct elf_metag_link_hash_entry
*) hh
->eh
.root
.u
.i
.link
;
2140 /* PR15323, ref flags aren't set for references in the same
2142 hh
->eh
.root
.non_ir_ref
= 1;
2145 /* Some relocs require a global offset table. */
2146 if (htab
->etab
.sgot
== NULL
)
2150 case R_METAG_TLS_GD
:
2151 case R_METAG_TLS_LDM
:
2152 case R_METAG_TLS_IE
:
2153 if (bfd_link_pic (info
))
2154 info
->flags
|= DF_STATIC_TLS
;
2157 case R_METAG_HI16_GOTOFF
:
2158 case R_METAG_LO16_GOTOFF
:
2159 case R_METAG_GETSET_GOTOFF
:
2160 case R_METAG_GETSET_GOT
:
2161 case R_METAG_HI16_GOTPC
:
2162 case R_METAG_LO16_GOTPC
:
2164 htab
->etab
.dynobj
= dynobj
= abfd
;
2165 if (!elf_metag_create_dynamic_sections (dynobj
, info
))
2176 case R_METAG_TLS_IE
:
2177 case R_METAG_TLS_GD
:
2178 case R_METAG_GETSET_GOT
:
2182 tls_type
= GOT_NORMAL
;
2184 case R_METAG_TLS_IE
:
2185 tls_type
= GOT_TLS_IE
;
2187 case R_METAG_TLS_GD
:
2188 tls_type
= GOT_TLS_GD
;
2194 hh
->eh
.got
.refcount
+= 1;
2195 old_tls_type
= hh
->tls_type
;
2199 bfd_signed_vma
*local_got_refcounts
;
2201 /* This is a global offset table entry for a local
2203 local_got_refcounts
= elf_local_got_refcounts (abfd
);
2204 if (local_got_refcounts
== NULL
)
2208 size
= symtab_hdr
->sh_info
;
2209 size
*= sizeof (bfd_signed_vma
);
2210 /* Add in space to store the local GOT TLS types. */
2211 size
+= symtab_hdr
->sh_info
;
2212 local_got_refcounts
= ((bfd_signed_vma
*)
2213 bfd_zalloc (abfd
, size
));
2214 if (local_got_refcounts
== NULL
)
2216 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
2217 memset (metag_elf_local_got_tls_type (abfd
),
2218 GOT_UNKNOWN
, symtab_hdr
->sh_info
);
2220 local_got_refcounts
[r_symndx
] += 1;
2221 old_tls_type
= metag_elf_local_got_tls_type (abfd
) [r_symndx
];
2224 if (old_tls_type
!= tls_type
)
2228 hh
->tls_type
= tls_type
;
2232 metag_elf_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
2238 case R_METAG_TLS_LDM
:
2239 metag_link_hash_table (info
)->tls_ldm_got
.refcount
+= 1;
2242 case R_METAG_RELBRANCH_PLT
:
2243 /* This symbol requires a procedure linkage table entry. We
2244 actually build the entry in adjust_dynamic_symbol,
2245 because this might be a case of linking PIC code without
2246 linking in any dynamic objects, in which case we don't
2247 need to generate a procedure linkage table after all. */
2249 /* If this is a local symbol, we resolve it directly without
2250 creating a procedure linkage table entry. */
2254 if (hh
->eh
.forced_local
)
2257 hh
->eh
.needs_plt
= 1;
2258 hh
->eh
.plt
.refcount
+= 1;
2261 case R_METAG_HIADDR16
:
2262 case R_METAG_LOADDR16
:
2263 /* Let's help debug shared library creation. These relocs
2264 cannot be used in shared libs. Don't error out for
2265 sections we don't care about, such as debug sections or
2266 non-constant sections. */
2267 if (bfd_link_pic (info
)
2268 && (sec
->flags
& SEC_ALLOC
) != 0
2269 && (sec
->flags
& SEC_READONLY
) != 0)
2274 name
= hh
->eh
.root
.root
.string
;
2276 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
, NULL
);
2278 /* xgettext:c-format */
2279 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2280 abfd
, elf_metag_howto_table
[r_type
].name
, name
);
2281 bfd_set_error (bfd_error_bad_value
);
2286 case R_METAG_ADDR32
:
2287 case R_METAG_RELBRANCH
:
2288 case R_METAG_GETSETOFF
:
2289 if (hh
!= NULL
&& !bfd_link_pic (info
))
2291 hh
->eh
.non_got_ref
= 1;
2292 hh
->eh
.plt
.refcount
+= 1;
2295 /* If we are creating a shared library, and this is a reloc
2296 against a global symbol, or a non PC relative reloc
2297 against a local symbol, then we need to copy the reloc
2298 into the shared library. However, if we are linking with
2299 -Bsymbolic, we do not need to copy a reloc against a
2300 global symbol which is defined in an object we are
2301 including in the link (i.e., DEF_REGULAR is set). At
2302 this point we have not seen all the input files, so it is
2303 possible that DEF_REGULAR is not set now but will be set
2304 later (it is never cleared). We account for that
2305 possibility below by storing information in the
2306 dyn_relocs field of the hash table entry. A similar
2307 situation occurs when creating shared libraries and symbol
2308 visibility changes render the symbol local.
2310 If on the other hand, we are creating an executable, we
2311 may need to keep relocations for symbols satisfied by a
2312 dynamic library if we manage to avoid copy relocs for the
2314 if ((bfd_link_pic (info
)
2315 && (sec
->flags
& SEC_ALLOC
) != 0
2316 && (r_type
!= R_METAG_RELBRANCH
2318 && (! info
->symbolic
2319 || hh
->eh
.root
.type
== bfd_link_hash_defweak
2320 || !hh
->eh
.def_regular
))))
2321 || (!bfd_link_pic (info
)
2322 && (sec
->flags
& SEC_ALLOC
) != 0
2324 && (hh
->eh
.root
.type
== bfd_link_hash_defweak
2325 || !hh
->eh
.def_regular
)))
2327 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2328 struct elf_metag_dyn_reloc_entry
**hdh_head
;
2331 htab
->etab
.dynobj
= dynobj
= abfd
;
2333 /* When creating a shared object, we must copy these
2334 relocs into the output file. We create a reloc
2335 section in dynobj and make room for the reloc. */
2338 sreloc
= _bfd_elf_make_dynamic_reloc_section
2339 (sec
, htab
->etab
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
2343 bfd_set_error (bfd_error_bad_value
);
2347 elf_section_data (sec
)->sreloc
= sreloc
;
2350 /* If this is a global symbol, we count the number of
2351 relocations we need for this symbol. */
2353 hdh_head
= &((struct elf_metag_link_hash_entry
*) hh
)->dyn_relocs
;
2356 /* Track dynamic relocs needed for local syms too. */
2360 sr
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2364 vpp
= &elf_section_data (sr
)->local_dynrel
;
2365 hdh_head
= (struct elf_metag_dyn_reloc_entry
**) vpp
;
2369 if (hdh_p
== NULL
|| hdh_p
->sec
!= sec
)
2371 hdh_p
= ((struct elf_metag_dyn_reloc_entry
*)
2372 bfd_alloc (dynobj
, sizeof *hdh_p
));
2375 hdh_p
->hdh_next
= *hdh_head
;
2379 hdh_p
->relative_count
= 0;
2383 if (ELF32_R_TYPE (rel
->r_info
) == R_METAG_RELBRANCH
)
2384 hdh_p
->relative_count
+= 1;
2388 /* This relocation describes the C++ object vtable hierarchy.
2389 Reconstruct it for later use during GC. */
2390 case R_METAG_GNU_VTINHERIT
:
2391 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, &hh
->eh
,
2396 /* This relocation describes which C++ vtable entries are actually
2397 used. Record for later use during GC. */
2398 case R_METAG_GNU_VTENTRY
:
2399 BFD_ASSERT (hh
!= NULL
);
2401 && !bfd_elf_gc_record_vtentry (abfd
, sec
, &hh
->eh
, rel
->r_addend
))
2410 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2413 elf_metag_copy_indirect_symbol (struct bfd_link_info
*info
,
2414 struct elf_link_hash_entry
*eh_dir
,
2415 struct elf_link_hash_entry
*eh_ind
)
2417 struct elf_metag_link_hash_entry
*hh_dir
, *hh_ind
;
2419 hh_dir
= metag_elf_hash_entry (eh_dir
);
2420 hh_ind
= metag_elf_hash_entry (eh_ind
);
2422 if (hh_ind
->dyn_relocs
!= NULL
)
2424 if (hh_dir
->dyn_relocs
!= NULL
)
2426 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
2427 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2429 if (eh_ind
->root
.type
== bfd_link_hash_indirect
)
2432 /* Add reloc counts against the weak sym to the strong sym
2433 list. Merge any entries against the same section. */
2434 for (hdh_pp
= &hh_ind
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
; )
2436 struct elf_metag_dyn_reloc_entry
*hdh_q
;
2438 for (hdh_q
= hh_dir
->dyn_relocs
; hdh_q
!= NULL
;
2439 hdh_q
= hdh_q
->hdh_next
)
2440 if (hdh_q
->sec
== hdh_p
->sec
)
2442 hdh_q
->relative_count
+= hdh_p
->relative_count
;
2443 hdh_q
->count
+= hdh_p
->count
;
2444 *hdh_pp
= hdh_p
->hdh_next
;
2448 hdh_pp
= &hdh_p
->hdh_next
;
2450 *hdh_pp
= hh_dir
->dyn_relocs
;
2453 hh_dir
->dyn_relocs
= hh_ind
->dyn_relocs
;
2454 hh_ind
->dyn_relocs
= NULL
;
2457 if (eh_ind
->root
.type
== bfd_link_hash_indirect
2458 && eh_dir
->got
.refcount
<= 0)
2460 hh_dir
->tls_type
= hh_ind
->tls_type
;
2461 hh_ind
->tls_type
= GOT_UNKNOWN
;
2464 _bfd_elf_link_hash_copy_indirect (info
, eh_dir
, eh_ind
);
2467 /* Adjust a symbol defined by a dynamic object and referenced by a
2468 regular object. The current definition is in some section of the
2469 dynamic object, but we're not including those sections. We have to
2470 change the definition to something the rest of the link can
2474 elf_metag_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2475 struct elf_link_hash_entry
*eh
)
2477 struct elf_metag_link_hash_table
*htab
;
2478 struct elf_metag_link_hash_entry
*hh
;
2479 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2482 /* If this is a function, put it in the procedure linkage table. We
2483 will fill in the contents of the procedure linkage table later,
2484 when we know the address of the .got section. */
2485 if (eh
->type
== STT_FUNC
2488 if (eh
->plt
.refcount
<= 0
2489 || SYMBOL_CALLS_LOCAL (info
, eh
)
2490 || (ELF_ST_VISIBILITY (eh
->other
) != STV_DEFAULT
2491 && eh
->root
.type
== bfd_link_hash_undefweak
))
2493 /* This case can occur if we saw a PLT reloc in an input
2494 file, but the symbol was never referred to by a dynamic
2495 object. In such a case, we don't actually need to build
2496 a procedure linkage table, and we can just do a PCREL
2498 eh
->plt
.offset
= (bfd_vma
) -1;
2505 eh
->plt
.offset
= (bfd_vma
) -1;
2507 /* If this is a weak symbol, and there is a real definition, the
2508 processor independent code will have arranged for us to see the
2509 real definition first, and we can just use the same value. */
2510 if (eh
->u
.weakdef
!= NULL
)
2512 if (eh
->u
.weakdef
->root
.type
!= bfd_link_hash_defined
2513 && eh
->u
.weakdef
->root
.type
!= bfd_link_hash_defweak
)
2515 eh
->root
.u
.def
.section
= eh
->u
.weakdef
->root
.u
.def
.section
;
2516 eh
->root
.u
.def
.value
= eh
->u
.weakdef
->root
.u
.def
.value
;
2517 eh
->non_got_ref
= eh
->u
.weakdef
->non_got_ref
;
2521 /* This is a reference to a symbol defined by a dynamic object which
2522 is not a function. */
2524 /* If we are creating a shared library, we must presume that the
2525 only references to the symbol are via the global offset table.
2526 For such cases we need not do anything here; the relocations will
2527 be handled correctly by relocate_section. */
2528 if (bfd_link_pic (info
))
2531 /* If there are no references to this symbol that do not use the
2532 GOT, we don't need to generate a copy reloc. */
2533 if (!eh
->non_got_ref
)
2536 /* If -z nocopyreloc was given, we won't generate them either. */
2537 if (info
->nocopyreloc
)
2539 eh
->non_got_ref
= 0;
2543 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2544 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2546 s
= hdh_p
->sec
->output_section
;
2547 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2551 /* If we didn't find any dynamic relocs in read-only sections, then
2552 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2555 eh
->non_got_ref
= 0;
2559 /* We must allocate the symbol in our .dynbss section, which will
2560 become part of the .bss section of the executable. There will be
2561 an entry for this symbol in the .dynsym section. The dynamic
2562 object will contain position independent code, so all references
2563 from the dynamic object to this symbol will go through the global
2564 offset table. The dynamic linker will use the .dynsym entry to
2565 determine the address it must put in the global offset table, so
2566 both the dynamic object and the regular object will refer to the
2567 same memory location for the variable. */
2569 htab
= metag_link_hash_table (info
);
2571 /* We must generate a COPY reloc to tell the dynamic linker to
2572 copy the initial value out of the dynamic object and into the
2573 runtime process image. */
2574 if ((eh
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && eh
->size
!= 0)
2576 htab
->srelbss
->size
+= sizeof (Elf32_External_Rela
);
2582 return _bfd_elf_adjust_dynamic_copy (info
, eh
, s
);
2585 /* Allocate space in .plt, .got and associated reloc sections for
2589 allocate_dynrelocs (struct elf_link_hash_entry
*eh
, void *inf
)
2591 struct bfd_link_info
*info
;
2592 struct elf_metag_link_hash_table
*htab
;
2593 struct elf_metag_link_hash_entry
*hh
;
2594 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2596 if (eh
->root
.type
== bfd_link_hash_indirect
)
2599 if (eh
->root
.type
== bfd_link_hash_warning
)
2600 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
2603 htab
= metag_link_hash_table (info
);
2605 if (htab
->etab
.dynamic_sections_created
2606 && eh
->plt
.refcount
> 0)
2608 /* Make sure this symbol is output as a dynamic symbol.
2609 Undefined weak syms won't yet be marked as dynamic. */
2610 if (eh
->dynindx
== -1
2611 && !eh
->forced_local
)
2613 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2617 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), eh
))
2619 asection
*s
= htab
->etab
.splt
;
2621 /* If this is the first .plt entry, make room for the special
2624 s
->size
+= PLT_ENTRY_SIZE
;
2626 eh
->plt
.offset
= s
->size
;
2628 /* If this symbol is not defined in a regular file, and we are
2629 not generating a shared library, then set the symbol to this
2630 location in the .plt. This is required to make function
2631 pointers compare as equal between the normal executable and
2632 the shared library. */
2633 if (! bfd_link_pic (info
)
2634 && !eh
->def_regular
)
2636 eh
->root
.u
.def
.section
= s
;
2637 eh
->root
.u
.def
.value
= eh
->plt
.offset
;
2640 /* Make room for this entry. */
2641 s
->size
+= PLT_ENTRY_SIZE
;
2643 /* We also need to make an entry in the .got.plt section, which
2644 will be placed in the .got section by the linker script. */
2645 htab
->etab
.sgotplt
->size
+= 4;
2647 /* We also need to make an entry in the .rel.plt section. */
2648 htab
->etab
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
2652 eh
->plt
.offset
= (bfd_vma
) -1;
2658 eh
->plt
.offset
= (bfd_vma
) -1;
2662 if (eh
->got
.refcount
> 0)
2666 int tls_type
= metag_elf_hash_entry (eh
)->tls_type
;
2668 /* Make sure this symbol is output as a dynamic symbol.
2669 Undefined weak syms won't yet be marked as dynamic. */
2670 if (eh
->dynindx
== -1
2671 && !eh
->forced_local
)
2673 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2677 s
= htab
->etab
.sgot
;
2679 eh
->got
.offset
= s
->size
;
2681 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
2682 if (tls_type
== GOT_TLS_GD
)
2684 dyn
= htab
->etab
.dynamic_sections_created
;
2685 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2686 R_METAG_TLS_GD needs one if local symbol and two if global. */
2687 if ((tls_type
== GOT_TLS_GD
&& eh
->dynindx
== -1)
2688 || (tls_type
== GOT_TLS_IE
&& dyn
))
2689 htab
->etab
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2690 else if (tls_type
== GOT_TLS_GD
)
2691 htab
->etab
.srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2692 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
2693 bfd_link_pic (info
),
2695 htab
->etab
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2698 eh
->got
.offset
= (bfd_vma
) -1;
2700 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2701 if (hh
->dyn_relocs
== NULL
)
2704 /* If this is a -Bsymbolic shared link, then we need to discard all
2705 space allocated for dynamic pc-relative relocs against symbols
2706 defined in a regular object. For the normal shared case, discard
2707 space for relocs that have become local due to symbol visibility
2709 if (bfd_link_pic (info
))
2711 if (SYMBOL_CALLS_LOCAL (info
, eh
))
2713 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
2715 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
; )
2717 hdh_p
->count
-= hdh_p
->relative_count
;
2718 hdh_p
->relative_count
= 0;
2719 if (hdh_p
->count
== 0)
2720 *hdh_pp
= hdh_p
->hdh_next
;
2722 hdh_pp
= &hdh_p
->hdh_next
;
2726 /* Also discard relocs on undefined weak syms with non-default
2728 if (hh
->dyn_relocs
!= NULL
2729 && eh
->root
.type
== bfd_link_hash_undefweak
)
2731 if (ELF_ST_VISIBILITY (eh
->other
) != STV_DEFAULT
)
2732 hh
->dyn_relocs
= NULL
;
2734 /* Make sure undefined weak symbols are output as a dynamic
2736 else if (eh
->dynindx
== -1
2737 && !eh
->forced_local
)
2739 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2746 /* For the non-shared case, discard space for relocs against
2747 symbols which turn out to need copy relocs or are not
2749 if (!eh
->non_got_ref
2750 && ((eh
->def_dynamic
2751 && !eh
->def_regular
)
2752 || (htab
->etab
.dynamic_sections_created
2753 && (eh
->root
.type
== bfd_link_hash_undefweak
2754 || eh
->root
.type
== bfd_link_hash_undefined
))))
2756 /* Make sure this symbol is output as a dynamic symbol.
2757 Undefined weak syms won't yet be marked as dynamic. */
2758 if (eh
->dynindx
== -1
2759 && !eh
->forced_local
)
2761 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2765 /* If that succeeded, we know we'll be keeping all the
2767 if (eh
->dynindx
!= -1)
2771 hh
->dyn_relocs
= NULL
;
2777 /* Finally, allocate space. */
2778 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2780 asection
*sreloc
= elf_section_data (hdh_p
->sec
)->sreloc
;
2781 sreloc
->size
+= hdh_p
->count
* sizeof (Elf32_External_Rela
);
2787 /* Find any dynamic relocs that apply to read-only sections. */
2790 readonly_dynrelocs (struct elf_link_hash_entry
*eh
, void *inf
)
2792 struct elf_metag_link_hash_entry
*hh
;
2793 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2795 if (eh
->root
.type
== bfd_link_hash_warning
)
2796 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
2798 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2799 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2801 asection
*s
= hdh_p
->sec
->output_section
;
2803 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2805 struct bfd_link_info
*info
= inf
;
2807 info
->flags
|= DF_TEXTREL
;
2809 /* Not an error, just cut short the traversal. */
2816 /* Set the sizes of the dynamic sections. */
2819 elf_metag_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2820 struct bfd_link_info
*info
)
2822 struct elf_metag_link_hash_table
*htab
;
2828 htab
= metag_link_hash_table (info
);
2829 dynobj
= htab
->etab
.dynobj
;
2833 if (htab
->etab
.dynamic_sections_created
)
2835 /* Set the contents of the .interp section to the interpreter. */
2836 if (bfd_link_executable (info
) && !info
->nointerp
)
2838 s
= bfd_get_linker_section (dynobj
, ".interp");
2841 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2842 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2846 /* Set up .got offsets for local syms, and space for local dynamic
2848 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
2850 bfd_signed_vma
*local_got
;
2851 bfd_signed_vma
*end_local_got
;
2852 bfd_size_type locsymcount
;
2853 Elf_Internal_Shdr
*symtab_hdr
;
2855 char *local_tls_type
;
2857 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
2860 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2862 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2864 for (hdh_p
= ((struct elf_metag_dyn_reloc_entry
*)
2865 elf_section_data (s
)->local_dynrel
);
2867 hdh_p
= hdh_p
->hdh_next
)
2869 if (!bfd_is_abs_section (hdh_p
->sec
)
2870 && bfd_is_abs_section (hdh_p
->sec
->output_section
))
2872 /* Input section has been discarded, either because
2873 it is a copy of a linkonce section or due to
2874 linker script /DISCARD/, so we'll be discarding
2877 else if (hdh_p
->count
!= 0)
2879 srel
= elf_section_data (hdh_p
->sec
)->sreloc
;
2880 srel
->size
+= hdh_p
->count
* sizeof (Elf32_External_Rela
);
2881 if ((hdh_p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2882 info
->flags
|= DF_TEXTREL
;
2887 local_got
= elf_local_got_refcounts (ibfd
);
2891 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2892 locsymcount
= symtab_hdr
->sh_info
;
2893 end_local_got
= local_got
+ locsymcount
;
2894 local_tls_type
= metag_elf_local_got_tls_type (ibfd
);
2895 s
= htab
->etab
.sgot
;
2896 srel
= htab
->etab
.srelgot
;
2897 for (; local_got
< end_local_got
; ++local_got
)
2901 *local_got
= s
->size
;
2902 s
->size
+= GOT_ENTRY_SIZE
;
2903 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
2904 if (*local_tls_type
== GOT_TLS_GD
)
2906 if (bfd_link_pic (info
))
2907 srel
->size
+= sizeof (Elf32_External_Rela
);
2910 *local_got
= (bfd_vma
) -1;
2915 if (htab
->tls_ldm_got
.refcount
> 0)
2917 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2919 htab
->tls_ldm_got
.offset
= htab
->etab
.sgot
->size
;
2920 htab
->etab
.sgot
->size
+= 8;
2921 htab
->etab
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2924 htab
->tls_ldm_got
.offset
= -1;
2926 /* Allocate global sym .plt and .got entries, and space for global
2927 sym dynamic relocs. */
2928 elf_link_hash_traverse (&htab
->etab
, allocate_dynrelocs
, info
);
2930 /* We now have determined the sizes of the various dynamic sections.
2931 Allocate memory for them. */
2933 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2935 bfd_boolean reloc_section
= FALSE
;
2937 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2940 if (s
== htab
->etab
.splt
2941 || s
== htab
->etab
.sgot
2942 || s
== htab
->etab
.sgotplt
2943 || s
== htab
->sdynbss
)
2945 /* Strip this section if we don't need it; see the
2948 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
2950 if (s
->size
!= 0 && s
!= htab
->etab
.srelplt
)
2953 /* We use the reloc_count field as a counter if we need
2954 to copy relocs into the output file. */
2956 reloc_section
= TRUE
;
2960 /* It's not one of our sections, so don't allocate space. */
2966 /* If we don't need this section, strip it from the
2967 output file. This is mostly to handle .rela.bss and
2968 .rela.plt. We must create both sections in
2969 create_dynamic_sections, because they must be created
2970 before the linker maps input sections to output
2971 sections. The linker does that before
2972 adjust_dynamic_symbol is called, and it is that
2973 function which decides whether anything needs to go
2974 into these sections. */
2975 s
->flags
|= SEC_EXCLUDE
;
2979 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
2982 /* Allocate memory for the section contents. */
2983 s
->contents
= bfd_zalloc (dynobj
, s
->size
);
2984 if (s
->contents
== NULL
)
2986 else if (reloc_section
)
2988 unsigned char *contents
= s
->contents
;
2989 Elf32_External_Rela reloc
;
2991 /* Fill the reloc section with a R_METAG_NONE type reloc. */
2992 memset(&reloc
, 0, sizeof(Elf32_External_Rela
));
2993 reloc
.r_info
[0] = R_METAG_NONE
;
2994 for (; contents
< (s
->contents
+ s
->size
);
2995 contents
+= sizeof(Elf32_External_Rela
))
2997 memcpy(contents
, &reloc
, sizeof(Elf32_External_Rela
));
3002 if (htab
->etab
.dynamic_sections_created
)
3004 /* Add some entries to the .dynamic section. We fill in the
3005 values later, in elf_metag_finish_dynamic_sections, but we
3006 must add the entries now so that we get the correct size for
3007 the .dynamic section. The DT_DEBUG entry is filled in by the
3008 dynamic linker and used by the debugger. */
3009 #define add_dynamic_entry(TAG, VAL) \
3010 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3012 if (!add_dynamic_entry (DT_PLTGOT
, 0))
3015 if (bfd_link_executable (info
))
3017 if (!add_dynamic_entry (DT_DEBUG
, 0))
3021 if (htab
->etab
.srelplt
->size
!= 0)
3023 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
3024 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3025 || !add_dynamic_entry (DT_JMPREL
, 0))
3031 if (!add_dynamic_entry (DT_RELA
, 0)
3032 || !add_dynamic_entry (DT_RELASZ
, 0)
3033 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
)))
3036 /* If any dynamic relocs apply to a read-only section,
3037 then we need a DT_TEXTREL entry. */
3038 if ((info
->flags
& DF_TEXTREL
) == 0)
3039 elf_link_hash_traverse (&htab
->etab
, readonly_dynrelocs
, info
);
3041 if ((info
->flags
& DF_TEXTREL
) != 0)
3043 if (!add_dynamic_entry (DT_TEXTREL
, 0))
3048 #undef add_dynamic_entry
3053 /* Finish up dynamic symbol handling. We set the contents of various
3054 dynamic sections here. */
3057 elf_metag_finish_dynamic_symbol (bfd
*output_bfd
,
3058 struct bfd_link_info
*info
,
3059 struct elf_link_hash_entry
*eh
,
3060 Elf_Internal_Sym
*sym
)
3062 struct elf_metag_link_hash_table
*htab
;
3063 Elf_Internal_Rela rel
;
3066 htab
= metag_link_hash_table (info
);
3068 if (eh
->plt
.offset
!= (bfd_vma
) -1)
3078 if (eh
->plt
.offset
& 1)
3081 BFD_ASSERT (eh
->dynindx
!= -1);
3083 splt
= htab
->etab
.splt
;
3084 sgot
= htab
->etab
.sgotplt
;
3085 srela
= htab
->etab
.srelplt
;
3086 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srela
!= NULL
);
3088 /* Get the index in the procedure linkage table which
3089 corresponds to this symbol. This is the index of this symbol
3090 in all the symbols for which we are making plt entries. The
3091 first entry in the procedure linkage table is reserved. */
3092 plt_index
= eh
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3094 /* Get the offset into the .got.plt table of the entry that
3095 corresponds to this function. */
3096 got_offset
= plt_index
* GOT_ENTRY_SIZE
;
3098 BFD_ASSERT (got_offset
< (1 << 16));
3100 got_entry
= sgot
->output_section
->vma
3101 + sgot
->output_offset
3104 BFD_ASSERT (plt_index
< (1 << 16));
3106 /* Fill in the entry in the procedure linkage table. */
3107 if (! bfd_link_pic (info
))
3109 bfd_put_32 (output_bfd
,
3111 | (((got_entry
>> 16) & 0xffff) << 3)),
3112 splt
->contents
+ eh
->plt
.offset
);
3113 bfd_put_32 (output_bfd
,
3115 | ((got_entry
& 0xffff) << 3)),
3116 splt
->contents
+ eh
->plt
.offset
+ 4);
3117 bfd_put_32 (output_bfd
, plt_entry
[2],
3118 splt
->contents
+ eh
->plt
.offset
+ 8);
3119 bfd_put_32 (output_bfd
,
3120 (plt_entry
[3] | (plt_index
<< 3)),
3121 splt
->contents
+ eh
->plt
.offset
+ 12);
3122 bfd_put_32 (output_bfd
,
3124 | ((((unsigned int) ((- (eh
->plt
.offset
+ 16)) >> 2)) & 0x7ffff) << 5)),
3125 splt
->contents
+ eh
->plt
.offset
+ 16);
3129 bfd_vma addr
= got_entry
- (splt
->output_section
->vma
+
3130 splt
->output_offset
+ eh
->plt
.offset
);
3132 bfd_put_32 (output_bfd
,
3133 plt_pic_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3134 splt
->contents
+ eh
->plt
.offset
);
3135 bfd_put_32 (output_bfd
,
3136 plt_pic_entry
[1] | ((addr
& 0xffff) << 3),
3137 splt
->contents
+ eh
->plt
.offset
+ 4);
3138 bfd_put_32 (output_bfd
, plt_pic_entry
[2],
3139 splt
->contents
+ eh
->plt
.offset
+ 8);
3140 bfd_put_32 (output_bfd
,
3141 (plt_pic_entry
[3] | (plt_index
<< 3)),
3142 splt
->contents
+ eh
->plt
.offset
+ 12);
3143 bfd_put_32 (output_bfd
,
3145 + ((((unsigned int) ((- (eh
->plt
.offset
+ 16)) >> 2)) & 0x7ffff) << 5)),
3146 splt
->contents
+ eh
->plt
.offset
+ 16);
3149 /* Fill in the entry in the global offset table. */
3150 bfd_put_32 (output_bfd
,
3151 (splt
->output_section
->vma
3152 + splt
->output_offset
3154 + 12), /* offset within PLT entry */
3155 sgot
->contents
+ got_offset
);
3157 /* Fill in the entry in the .rela.plt section. */
3158 rel
.r_offset
= (sgot
->output_section
->vma
3159 + sgot
->output_offset
3161 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_JMP_SLOT
);
3163 loc
= htab
->etab
.srelplt
->contents
;
3164 loc
+= plt_index
* sizeof(Elf32_External_Rela
);
3165 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3167 if (!eh
->def_regular
)
3169 /* Mark the symbol as undefined, rather than as defined in
3170 the .plt section. Leave the value alone. */
3171 sym
->st_shndx
= SHN_UNDEF
;
3175 if (eh
->got
.offset
!= (bfd_vma
) -1
3176 && (metag_elf_hash_entry (eh
)->tls_type
& GOT_TLS_GD
) == 0
3177 && (metag_elf_hash_entry (eh
)->tls_type
& GOT_TLS_IE
) == 0)
3179 /* This symbol has an entry in the global offset table. Set it
3182 rel
.r_offset
= ((eh
->got
.offset
&~ (bfd_vma
) 1)
3183 + htab
->etab
.sgot
->output_offset
3184 + htab
->etab
.sgot
->output_section
->vma
);
3186 /* If this is a -Bsymbolic link and the symbol is defined
3187 locally or was forced to be local because of a version file,
3188 we just want to emit a RELATIVE reloc. The entry in the
3189 global offset table will already have been initialized in the
3190 relocate_section function. */
3191 if (bfd_link_pic (info
)
3192 && (info
->symbolic
|| eh
->dynindx
== -1)
3195 rel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
3196 rel
.r_addend
= (eh
->root
.u
.def
.value
3197 + eh
->root
.u
.def
.section
->output_offset
3198 + eh
->root
.u
.def
.section
->output_section
->vma
);
3202 if ((eh
->got
.offset
& 1) != 0)
3204 bfd_put_32 (output_bfd
, 0, htab
->etab
.sgot
->contents
+ eh
->got
.offset
);
3205 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_GLOB_DAT
);
3209 loc
= htab
->etab
.srelgot
->contents
;
3210 loc
+= htab
->etab
.srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3211 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3218 /* This symbol needs a copy reloc. Set it up. */
3220 if (! (eh
->dynindx
!= -1
3221 && (eh
->root
.type
== bfd_link_hash_defined
3222 || eh
->root
.type
== bfd_link_hash_defweak
)))
3227 rel
.r_offset
= (eh
->root
.u
.def
.value
3228 + eh
->root
.u
.def
.section
->output_offset
3229 + eh
->root
.u
.def
.section
->output_section
->vma
);
3231 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_COPY
);
3232 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3233 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3236 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3237 if (eh
->root
.root
.string
[0] == '_'
3238 && (strcmp (eh
->root
.root
.string
, "_DYNAMIC") == 0
3239 || eh
== htab
->etab
.hgot
))
3241 sym
->st_shndx
= SHN_ABS
;
3247 /* Set the Meta ELF ABI version. */
3250 elf_metag_post_process_headers (bfd
* abfd
, struct bfd_link_info
* link_info
)
3252 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
3254 _bfd_elf_post_process_headers (abfd
, link_info
);
3255 i_ehdrp
= elf_elfheader (abfd
);
3256 i_ehdrp
->e_ident
[EI_ABIVERSION
] = METAG_ELF_ABI_VERSION
;
3259 /* Used to decide how to sort relocs in an optimal manner for the
3260 dynamic linker, before writing them out. */
3262 static enum elf_reloc_type_class
3263 elf_metag_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3264 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3265 const Elf_Internal_Rela
*rela
)
3267 switch ((int) ELF32_R_TYPE (rela
->r_info
))
3269 case R_METAG_RELATIVE
:
3270 return reloc_class_relative
;
3271 case R_METAG_JMP_SLOT
:
3272 return reloc_class_plt
;
3274 return reloc_class_copy
;
3276 return reloc_class_normal
;
3280 /* Finish up the dynamic sections. */
3283 elf_metag_finish_dynamic_sections (bfd
*output_bfd
,
3284 struct bfd_link_info
*info
)
3287 struct elf_metag_link_hash_table
*htab
;
3290 htab
= metag_link_hash_table (info
);
3291 dynobj
= htab
->etab
.dynobj
;
3293 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3295 if (htab
->etab
.dynamic_sections_created
)
3298 Elf32_External_Dyn
*dyncon
, *dynconend
;
3303 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
3304 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3305 for (; dyncon
< dynconend
; dyncon
++)
3307 Elf_Internal_Dyn dyn
;
3310 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3318 s
= htab
->etab
.sgot
;
3319 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3320 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3324 s
= htab
->etab
.srelplt
;
3325 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3326 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3330 s
= htab
->etab
.srelplt
;
3331 dyn
.d_un
.d_val
= s
->size
;
3332 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3338 /* Fill in the first entry in the procedure linkage table. */
3339 splt
= htab
->etab
.splt
;
3340 if (splt
&& splt
->size
> 0)
3343 /* addr = .got + 4 */
3344 addr
= (htab
->etab
.sgot
->output_section
->vma
3345 + htab
->etab
.sgot
->output_offset
+ 4);
3346 if (bfd_link_pic (info
))
3348 addr
-= splt
->output_section
->vma
+ splt
->output_offset
;
3349 bfd_put_32 (output_bfd
,
3350 plt0_pic_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3352 bfd_put_32 (output_bfd
,
3353 plt0_pic_entry
[1] | ((addr
& 0xffff) << 3),
3354 splt
->contents
+ 4);
3355 bfd_put_32 (output_bfd
, plt0_pic_entry
[2], splt
->contents
+ 8);
3356 bfd_put_32 (output_bfd
, plt0_pic_entry
[3], splt
->contents
+ 12);
3357 bfd_put_32 (output_bfd
, plt0_pic_entry
[4], splt
->contents
+ 16);
3361 bfd_put_32 (output_bfd
,
3362 plt0_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3364 bfd_put_32 (output_bfd
,
3365 plt0_entry
[1] | ((addr
& 0xffff) << 3),
3366 splt
->contents
+ 4);
3367 bfd_put_32 (output_bfd
, plt0_entry
[2], splt
->contents
+ 8);
3368 bfd_put_32 (output_bfd
, plt0_entry
[3], splt
->contents
+ 12);
3369 bfd_put_32 (output_bfd
, plt0_entry
[4], splt
->contents
+ 16);
3372 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
=
3377 if (htab
->etab
.sgot
!= NULL
&& htab
->etab
.sgot
->size
!= 0)
3379 /* Fill in the first entry in the global offset table.
3380 We use it to point to our dynamic section, if we have one. */
3381 bfd_put_32 (output_bfd
,
3382 sdyn
? sdyn
->output_section
->vma
+ sdyn
->output_offset
: 0,
3383 htab
->etab
.sgot
->contents
);
3385 /* The second entry is reserved for use by the dynamic linker. */
3386 memset (htab
->etab
.sgot
->contents
+ GOT_ENTRY_SIZE
, 0, GOT_ENTRY_SIZE
);
3388 /* Set .got entry size. */
3389 elf_section_data (htab
->etab
.sgot
->output_section
)
3390 ->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3396 /* Return the section that should be marked against GC for a given
3400 elf_metag_gc_mark_hook (asection
*sec
,
3401 struct bfd_link_info
*info
,
3402 Elf_Internal_Rela
*rela
,
3403 struct elf_link_hash_entry
*hh
,
3404 Elf_Internal_Sym
*sym
)
3407 switch ((unsigned int) ELF32_R_TYPE (rela
->r_info
))
3409 case R_METAG_GNU_VTINHERIT
:
3410 case R_METAG_GNU_VTENTRY
:
3414 return _bfd_elf_gc_mark_hook (sec
, info
, rela
, hh
, sym
);
3417 /* Update the got and plt entry reference counts for the section being
3421 elf_metag_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
3422 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3423 asection
*sec ATTRIBUTE_UNUSED
,
3424 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
3426 Elf_Internal_Shdr
*symtab_hdr
;
3427 struct elf_link_hash_entry
**eh_syms
;
3428 bfd_signed_vma
*local_got_refcounts
;
3429 bfd_signed_vma
*local_plt_refcounts
;
3430 const Elf_Internal_Rela
*rel
, *relend
;
3432 if (bfd_link_relocatable (info
))
3435 elf_section_data (sec
)->local_dynrel
= NULL
;
3437 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3438 eh_syms
= elf_sym_hashes (abfd
);
3439 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3440 local_plt_refcounts
= local_got_refcounts
;
3441 if (local_plt_refcounts
!= NULL
)
3442 local_plt_refcounts
+= symtab_hdr
->sh_info
;
3444 relend
= relocs
+ sec
->reloc_count
;
3445 for (rel
= relocs
; rel
< relend
; rel
++)
3447 unsigned long r_symndx
;
3448 unsigned int r_type
;
3449 struct elf_link_hash_entry
*eh
= NULL
;
3451 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3452 if (r_symndx
>= symtab_hdr
->sh_info
)
3454 struct elf_metag_link_hash_entry
*hh
;
3455 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
3456 struct elf_metag_dyn_reloc_entry
*hdh_p
;
3458 eh
= eh_syms
[r_symndx
- symtab_hdr
->sh_info
];
3459 while (eh
->root
.type
== bfd_link_hash_indirect
3460 || eh
->root
.type
== bfd_link_hash_warning
)
3461 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
3462 hh
= (struct elf_metag_link_hash_entry
*) eh
;
3464 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
;
3465 hdh_pp
= &hdh_p
->hdh_next
)
3466 if (hdh_p
->sec
== sec
)
3468 /* Everything must go for SEC. */
3469 *hdh_pp
= hdh_p
->hdh_next
;
3474 r_type
= ELF32_R_TYPE (rel
->r_info
);
3477 case R_METAG_TLS_LDM
:
3478 if (metag_link_hash_table (info
)->tls_ldm_got
.refcount
> 0)
3479 metag_link_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
3481 case R_METAG_TLS_IE
:
3482 case R_METAG_TLS_GD
:
3483 case R_METAG_GETSET_GOT
:
3486 if (eh
->got
.refcount
> 0)
3487 eh
->got
.refcount
-= 1;
3489 else if (local_got_refcounts
!= NULL
)
3491 if (local_got_refcounts
[r_symndx
] > 0)
3492 local_got_refcounts
[r_symndx
] -= 1;
3496 case R_METAG_RELBRANCH_PLT
:
3499 if (eh
->plt
.refcount
> 0)
3500 eh
->plt
.refcount
-= 1;
3504 case R_METAG_ADDR32
:
3505 case R_METAG_HIADDR16
:
3506 case R_METAG_LOADDR16
:
3507 case R_METAG_GETSETOFF
:
3508 case R_METAG_RELBRANCH
:
3511 struct elf_metag_link_hash_entry
*hh
;
3512 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
3513 struct elf_metag_dyn_reloc_entry
*hdh_p
;
3515 if (!bfd_link_pic (info
) && eh
->plt
.refcount
> 0)
3516 eh
->plt
.refcount
-= 1;
3518 hh
= (struct elf_metag_link_hash_entry
*) eh
;
3520 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
;
3521 hdh_pp
= &hdh_p
->hdh_next
)
3522 if (hdh_p
->sec
== sec
)
3524 if (ELF32_R_TYPE (rel
->r_info
) == R_METAG_RELBRANCH
)
3525 hdh_p
->relative_count
-= 1;
3527 if (hdh_p
->count
== 0)
3528 *hdh_pp
= hdh_p
->hdh_next
;
3542 /* Determine the type of stub needed, if any, for a call. */
3544 static enum elf_metag_stub_type
3545 metag_type_of_stub (asection
*input_sec
,
3546 const Elf_Internal_Rela
*rel
,
3547 struct elf_metag_link_hash_entry
*hh
,
3548 bfd_vma destination
,
3549 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3552 bfd_vma branch_offset
;
3553 bfd_vma max_branch_offset
;
3556 !(hh
->eh
.root
.type
== bfd_link_hash_defined
3557 || hh
->eh
.root
.type
== bfd_link_hash_defweak
))
3558 return metag_stub_none
;
3560 /* Determine where the call point is. */
3561 location
= (input_sec
->output_offset
3562 + input_sec
->output_section
->vma
3565 branch_offset
= destination
- location
;
3567 /* Determine if a long branch stub is needed. Meta branch offsets
3568 are signed 19 bits 4 byte aligned. */
3569 max_branch_offset
= (1 << (BRANCH_BITS
-1)) << 2;
3571 if (branch_offset
+ max_branch_offset
>= 2*max_branch_offset
)
3573 if (bfd_link_pic (info
))
3574 return metag_stub_long_branch_shared
;
3576 return metag_stub_long_branch
;
3579 return metag_stub_none
;
3582 #define MOVT_A0_3 0x82180005
3583 #define JUMP_A0_3 0xac180003
3585 #define MOVT_A1LBP 0x83080005
3586 #define ADD_A1LBP 0x83080000
3588 #define ADDT_A0_3_CPC 0x82980001
3589 #define ADD_A0_3_A0_3 0x82180000
3590 #define MOV_PC_A0_3 0xa3180ca0
3593 metag_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
3595 struct elf_metag_stub_hash_entry
*hsh
;
3602 /* Massage our args to the form they really have. */
3603 hsh
= (struct elf_metag_stub_hash_entry
*) gen_entry
;
3605 stub_sec
= hsh
->stub_sec
;
3607 /* Make a note of the offset within the stubs for this entry. */
3608 hsh
->stub_offset
= stub_sec
->size
;
3609 loc
= stub_sec
->contents
+ hsh
->stub_offset
;
3611 stub_bfd
= stub_sec
->owner
;
3613 switch (hsh
->stub_type
)
3615 case metag_stub_long_branch_shared
:
3616 /* A PIC long branch stub is an ADDT and an ADD instruction used to
3617 calculate the jump target using A0.3 as a temporary. Then a MOV
3618 to PC carries out the jump. */
3619 sym_value
= (hsh
->target_value
3620 + hsh
->target_section
->output_offset
3621 + hsh
->target_section
->output_section
->vma
3624 sym_value
-= (hsh
->stub_offset
3625 + stub_sec
->output_offset
3626 + stub_sec
->output_section
->vma
);
3628 bfd_put_32 (stub_bfd
, ADDT_A0_3_CPC
| (((sym_value
>> 16) & 0xffff) << 3),
3631 bfd_put_32 (stub_bfd
, ADD_A0_3_A0_3
| ((sym_value
& 0xffff) << 3),
3634 bfd_put_32 (stub_bfd
, MOV_PC_A0_3
, loc
+ 8);
3638 case metag_stub_long_branch
:
3639 /* A standard long branch stub is a MOVT instruction followed by a
3640 JUMP instruction using the A0.3 register as a temporary. This is
3641 the same method used by the LDLK linker (patch.c). */
3642 sym_value
= (hsh
->target_value
3643 + hsh
->target_section
->output_offset
3644 + hsh
->target_section
->output_section
->vma
3647 bfd_put_32 (stub_bfd
, MOVT_A0_3
| (((sym_value
>> 16) & 0xffff) << 3),
3650 bfd_put_32 (stub_bfd
, JUMP_A0_3
| ((sym_value
& 0xffff) << 3), loc
+ 4);
3659 stub_sec
->size
+= size
;
3663 /* As above, but don't actually build the stub. Just bump offset so
3664 we know stub section sizes. */
3667 metag_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
3669 struct elf_metag_stub_hash_entry
*hsh
;
3672 /* Massage our args to the form they really have. */
3673 hsh
= (struct elf_metag_stub_hash_entry
*) gen_entry
;
3675 if (hsh
->stub_type
== metag_stub_long_branch
)
3677 else if (hsh
->stub_type
== metag_stub_long_branch_shared
)
3680 hsh
->stub_sec
->size
+= size
;
3684 /* Set up various things so that we can make a list of input sections
3685 for each output section included in the link. Returns -1 on error,
3686 0 when no stubs will be needed, and 1 on success. */
3689 elf_metag_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
3692 unsigned int bfd_count
;
3693 unsigned int top_id
, top_index
;
3695 asection
**input_list
, **list
;
3697 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3699 /* Count the number of input BFDs and find the top input section id. */
3700 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
3702 input_bfd
= input_bfd
->link
.next
)
3705 for (section
= input_bfd
->sections
;
3707 section
= section
->next
)
3709 if (top_id
< section
->id
)
3710 top_id
= section
->id
;
3714 htab
->bfd_count
= bfd_count
;
3716 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
3717 htab
->stub_group
= bfd_zmalloc (amt
);
3718 if (htab
->stub_group
== NULL
)
3721 /* We can't use output_bfd->section_count here to find the top output
3722 section index as some sections may have been removed, and
3723 strip_excluded_output_sections doesn't renumber the indices. */
3724 for (section
= output_bfd
->sections
, top_index
= 0;
3726 section
= section
->next
)
3728 if (top_index
< section
->index
)
3729 top_index
= section
->index
;
3732 htab
->top_index
= top_index
;
3733 amt
= sizeof (asection
*) * (top_index
+ 1);
3734 input_list
= bfd_malloc (amt
);
3735 htab
->input_list
= input_list
;
3736 if (input_list
== NULL
)
3739 /* For sections we aren't interested in, mark their entries with a
3740 value we can check later. */
3741 list
= input_list
+ top_index
;
3743 *list
= bfd_abs_section_ptr
;
3744 while (list
-- != input_list
);
3746 for (section
= output_bfd
->sections
;
3748 section
= section
->next
)
3750 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3751 * have PC relative relocs in them but no code flag set. */
3752 if (((section
->flags
& SEC_CODE
) != 0) ||
3753 strcmp(".ctors", section
->name
) ||
3754 strcmp(".dtors", section
->name
))
3755 input_list
[section
->index
] = NULL
;
3761 /* The linker repeatedly calls this function for each input section,
3762 in the order that input sections are linked into output sections.
3763 Build lists of input sections to determine groupings between which
3764 we may insert linker stubs. */
3767 elf_metag_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
3769 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3771 if (isec
->output_section
->index
<= htab
->top_index
)
3773 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
3774 if (*list
!= bfd_abs_section_ptr
)
3776 /* Steal the link_sec pointer for our list. */
3777 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3778 /* This happens to make the list in reverse order,
3779 which is what we want. */
3780 PREV_SEC (isec
) = *list
;
3786 /* See whether we can group stub sections together. Grouping stub
3787 sections may result in fewer stubs. More importantly, we need to
3788 put all .init* and .fini* stubs at the beginning of the .init or
3789 .fini output sections respectively, because glibc splits the
3790 _init and _fini functions into multiple parts. Putting a stub in
3791 the middle of a function is not a good idea. */
3794 group_sections (struct elf_metag_link_hash_table
*htab
,
3795 bfd_size_type stub_group_size
,
3796 bfd_boolean stubs_always_before_branch
)
3798 asection
**list
= htab
->input_list
+ htab
->top_index
;
3801 asection
*tail
= *list
;
3802 if (tail
== bfd_abs_section_ptr
)
3804 while (tail
!= NULL
)
3808 bfd_size_type total
;
3809 bfd_boolean big_sec
;
3813 big_sec
= total
>= stub_group_size
;
3815 while ((prev
= PREV_SEC (curr
)) != NULL
3816 && ((total
+= curr
->output_offset
- prev
->output_offset
)
3820 /* OK, the size from the start of CURR to the end is less
3821 than stub_group_size bytes and thus can be handled by one stub
3822 section. (or the tail section is itself larger than
3823 stub_group_size bytes, in which case we may be toast.)
3824 We should really be keeping track of the total size of
3825 stubs added here, as stubs contribute to the final output
3829 prev
= PREV_SEC (tail
);
3830 /* Set up this stub group. */
3831 htab
->stub_group
[tail
->id
].link_sec
= curr
;
3833 while (tail
!= curr
&& (tail
= prev
) != NULL
);
3835 /* But wait, there's more! Input sections up to stub_group_size
3836 bytes before the stub section can be handled by it too.
3837 Don't do this if we have a really large section after the
3838 stubs, as adding more stubs increases the chance that
3839 branches may not reach into the stub section. */
3840 if (!stubs_always_before_branch
&& !big_sec
)
3844 && ((total
+= tail
->output_offset
- prev
->output_offset
)
3848 prev
= PREV_SEC (tail
);
3849 htab
->stub_group
[tail
->id
].link_sec
= curr
;
3855 while (list
-- != htab
->input_list
);
3856 free (htab
->input_list
);
3860 /* Read in all local syms for all input bfds.
3861 Returns -1 on error, 0 otherwise. */
3864 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
3865 struct bfd_link_info
*info
)
3867 unsigned int bfd_indx
;
3868 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
3869 int stub_changed
= 0;
3870 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3872 /* We want to read in symbol extension records only once. To do this
3873 we need to read in the local symbols in parallel and save them for
3874 later use; so hold pointers to the local symbols in an array. */
3875 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
3876 all_local_syms
= bfd_zmalloc (amt
);
3877 htab
->all_local_syms
= all_local_syms
;
3878 if (all_local_syms
== NULL
)
3881 /* Walk over all the input BFDs, swapping in local symbols. */
3884 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3886 Elf_Internal_Shdr
*symtab_hdr
;
3888 /* We'll need the symbol table in a second. */
3889 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3890 if (symtab_hdr
->sh_info
== 0)
3893 /* We need an array of the local symbols attached to the input bfd. */
3894 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3895 if (local_syms
== NULL
)
3897 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3898 symtab_hdr
->sh_info
, 0,
3900 /* Cache them for elf_link_input_bfd. */
3901 symtab_hdr
->contents
= (unsigned char *) local_syms
;
3903 if (local_syms
== NULL
)
3906 all_local_syms
[bfd_indx
] = local_syms
;
3909 return stub_changed
;
3912 /* Determine and set the size of the stub section for a final link.
3914 The basic idea here is to examine all the relocations looking for
3915 PC-relative calls to a target that is unreachable with a "CALLR"
3918 /* See elf32-hppa.c and elf64-ppc.c. */
3921 elf_metag_size_stubs(bfd
*output_bfd
, bfd
*stub_bfd
,
3922 struct bfd_link_info
*info
,
3923 bfd_signed_vma group_size
,
3924 asection
* (*add_stub_section
) (const char *, asection
*),
3925 void (*layout_sections_again
) (void))
3927 bfd_size_type stub_group_size
;
3928 bfd_boolean stubs_always_before_branch
;
3929 bfd_boolean stub_changed
;
3930 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3932 /* Stash our params away. */
3933 htab
->stub_bfd
= stub_bfd
;
3934 htab
->add_stub_section
= add_stub_section
;
3935 htab
->layout_sections_again
= layout_sections_again
;
3936 stubs_always_before_branch
= group_size
< 0;
3938 stub_group_size
= -group_size
;
3940 stub_group_size
= group_size
;
3941 if (stub_group_size
== 1)
3943 /* Default values. */
3944 /* FIXME: not sure what these values should be */
3945 if (stubs_always_before_branch
)
3947 stub_group_size
= (1 << BRANCH_BITS
);
3951 stub_group_size
= (1 << BRANCH_BITS
);
3955 group_sections (htab
, stub_group_size
, stubs_always_before_branch
);
3957 switch (get_local_syms (output_bfd
, info
->input_bfds
, info
))
3960 if (htab
->all_local_syms
)
3961 goto error_ret_free_local
;
3965 stub_changed
= FALSE
;
3969 stub_changed
= TRUE
;
3976 unsigned int bfd_indx
;
3979 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
3981 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3983 Elf_Internal_Shdr
*symtab_hdr
;
3985 Elf_Internal_Sym
*local_syms
;
3987 /* We'll need the symbol table in a second. */
3988 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3989 if (symtab_hdr
->sh_info
== 0)
3992 local_syms
= htab
->all_local_syms
[bfd_indx
];
3994 /* Walk over each section attached to the input bfd. */
3995 for (section
= input_bfd
->sections
;
3997 section
= section
->next
)
3999 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
4001 /* If there aren't any relocs, then there's nothing more
4003 if ((section
->flags
& SEC_RELOC
) == 0
4004 || section
->reloc_count
== 0)
4007 /* If this section is a link-once section that will be
4008 discarded, then don't create any stubs. */
4009 if (section
->output_section
== NULL
4010 || section
->output_section
->owner
!= output_bfd
)
4013 /* Get the relocs. */
4015 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
4017 if (internal_relocs
== NULL
)
4018 goto error_ret_free_local
;
4020 /* Now examine each relocation. */
4021 irela
= internal_relocs
;
4022 irelaend
= irela
+ section
->reloc_count
;
4023 for (; irela
< irelaend
; irela
++)
4025 unsigned int r_type
, r_indx
;
4026 enum elf_metag_stub_type stub_type
;
4027 struct elf_metag_stub_hash_entry
*hsh
;
4030 bfd_vma destination
;
4031 struct elf_metag_link_hash_entry
*hh
;
4033 const asection
*id_sec
;
4035 r_type
= ELF32_R_TYPE (irela
->r_info
);
4036 r_indx
= ELF32_R_SYM (irela
->r_info
);
4038 if (r_type
>= (unsigned int) R_METAG_MAX
)
4040 bfd_set_error (bfd_error_bad_value
);
4041 error_ret_free_internal
:
4042 if (elf_section_data (section
)->relocs
== NULL
)
4043 free (internal_relocs
);
4044 goto error_ret_free_local
;
4047 /* Only look for stubs on CALLR and B instructions. */
4048 if (!(r_type
== (unsigned int) R_METAG_RELBRANCH
||
4049 r_type
== (unsigned int) R_METAG_RELBRANCH_PLT
))
4052 /* Now determine the call target, its name, value,
4058 if (r_indx
< symtab_hdr
->sh_info
)
4060 /* It's a local symbol. */
4061 Elf_Internal_Sym
*sym
;
4062 Elf_Internal_Shdr
*hdr
;
4065 sym
= local_syms
+ r_indx
;
4066 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
4067 sym_value
= sym
->st_value
;
4068 shndx
= sym
->st_shndx
;
4069 if (shndx
< elf_numsections (input_bfd
))
4071 hdr
= elf_elfsections (input_bfd
)[shndx
];
4072 sym_sec
= hdr
->bfd_section
;
4073 destination
= (sym_value
+ irela
->r_addend
4074 + sym_sec
->output_offset
4075 + sym_sec
->output_section
->vma
);
4080 /* It's an external symbol. */
4083 e_indx
= r_indx
- symtab_hdr
->sh_info
;
4084 hh
= ((struct elf_metag_link_hash_entry
*)
4085 elf_sym_hashes (input_bfd
)[e_indx
]);
4087 while (hh
->eh
.root
.type
== bfd_link_hash_indirect
4088 || hh
->eh
.root
.type
== bfd_link_hash_warning
)
4089 hh
= ((struct elf_metag_link_hash_entry
*)
4090 hh
->eh
.root
.u
.i
.link
);
4092 if (hh
->eh
.root
.type
== bfd_link_hash_defined
4093 || hh
->eh
.root
.type
== bfd_link_hash_defweak
)
4095 sym_sec
= hh
->eh
.root
.u
.def
.section
;
4096 sym_value
= hh
->eh
.root
.u
.def
.value
;
4097 if (hh
->eh
.plt
.offset
!= (bfd_vma
) -1
4098 && hh
->eh
.dynindx
!= -1
4099 && r_type
== (unsigned int) R_METAG_RELBRANCH_PLT
)
4101 sym_sec
= htab
->etab
.splt
;
4102 sym_value
= hh
->eh
.plt
.offset
;
4105 if (sym_sec
->output_section
!= NULL
)
4106 destination
= (sym_value
+ irela
->r_addend
4107 + sym_sec
->output_offset
4108 + sym_sec
->output_section
->vma
);
4112 else if (hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
4114 if (! bfd_link_pic (info
))
4117 else if (hh
->eh
.root
.type
== bfd_link_hash_undefined
)
4119 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
4120 && (ELF_ST_VISIBILITY (hh
->eh
.other
)
4126 bfd_set_error (bfd_error_bad_value
);
4127 goto error_ret_free_internal
;
4131 /* Determine what (if any) linker stub is needed. */
4132 stub_type
= metag_type_of_stub (section
, irela
, hh
,
4134 if (stub_type
== metag_stub_none
)
4137 /* Support for grouping stub sections. */
4138 id_sec
= htab
->stub_group
[section
->id
].link_sec
;
4140 /* Get the name of this stub. */
4141 stub_name
= metag_stub_name (id_sec
, sym_sec
, hh
, irela
);
4143 goto error_ret_free_internal
;
4145 hsh
= metag_stub_hash_lookup (&htab
->bstab
,
4150 /* The proper stub has already been created. */
4155 hsh
= metag_add_stub (stub_name
, section
, htab
);
4159 goto error_ret_free_internal
;
4161 hsh
->target_value
= sym_value
;
4162 hsh
->target_section
= sym_sec
;
4163 hsh
->stub_type
= stub_type
;
4165 hsh
->addend
= irela
->r_addend
;
4166 stub_changed
= TRUE
;
4169 /* We're done with the internal relocs, free them. */
4170 if (elf_section_data (section
)->relocs
== NULL
)
4171 free (internal_relocs
);
4178 /* OK, we've added some stubs. Find out the new size of the
4180 for (stub_sec
= htab
->stub_bfd
->sections
;
4182 stub_sec
= stub_sec
->next
)
4185 bfd_hash_traverse (&htab
->bstab
, metag_size_one_stub
, htab
);
4187 /* Ask the linker to do its stuff. */
4188 (*htab
->layout_sections_again
) ();
4189 stub_changed
= FALSE
;
4192 free (htab
->all_local_syms
);
4195 error_ret_free_local
:
4196 free (htab
->all_local_syms
);
4200 /* Build all the stubs associated with the current output file. The
4201 stubs are kept in a hash table attached to the main linker hash
4202 table. This function is called via metagelf_finish in the linker. */
4205 elf_metag_build_stubs (struct bfd_link_info
*info
)
4208 struct bfd_hash_table
*table
;
4209 struct elf_metag_link_hash_table
*htab
;
4211 htab
= metag_link_hash_table (info
);
4213 for (stub_sec
= htab
->stub_bfd
->sections
;
4215 stub_sec
= stub_sec
->next
)
4219 /* Allocate memory to hold the linker stubs. */
4220 size
= stub_sec
->size
;
4221 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
4222 if (stub_sec
->contents
== NULL
&& size
!= 0)
4227 /* Build the stubs as directed by the stub hash table. */
4228 table
= &htab
->bstab
;
4229 bfd_hash_traverse (table
, metag_build_one_stub
, info
);
4234 /* Return TRUE if SYM represents a local label symbol. */
4237 elf_metag_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
, const char *name
)
4239 if (name
[0] == '$' && name
[1] == 'L')
4241 return _bfd_elf_is_local_label_name (abfd
, name
);
4244 /* Return address for Ith PLT stub in section PLT, for relocation REL
4245 or (bfd_vma) -1 if it should not be included. */
4248 elf_metag_plt_sym_val (bfd_vma i
, const asection
*plt
,
4249 const arelent
*rel ATTRIBUTE_UNUSED
)
4251 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
4254 #define ELF_ARCH bfd_arch_metag
4255 #define ELF_TARGET_ID METAG_ELF_DATA
4256 #define ELF_MACHINE_CODE EM_METAG
4257 #define ELF_MINPAGESIZE 0x1000
4258 #define ELF_MAXPAGESIZE 0x4000
4259 #define ELF_COMMONPAGESIZE 0x1000
4261 #define TARGET_LITTLE_SYM metag_elf32_vec
4262 #define TARGET_LITTLE_NAME "elf32-metag"
4264 #define elf_symbol_leading_char '_'
4266 #define elf_info_to_howto_rel NULL
4267 #define elf_info_to_howto metag_info_to_howto_rela
4269 #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
4270 #define bfd_elf32_bfd_link_hash_table_create \
4271 elf_metag_link_hash_table_create
4272 #define elf_backend_relocate_section elf_metag_relocate_section
4273 #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
4274 #define elf_backend_gc_sweep_hook elf_metag_gc_sweep_hook
4275 #define elf_backend_check_relocs elf_metag_check_relocs
4276 #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
4277 #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
4278 #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
4279 #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
4280 #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
4281 #define elf_backend_omit_section_dynsym \
4282 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4283 #define elf_backend_post_process_headers elf_metag_post_process_headers
4284 #define elf_backend_reloc_type_class elf_metag_reloc_type_class
4285 #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
4286 #define elf_backend_plt_sym_val elf_metag_plt_sym_val
4288 #define elf_backend_can_gc_sections 1
4289 #define elf_backend_can_refcount 1
4290 #define elf_backend_rela_normal 1
4291 #define elf_backend_want_got_plt 1
4292 #define elf_backend_want_got_sym 0
4293 #define elf_backend_want_plt_sym 0
4294 #define elf_backend_plt_readonly 1
4295 #define elf_backend_dtrel_excludes_plt 1
4297 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4298 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4300 #include "elf32-target.h"