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. */
854 /* Small local sym cache. */
855 struct sym_cache sym_cache
;
857 /* Data for LDM relocations. */
860 bfd_signed_vma refcount
;
865 /* Return the base vma address which should be subtracted from the
866 real address when resolving a dtpoff relocation. This is PT_TLS
869 dtpoff_base (struct bfd_link_info
*info
)
871 /* If tls_sec is NULL, we should have signalled an error already. */
872 if (elf_hash_table (info
)->tls_sec
== NULL
)
874 return elf_hash_table (info
)->tls_sec
->vma
;
877 /* Return the relocation value for R_METAG_TLS_IE */
879 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
881 /* If tls_sec is NULL, we should have signalled an error already. */
882 if (elf_hash_table (info
)->tls_sec
== NULL
)
884 /* METAG TLS ABI is variant I and static TLS blocks start just after
885 tcbhead structure which has 2 pointer fields. */
886 return (address
- elf_hash_table (info
)->tls_sec
->vma
887 + align_power ((bfd_vma
) 8,
888 elf_hash_table (info
)->tls_sec
->alignment_power
));
892 metag_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
894 Elf_Internal_Rela
*dst
)
898 r_type
= ELF32_R_TYPE (dst
->r_info
);
899 if (r_type
>= (unsigned int) R_METAG_MAX
)
901 /* xgettext:c-format */
902 _bfd_error_handler (_("%B: invalid METAG reloc number: %d"), abfd
, r_type
);
905 cache_ptr
->howto
= & elf_metag_howto_table
[r_type
];
908 static reloc_howto_type
*
909 metag_reloc_type_lookup (bfd
* abfd ATTRIBUTE_UNUSED
,
910 bfd_reloc_code_real_type code
)
914 for (i
= 0; i
< sizeof (metag_reloc_map
) / sizeof (metag_reloc_map
[0]); i
++)
915 if (metag_reloc_map
[i
].bfd_reloc_val
== code
)
916 return & elf_metag_howto_table
[metag_reloc_map
[i
].metag_reloc_val
];
921 static reloc_howto_type
*
922 metag_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
927 for (i
= 0; i
< sizeof (elf_metag_howto_table
) / sizeof (elf_metag_howto_table
[0]); i
++)
928 if (elf_metag_howto_table
[i
].name
!= NULL
929 && strcasecmp (elf_metag_howto_table
[i
].name
, r_name
) == 0)
930 return &elf_metag_howto_table
[i
];
935 /* Various hash macros and functions. */
936 #define metag_link_hash_table(p) \
937 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
938 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
940 #define metag_elf_hash_entry(ent) \
941 ((struct elf_metag_link_hash_entry *)(ent))
943 #define metag_stub_hash_entry(ent) \
944 ((struct elf_metag_stub_hash_entry *)(ent))
946 #define metag_stub_hash_lookup(table, string, create, copy) \
947 ((struct elf_metag_stub_hash_entry *) \
948 bfd_hash_lookup ((table), (string), (create), (copy)))
950 #define metag_elf_local_got_tls_type(abfd) \
951 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
953 /* Assorted hash table functions. */
955 /* Initialize an entry in the stub hash table. */
957 static struct bfd_hash_entry
*
958 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
959 struct bfd_hash_table
*table
,
962 /* Allocate the structure if it has not already been allocated by a
966 entry
= bfd_hash_allocate (table
,
967 sizeof (struct elf_metag_stub_hash_entry
));
972 /* Call the allocation method of the superclass. */
973 entry
= bfd_hash_newfunc (entry
, table
, string
);
976 struct elf_metag_stub_hash_entry
*hsh
;
978 /* Initialize the local fields. */
979 hsh
= (struct elf_metag_stub_hash_entry
*) entry
;
980 hsh
->stub_sec
= NULL
;
981 hsh
->stub_offset
= 0;
982 hsh
->target_value
= 0;
983 hsh
->target_section
= NULL
;
984 hsh
->stub_type
= metag_stub_long_branch
;
992 /* Initialize an entry in the link hash table. */
994 static struct bfd_hash_entry
*
995 metag_link_hash_newfunc (struct bfd_hash_entry
*entry
,
996 struct bfd_hash_table
*table
,
999 /* Allocate the structure if it has not already been allocated by a
1003 entry
= bfd_hash_allocate (table
,
1004 sizeof (struct elf_metag_link_hash_entry
));
1009 /* Call the allocation method of the superclass. */
1010 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
1013 struct elf_metag_link_hash_entry
*hh
;
1015 /* Initialize the local fields. */
1016 hh
= (struct elf_metag_link_hash_entry
*) entry
;
1017 hh
->hsh_cache
= NULL
;
1018 hh
->dyn_relocs
= NULL
;
1019 hh
->tls_type
= GOT_UNKNOWN
;
1025 /* Free the derived linker hash table. */
1028 elf_metag_link_hash_table_free (bfd
*obfd
)
1030 struct elf_metag_link_hash_table
*htab
1031 = (struct elf_metag_link_hash_table
*) obfd
->link
.hash
;
1033 bfd_hash_table_free (&htab
->bstab
);
1034 _bfd_elf_link_hash_table_free (obfd
);
1037 /* Create the derived linker hash table. The Meta ELF port uses the derived
1038 hash table to keep information specific to the Meta ELF linker (without
1039 using static variables). */
1041 static struct bfd_link_hash_table
*
1042 elf_metag_link_hash_table_create (bfd
*abfd
)
1044 struct elf_metag_link_hash_table
*htab
;
1045 bfd_size_type amt
= sizeof (*htab
);
1047 htab
= bfd_zmalloc (amt
);
1051 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
1052 metag_link_hash_newfunc
,
1053 sizeof (struct elf_metag_link_hash_entry
),
1060 /* Init the stub hash table too. */
1061 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
1062 sizeof (struct elf_metag_stub_hash_entry
)))
1064 _bfd_elf_link_hash_table_free (abfd
);
1067 htab
->etab
.root
.hash_table_free
= elf_metag_link_hash_table_free
;
1069 return &htab
->etab
.root
;
1072 /* Section name for stubs is the associated section name plus this
1074 #define STUB_SUFFIX ".stub"
1076 /* Build a name for an entry in the stub hash table. */
1079 metag_stub_name (const asection
*input_section
,
1080 const asection
*sym_sec
,
1081 const struct elf_metag_link_hash_entry
*hh
,
1082 const Elf_Internal_Rela
*rel
)
1089 len
= 8 + 1 + strlen (hh
->eh
.root
.root
.string
) + 1 + 8 + 1;
1090 stub_name
= bfd_malloc (len
);
1091 if (stub_name
!= NULL
)
1093 sprintf (stub_name
, "%08x_%s+%x",
1094 input_section
->id
& 0xffffffff,
1095 hh
->eh
.root
.root
.string
,
1096 (int) rel
->r_addend
& 0xffffffff);
1101 len
= 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1102 stub_name
= bfd_malloc (len
);
1103 if (stub_name
!= NULL
)
1105 sprintf (stub_name
, "%08x_%x:%x+%x",
1106 input_section
->id
& 0xffffffff,
1107 sym_sec
->id
& 0xffffffff,
1108 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
1109 (int) rel
->r_addend
& 0xffffffff);
1115 /* Look up an entry in the stub hash. Stub entries are cached because
1116 creating the stub name takes a bit of time. */
1118 static struct elf_metag_stub_hash_entry
*
1119 metag_get_stub_entry (const asection
*input_section
,
1120 const asection
*sym_sec
,
1121 struct elf_metag_link_hash_entry
*hh
,
1122 const Elf_Internal_Rela
*rel
,
1123 struct elf_metag_link_hash_table
*htab
)
1125 struct elf_metag_stub_hash_entry
*hsh
;
1126 const asection
*id_sec
;
1128 /* If this input section is part of a group of sections sharing one
1129 stub section, then use the id of the first section in the group.
1130 Stub names need to include a section id, as there may well be
1131 more than one stub used to reach say, printf, and we need to
1132 distinguish between them. */
1133 id_sec
= htab
->stub_group
[input_section
->id
].link_sec
;
1135 if (hh
!= NULL
&& hh
->hsh_cache
!= NULL
1136 && hh
->hsh_cache
->hh
== hh
1137 && hh
->hsh_cache
->id_sec
== id_sec
)
1139 hsh
= hh
->hsh_cache
;
1145 stub_name
= metag_stub_name (id_sec
, sym_sec
, hh
, rel
);
1146 if (stub_name
== NULL
)
1149 hsh
= metag_stub_hash_lookup (&htab
->bstab
,
1150 stub_name
, FALSE
, FALSE
);
1153 hh
->hsh_cache
= hsh
;
1161 /* Add a new stub entry to the stub hash. Not all fields of the new
1162 stub entry are initialised. */
1164 static struct elf_metag_stub_hash_entry
*
1165 metag_add_stub (const char *stub_name
,
1167 struct elf_metag_link_hash_table
*htab
)
1171 struct elf_metag_stub_hash_entry
*hsh
;
1173 link_sec
= htab
->stub_group
[section
->id
].link_sec
;
1174 stub_sec
= htab
->stub_group
[section
->id
].stub_sec
;
1175 if (stub_sec
== NULL
)
1177 stub_sec
= htab
->stub_group
[link_sec
->id
].stub_sec
;
1178 if (stub_sec
== NULL
)
1184 namelen
= strlen (link_sec
->name
);
1185 len
= namelen
+ sizeof (STUB_SUFFIX
);
1186 s_name
= bfd_alloc (htab
->stub_bfd
, len
);
1190 memcpy (s_name
, link_sec
->name
, namelen
);
1191 memcpy (s_name
+ namelen
, STUB_SUFFIX
, sizeof (STUB_SUFFIX
));
1193 stub_sec
= (*htab
->add_stub_section
) (s_name
, link_sec
);
1194 if (stub_sec
== NULL
)
1196 htab
->stub_group
[link_sec
->id
].stub_sec
= stub_sec
;
1198 htab
->stub_group
[section
->id
].stub_sec
= stub_sec
;
1201 /* Enter this entry into the linker stub hash table. */
1202 hsh
= metag_stub_hash_lookup (&htab
->bstab
, stub_name
,
1206 /* xgettext:c-format */
1207 _bfd_error_handler (_("%B: cannot create stub entry %s"),
1208 section
->owner
, stub_name
);
1212 hsh
->stub_sec
= stub_sec
;
1213 hsh
->stub_offset
= 0;
1214 hsh
->id_sec
= link_sec
;
1218 /* Check a signed integer value can be represented in the given number
1222 within_signed_range (int value
, unsigned int bits
)
1224 int min_val
= -(1 << (bits
- 1));
1225 int max_val
= (1 << (bits
- 1)) - 1;
1226 return (value
<= max_val
) && (value
>= min_val
);
1229 /* Perform a relocation as part of a final link. */
1231 static bfd_reloc_status_type
1232 metag_final_link_relocate (reloc_howto_type
*howto
,
1234 asection
*input_section
,
1236 Elf_Internal_Rela
*rel
,
1238 struct elf_metag_link_hash_entry
*hh
,
1239 struct elf_metag_link_hash_table
*htab
,
1242 bfd_reloc_status_type r
= bfd_reloc_ok
;
1243 bfd_byte
*hit_data
= contents
+ rel
->r_offset
;
1244 int opcode
, op_shift
, op_extended
, l1
, l2
;
1245 bfd_signed_vma srel
, addend
= rel
->r_addend
;
1246 struct elf_metag_stub_hash_entry
*hsh
= NULL
;
1249 /* Find out where we are and where we're going. */
1250 location
= (rel
->r_offset
+
1251 input_section
->output_offset
+
1252 input_section
->output_section
->vma
);
1254 switch (howto
->type
)
1256 case R_METAG_RELBRANCH
:
1257 case R_METAG_RELBRANCH_PLT
:
1258 /* Make it a pc relative offset. */
1259 relocation
-= location
;
1261 case R_METAG_TLS_GD
:
1262 case R_METAG_TLS_IE
:
1263 relocation
-= elf_gp (input_section
->output_section
->owner
);
1269 switch (howto
->type
)
1271 case R_METAG_RELBRANCH_PLT
:
1272 case R_METAG_RELBRANCH
:
1273 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1275 srel
= (bfd_signed_vma
) relocation
;
1278 /* If the branch is out of reach, then redirect the
1279 call to the local stub for this function. */
1280 if (srel
> ((1 << (BRANCH_BITS
+ 1)) - 1) ||
1281 (srel
< - (1 << (BRANCH_BITS
+ 1))))
1283 if (sym_sec
== NULL
)
1286 hsh
= metag_get_stub_entry (input_section
, sym_sec
,
1289 return bfd_reloc_undefined
;
1291 /* Munge up the value and addend so that we call the stub
1292 rather than the procedure directly. */
1293 srel
= (hsh
->stub_offset
1294 + hsh
->stub_sec
->output_offset
1295 + hsh
->stub_sec
->output_section
->vma
);
1301 if (!within_signed_range (srel
, BRANCH_BITS
))
1303 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1306 return bfd_reloc_overflow
;
1309 opcode
&= ~(0x7ffff << 5);
1310 opcode
|= ((srel
& 0x7ffff) << 5);
1312 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1314 case R_METAG_GETSETOFF
:
1315 case R_METAG_GETSET_GOT
:
1316 case R_METAG_GETSET_GOTOFF
:
1317 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1319 srel
= (bfd_signed_vma
) relocation
;
1322 /* Is this a standard or extended GET/SET? */
1323 if ((opcode
& 0xf0000000) == 0xa0000000)
1325 /* Extended GET/SET. */
1332 /* Standard GET/SET. */
1333 l1
= opcode
& 0x01000000;
1334 l2
= opcode
& 0x04000000;
1338 /* Calculate the width of the GET/SET and how much we need to
1339 shift the result by. */
1351 /* GET/SET offsets are scaled by the width of the transfer. */
1352 srel
= srel
>> op_shift
;
1354 /* Extended GET/SET has signed 12 bits of offset, standard has
1358 if (!within_signed_range (srel
, 12))
1360 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1363 return bfd_reloc_overflow
;
1365 opcode
&= ~(0xfff << 7);
1366 opcode
|= ((srel
& 0xfff) << 7);
1370 if (!within_signed_range (srel
, 5))
1372 if (hh
&& hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
1375 return bfd_reloc_overflow
;
1377 opcode
&= ~(0x3f << 8);
1378 opcode
|= ((srel
& 0x3f) << 8);
1381 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1383 case R_METAG_TLS_GD
:
1384 case R_METAG_TLS_LDM
:
1385 opcode
= bfd_get_32 (input_bfd
, hit_data
);
1387 if ((bfd_signed_vma
)relocation
< 0)
1389 /* sign extend immediate */
1390 if ((opcode
& 0xf2000001) == 0x02000000)
1392 /* ADD De.e,Dx.r,#I16 */
1396 return bfd_reloc_overflow
;
1399 bfd_put_32 (input_bfd
, opcode
, hit_data
);
1401 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1402 contents
, rel
->r_offset
,
1403 relocation
, rel
->r_addend
);
1406 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1407 contents
, rel
->r_offset
,
1408 relocation
, rel
->r_addend
);
1414 /* This is defined because R_METAG_NONE != 0...
1415 See RELOC_AGAINST_DISCARDED_SECTION for details. */
1416 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1417 rel, relend, howto, contents) \
1419 _bfd_clear_contents (howto, input_bfd, input_section, \
1420 contents + rel->r_offset); \
1422 if (bfd_link_relocatable (info) \
1423 && (input_section->flags & SEC_DEBUGGING)) \
1425 /* Only remove relocations in debug sections since other \
1426 sections may require relocations. */ \
1427 Elf_Internal_Shdr *rel_hdr; \
1429 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1431 /* Avoid empty output section. */ \
1432 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \
1434 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1435 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \
1436 rel_hdr->sh_size -= rel_hdr->sh_entsize; \
1438 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \
1440 input_section->reloc_count--; \
1447 rel->r_info = R_METAG_NONE; \
1448 rel->r_addend = 0; \
1452 /* Relocate a META ELF section.
1454 The RELOCATE_SECTION function is called by the new ELF backend linker
1455 to handle the relocations for a section.
1457 The relocs are always passed as Rela structures; if the section
1458 actually uses Rel structures, the r_addend field will always be
1461 This function is responsible for adjusting the section contents as
1462 necessary, and (if using Rela relocs and generating a relocatable
1463 output file) adjusting the reloc addend as necessary.
1465 This function does not have to worry about setting the reloc
1466 address or the reloc symbol index.
1468 LOCAL_SYMS is a pointer to the swapped in local symbols.
1470 LOCAL_SECTIONS is an array giving the section in the input file
1471 corresponding to the st_shndx field of each local symbol.
1473 The global hash table entry for the global symbols can be found
1474 via elf_sym_hashes (input_bfd).
1476 When generating relocatable output, this function must handle
1477 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1478 going to be the section symbol corresponding to the output
1479 section, which means that the addend must be adjusted
1483 elf_metag_relocate_section (bfd
*output_bfd
,
1484 struct bfd_link_info
*info
,
1486 asection
*input_section
,
1488 Elf_Internal_Rela
*relocs
,
1489 Elf_Internal_Sym
*local_syms
,
1490 asection
**local_sections
)
1492 bfd_vma
*local_got_offsets
;
1493 Elf_Internal_Shdr
*symtab_hdr
;
1494 struct elf_link_hash_entry
**eh_syms
;
1495 struct elf_metag_link_hash_table
*htab
;
1496 Elf_Internal_Rela
*rel
;
1497 Elf_Internal_Rela
*relend
;
1500 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1501 eh_syms
= elf_sym_hashes (input_bfd
);
1502 relend
= relocs
+ input_section
->reloc_count
;
1504 htab
= metag_link_hash_table (info
);
1505 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1509 for (rel
= relocs
; rel
< relend
; rel
++)
1511 reloc_howto_type
*howto
;
1512 unsigned long r_symndx
;
1513 Elf_Internal_Sym
*sym
;
1515 struct elf_metag_link_hash_entry
*hh
;
1517 bfd_reloc_status_type r
;
1521 r_type
= ELF32_R_TYPE (rel
->r_info
);
1523 if (r_type
== R_METAG_GNU_VTINHERIT
1524 || r_type
== R_METAG_GNU_VTENTRY
1525 || r_type
== R_METAG_NONE
)
1528 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1530 howto
= elf_metag_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
1535 if (r_symndx
< symtab_hdr
->sh_info
)
1537 sym
= local_syms
+ r_symndx
;
1538 sec
= local_sections
[r_symndx
];
1539 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1541 name
= bfd_elf_string_from_elf_section
1542 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1543 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1547 struct elf_link_hash_entry
*eh
;
1548 bfd_boolean unresolved_reloc
, warned
, ignored
;
1550 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1551 r_symndx
, symtab_hdr
, eh_syms
,
1552 eh
, sec
, relocation
,
1553 unresolved_reloc
, warned
, ignored
);
1555 name
= eh
->root
.root
.string
;
1556 hh
= (struct elf_metag_link_hash_entry
*) eh
;
1559 if (sec
!= NULL
&& discarded_section (sec
))
1560 METAG_RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1561 rel
, relend
, howto
, contents
);
1563 if (bfd_link_relocatable (info
))
1568 case R_METAG_ADDR32
:
1569 case R_METAG_RELBRANCH
:
1570 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1573 if ((bfd_link_pic (info
)
1574 && r_symndx
!= STN_UNDEF
1575 && (input_section
->flags
& SEC_ALLOC
) != 0
1576 && (r_type
!= R_METAG_RELBRANCH
1577 || !SYMBOL_CALLS_LOCAL (info
, &hh
->eh
)))
1578 || (!bfd_link_pic (info
)
1580 && hh
->eh
.dynindx
!= -1
1581 && !hh
->eh
.non_got_ref
1582 && ((hh
->eh
.def_dynamic
1583 && !hh
->eh
.def_regular
)
1584 || hh
->eh
.root
.type
== bfd_link_hash_undefweak
1585 || hh
->eh
.root
.type
== bfd_link_hash_undefined
)))
1587 Elf_Internal_Rela outrel
;
1588 bfd_boolean skip
, relocate
;
1591 /* When generating a shared object, these relocations
1592 are copied into the output file to be resolved at run
1595 sreloc
= elf_section_data (input_section
)->sreloc
;
1596 BFD_ASSERT (sreloc
!= NULL
);
1601 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
1605 if (outrel
.r_offset
== (bfd_vma
) -1)
1607 else if (outrel
.r_offset
== (bfd_vma
) -2)
1608 skip
= TRUE
, relocate
= TRUE
;
1609 outrel
.r_offset
+= (input_section
->output_section
->vma
1610 + input_section
->output_offset
);
1614 memset (&outrel
, 0, sizeof outrel
);
1615 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_NONE
);
1617 else if (r_type
== R_METAG_RELBRANCH
)
1619 BFD_ASSERT (hh
!= NULL
&& hh
->eh
.dynindx
!= -1);
1620 outrel
.r_info
= ELF32_R_INFO (hh
->eh
.dynindx
, r_type
);
1621 outrel
.r_addend
= rel
->r_addend
;
1625 /* h->dynindx may be -1 if this symbol was marked to
1628 || ((info
->symbolic
|| hh
->eh
.dynindx
== -1)
1629 && hh
->eh
.def_regular
))
1632 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
1633 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1637 BFD_ASSERT (hh
->eh
.dynindx
!= -1);
1638 outrel
.r_info
= ELF32_R_INFO (hh
->eh
.dynindx
, r_type
);
1639 outrel
.r_addend
= rel
->r_addend
;
1643 loc
= sreloc
->contents
;
1644 loc
+= sreloc
->reloc_count
* sizeof(Elf32_External_Rela
);
1645 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,loc
);
1646 ++sreloc
->reloc_count
;
1648 /* If this reloc is against an external symbol, we do
1649 not want to fiddle with the addend. Otherwise, we
1650 need to include the symbol value so that it becomes
1651 an addend for the dynamic reloc. */
1657 case R_METAG_RELBRANCH_PLT
:
1658 /* Relocation is to the entry for this symbol in the
1659 procedure linkage table. */
1664 if (hh
->eh
.forced_local
)
1667 if (hh
->eh
.plt
.offset
== (bfd_vma
) -1 ||
1670 /* We didn't make a PLT entry for this symbol. This
1671 happens when statically linking PIC code, or when
1672 using -Bsymbolic. */
1676 relocation
= (htab
->splt
->output_section
->vma
1677 + htab
->splt
->output_offset
1678 + hh
->eh
.plt
.offset
);
1680 case R_METAG_HI16_GOTPC
:
1681 case R_METAG_LO16_GOTPC
:
1682 BFD_ASSERT (htab
->sgot
!= NULL
);
1684 relocation
= (htab
->sgot
->output_section
->vma
+
1685 htab
->sgot
->output_offset
);
1686 relocation
+= GOT_REG_OFFSET
;
1687 relocation
-= (input_section
->output_section
->vma
1688 + input_section
->output_offset
1691 case R_METAG_HI16_GOTOFF
:
1692 case R_METAG_LO16_GOTOFF
:
1693 case R_METAG_GETSET_GOTOFF
:
1694 BFD_ASSERT (htab
->sgot
!= NULL
);
1696 relocation
-= (htab
->sgot
->output_section
->vma
+
1697 htab
->sgot
->output_offset
);
1698 relocation
-= GOT_REG_OFFSET
;
1700 case R_METAG_GETSET_GOT
:
1703 bfd_boolean do_got
= 0;
1705 /* Relocation is to the entry for this symbol in the
1706 global offset table. */
1711 off
= hh
->eh
.got
.offset
;
1712 dyn
= htab
->etab
.dynamic_sections_created
;
1713 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
1714 bfd_link_pic (info
),
1717 /* If we aren't going to call finish_dynamic_symbol,
1718 then we need to handle initialisation of the .got
1719 entry and create needed relocs here. Since the
1720 offset must always be a multiple of 4, we use the
1721 least significant bit to record whether we have
1722 initialised it already. */
1727 hh
->eh
.got
.offset
|= 1;
1734 /* Local symbol case. */
1735 if (local_got_offsets
== NULL
)
1738 off
= local_got_offsets
[r_symndx
];
1740 /* The offset must always be a multiple of 4. We use
1741 the least significant bit to record whether we have
1742 already generated the necessary reloc. */
1747 local_got_offsets
[r_symndx
] |= 1;
1754 if (bfd_link_pic (info
))
1756 /* Output a dynamic relocation for this GOT entry.
1757 In this case it is relative to the base of the
1758 object because the symbol index is zero. */
1759 Elf_Internal_Rela outrel
;
1761 asection
*s
= htab
->srelgot
;
1763 outrel
.r_offset
= (off
1764 + htab
->sgot
->output_offset
1765 + htab
->sgot
->output_section
->vma
);
1766 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
1767 outrel
.r_addend
= relocation
;
1769 loc
+= s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
1770 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1773 bfd_put_32 (output_bfd
, relocation
,
1774 htab
->sgot
->contents
+ off
);
1777 if (off
>= (bfd_vma
) -2)
1780 relocation
= off
- GOT_REG_OFFSET
;
1783 case R_METAG_TLS_GD
:
1784 case R_METAG_TLS_IE
:
1786 /* XXXMJF There is room here for optimisations. For example
1787 converting from GD->IE, etc. */
1792 if (htab
->sgot
== NULL
)
1799 dyn
= htab
->etab
.dynamic_sections_created
;
1801 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
1802 bfd_link_pic (info
),
1804 && (!bfd_link_pic (info
)
1805 || !SYMBOL_REFERENCES_LOCAL (info
, &hh
->eh
)))
1807 indx
= hh
->eh
.dynindx
;
1809 off
= hh
->eh
.got
.offset
;
1810 tls_type
= hh
->tls_type
;
1814 /* Local symbol case. */
1815 if (local_got_offsets
== NULL
)
1818 off
= local_got_offsets
[r_symndx
];
1819 tls_type
= metag_elf_local_got_tls_type (input_bfd
) [r_symndx
];
1822 if (tls_type
== GOT_UNKNOWN
)
1829 bfd_boolean need_relocs
= FALSE
;
1830 Elf_Internal_Rela outrel
;
1831 bfd_byte
*loc
= NULL
;
1834 /* The GOT entries have not been initialized yet. Do it
1835 now, and emit any relocations. If both an IE GOT and a
1836 GD GOT are necessary, we emit the GD first. */
1838 if ((bfd_link_pic (info
) || indx
!= 0)
1840 || ELF_ST_VISIBILITY (hh
->eh
.other
) == STV_DEFAULT
1841 || hh
->eh
.root
.type
!= bfd_link_hash_undefweak
))
1844 loc
= htab
->srelgot
->contents
;
1845 /* FIXME (CAO): Should this be reloc_count++ ? */
1846 loc
+= htab
->srelgot
->reloc_count
* sizeof (Elf32_External_Rela
);
1849 if (tls_type
& GOT_TLS_GD
)
1853 outrel
.r_offset
= (cur_off
1854 + htab
->sgot
->output_section
->vma
1855 + htab
->sgot
->output_offset
);
1856 outrel
.r_info
= ELF32_R_INFO (indx
, R_METAG_TLS_DTPMOD
);
1857 outrel
.r_addend
= 0;
1858 bfd_put_32 (output_bfd
, 0, htab
->sgot
->contents
+ cur_off
);
1860 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1861 htab
->srelgot
->reloc_count
++;
1862 loc
+= sizeof (Elf32_External_Rela
);
1865 bfd_put_32 (output_bfd
, 0,
1866 htab
->sgot
->contents
+ cur_off
+ 4);
1869 bfd_put_32 (output_bfd
, 0,
1870 htab
->sgot
->contents
+ cur_off
+ 4);
1871 outrel
.r_info
= ELF32_R_INFO (indx
,
1872 R_METAG_TLS_DTPOFF
);
1873 outrel
.r_offset
+= 4;
1874 bfd_elf32_swap_reloca_out (output_bfd
,
1876 htab
->srelgot
->reloc_count
++;
1877 loc
+= sizeof (Elf32_External_Rela
);
1882 /* We don't support changing the TLS model. */
1884 if (bfd_link_pic (info
))
1885 _bfd_error_handler (_("%B(%A): multiple TLS models are not supported"),
1886 input_bfd
, input_section
, name
);
1888 _bfd_error_handler (_("%B(%A): shared library symbol %s encountered whilst performing a static link"),
1889 input_bfd
, input_section
, name
);
1896 if (tls_type
& GOT_TLS_IE
)
1900 outrel
.r_offset
= (cur_off
1901 + htab
->sgot
->output_section
->vma
1902 + htab
->sgot
->output_offset
);
1903 outrel
.r_info
= ELF32_R_INFO (indx
, R_METAG_TLS_TPOFF
);
1906 outrel
.r_addend
= relocation
- dtpoff_base (info
);
1908 outrel
.r_addend
= 0;
1910 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1911 htab
->srelgot
->reloc_count
++;
1912 loc
+= sizeof (Elf32_External_Rela
);
1915 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
1916 htab
->sgot
->contents
+ cur_off
);
1922 hh
->eh
.got
.offset
|= 1;
1924 local_got_offsets
[r_symndx
] |= 1;
1927 /* Add the base of the GOT to the relocation value. */
1928 relocation
= off
- GOT_REG_OFFSET
;
1933 case R_METAG_TLS_IENONPIC_HI16
:
1934 case R_METAG_TLS_IENONPIC_LO16
:
1935 case R_METAG_TLS_LE_HI16
:
1936 case R_METAG_TLS_LE_LO16
:
1937 if (bfd_link_pic (info
))
1940 /* xgettext:c-format */
1941 (_("%B(%A+0x%lx): R_METAG_TLS_LE/IENONPIC relocation not permitted in shared object"),
1942 input_bfd
, input_section
,
1943 (long) rel
->r_offset
, howto
->name
);
1947 relocation
= tpoff (info
, relocation
);
1949 case R_METAG_TLS_LDO_HI16
:
1950 case R_METAG_TLS_LDO_LO16
:
1951 if (! bfd_link_pic (info
))
1952 relocation
= tpoff (info
, relocation
);
1954 relocation
-= dtpoff_base (info
);
1956 case R_METAG_TLS_LDM
:
1960 if (htab
->sgot
== NULL
)
1962 off
= htab
->tls_ldm_got
.offset
;
1967 Elf_Internal_Rela outrel
;
1970 outrel
.r_offset
= (off
1971 + htab
->sgot
->output_section
->vma
1972 + htab
->sgot
->output_offset
);
1974 outrel
.r_addend
= 0;
1975 outrel
.r_info
= ELF32_R_INFO (0, R_METAG_TLS_DTPMOD
);
1976 loc
= htab
->srelgot
->contents
;
1977 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
1978 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
1979 htab
->tls_ldm_got
.offset
|= 1;
1982 relocation
= off
- GOT_REG_OFFSET
;
1989 r
= metag_final_link_relocate (howto
, input_bfd
, input_section
,
1990 contents
, rel
, relocation
, hh
, htab
,
1993 if (r
!= bfd_reloc_ok
)
1995 const char * msg
= (const char *) NULL
;
1999 case bfd_reloc_overflow
:
2000 (*info
->callbacks
->reloc_overflow
)
2001 (info
, (hh
? &hh
->eh
.root
: NULL
), name
, howto
->name
,
2002 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2005 case bfd_reloc_undefined
:
2006 (*info
->callbacks
->undefined_symbol
)
2007 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
2010 case bfd_reloc_outofrange
:
2011 msg
= _("internal error: out of range error");
2014 case bfd_reloc_notsupported
:
2015 msg
= _("internal error: unsupported relocation error");
2018 case bfd_reloc_dangerous
:
2019 msg
= _("internal error: dangerous relocation");
2023 msg
= _("internal error: unknown error");
2028 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
2029 input_section
, rel
->r_offset
);
2036 /* Create the .plt and .got sections, and set up our hash table
2037 short-cuts to various dynamic sections. */
2040 elf_metag_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2042 struct elf_metag_link_hash_table
*htab
;
2043 struct elf_link_hash_entry
*eh
;
2044 struct bfd_link_hash_entry
*bh
;
2045 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2047 /* Don't try to create the .plt and .got twice. */
2048 htab
= metag_link_hash_table (info
);
2049 if (htab
->splt
!= NULL
)
2052 /* Call the generic code to do most of the work. */
2053 if (! _bfd_elf_create_dynamic_sections (abfd
, info
))
2056 htab
->sgot
= bfd_get_linker_section (abfd
, ".got");
2060 htab
->sgotplt
= bfd_make_section_with_flags (abfd
, ".got.plt",
2061 (SEC_ALLOC
| SEC_LOAD
|
2064 SEC_LINKER_CREATED
));
2065 if (htab
->sgotplt
== NULL
2066 || !bfd_set_section_alignment (abfd
, htab
->sgotplt
, 2))
2069 /* Define the symbol __GLOBAL_OFFSET_TABLE__ at the start of the .got
2070 section. We don't do this in the linker script because we don't want
2071 to define the symbol if we are not creating a global offset table. */
2073 if (!(_bfd_generic_link_add_one_symbol
2074 (info
, abfd
, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL
, htab
->sgot
,
2075 (bfd_vma
) 0, NULL
, FALSE
, bed
->collect
, &bh
)))
2077 eh
= (struct elf_link_hash_entry
*) bh
;
2078 eh
->def_regular
= 1;
2079 eh
->type
= STT_OBJECT
;
2080 eh
->other
= STV_HIDDEN
;
2082 if (! bfd_link_executable (info
)
2083 && ! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2086 elf_hash_table (info
)->hgot
= eh
;
2088 htab
->splt
= bfd_get_linker_section (abfd
, ".plt");
2089 htab
->srelplt
= bfd_get_linker_section (abfd
, ".rela.plt");
2091 htab
->srelgot
= bfd_get_linker_section (abfd
, ".rela.got");
2093 htab
->sdynbss
= bfd_get_linker_section (abfd
, ".dynbss");
2094 htab
->srelbss
= bfd_get_linker_section (abfd
, ".rela.bss");
2099 /* Look through the relocs for a section during the first phase, and
2100 calculate needed space in the global offset table, procedure linkage
2101 table, and dynamic reloc sections. At this point we haven't
2102 necessarily read all the input files. */
2105 elf_metag_check_relocs (bfd
*abfd
,
2106 struct bfd_link_info
*info
,
2108 const Elf_Internal_Rela
*relocs
)
2110 Elf_Internal_Shdr
*symtab_hdr
;
2111 struct elf_link_hash_entry
**eh_syms
;
2112 const Elf_Internal_Rela
*rel
;
2113 const Elf_Internal_Rela
*rel_end
;
2114 struct elf_metag_link_hash_table
*htab
;
2117 int tls_type
= GOT_UNKNOWN
, old_tls_type
= GOT_UNKNOWN
;
2119 if (bfd_link_relocatable (info
))
2122 htab
= metag_link_hash_table (info
);
2123 dynobj
= htab
->etab
.dynobj
;
2124 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2125 eh_syms
= elf_sym_hashes (abfd
);
2131 rel_end
= relocs
+ sec
->reloc_count
;
2132 for (rel
= relocs
; rel
< rel_end
; rel
++)
2135 struct elf_metag_link_hash_entry
*hh
;
2136 Elf_Internal_Sym
*isym
;
2137 unsigned long r_symndx
;
2139 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2140 r_type
= ELF32_R_TYPE (rel
->r_info
);
2141 if (r_symndx
< symtab_hdr
->sh_info
)
2143 /* A local symbol. */
2144 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
2155 hh
= (struct elf_metag_link_hash_entry
*)
2156 eh_syms
[r_symndx
- symtab_hdr
->sh_info
];
2157 while (hh
->eh
.root
.type
== bfd_link_hash_indirect
2158 || hh
->eh
.root
.type
== bfd_link_hash_warning
)
2159 hh
= (struct elf_metag_link_hash_entry
*) hh
->eh
.root
.u
.i
.link
;
2161 /* PR15323, ref flags aren't set for references in the same
2163 hh
->eh
.root
.non_ir_ref
= 1;
2166 /* Some relocs require a global offset table. */
2167 if (htab
->sgot
== NULL
)
2171 case R_METAG_TLS_GD
:
2172 case R_METAG_TLS_LDM
:
2173 case R_METAG_TLS_IE
:
2174 if (bfd_link_pic (info
))
2175 info
->flags
|= DF_STATIC_TLS
;
2178 case R_METAG_HI16_GOTOFF
:
2179 case R_METAG_LO16_GOTOFF
:
2180 case R_METAG_GETSET_GOTOFF
:
2181 case R_METAG_GETSET_GOT
:
2182 case R_METAG_HI16_GOTPC
:
2183 case R_METAG_LO16_GOTPC
:
2185 htab
->etab
.dynobj
= dynobj
= abfd
;
2186 if (!elf_metag_create_dynamic_sections (dynobj
, info
))
2197 case R_METAG_TLS_IE
:
2198 case R_METAG_TLS_GD
:
2199 case R_METAG_GETSET_GOT
:
2203 tls_type
= GOT_NORMAL
;
2205 case R_METAG_TLS_IE
:
2206 tls_type
= GOT_TLS_IE
;
2208 case R_METAG_TLS_GD
:
2209 tls_type
= GOT_TLS_GD
;
2215 hh
->eh
.got
.refcount
+= 1;
2216 old_tls_type
= hh
->tls_type
;
2220 bfd_signed_vma
*local_got_refcounts
;
2222 /* This is a global offset table entry for a local
2224 local_got_refcounts
= elf_local_got_refcounts (abfd
);
2225 if (local_got_refcounts
== NULL
)
2229 size
= symtab_hdr
->sh_info
;
2230 size
*= sizeof (bfd_signed_vma
);
2231 /* Add in space to store the local GOT TLS types. */
2232 size
+= symtab_hdr
->sh_info
;
2233 local_got_refcounts
= ((bfd_signed_vma
*)
2234 bfd_zalloc (abfd
, size
));
2235 if (local_got_refcounts
== NULL
)
2237 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
2238 memset (metag_elf_local_got_tls_type (abfd
),
2239 GOT_UNKNOWN
, symtab_hdr
->sh_info
);
2241 local_got_refcounts
[r_symndx
] += 1;
2242 old_tls_type
= metag_elf_local_got_tls_type (abfd
) [r_symndx
];
2245 if (old_tls_type
!= tls_type
)
2249 hh
->tls_type
= tls_type
;
2253 metag_elf_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
2259 case R_METAG_TLS_LDM
:
2260 metag_link_hash_table (info
)->tls_ldm_got
.refcount
+= 1;
2263 case R_METAG_RELBRANCH_PLT
:
2264 /* This symbol requires a procedure linkage table entry. We
2265 actually build the entry in adjust_dynamic_symbol,
2266 because this might be a case of linking PIC code without
2267 linking in any dynamic objects, in which case we don't
2268 need to generate a procedure linkage table after all. */
2270 /* If this is a local symbol, we resolve it directly without
2271 creating a procedure linkage table entry. */
2275 if (hh
->eh
.forced_local
)
2278 hh
->eh
.needs_plt
= 1;
2279 hh
->eh
.plt
.refcount
+= 1;
2282 case R_METAG_HIADDR16
:
2283 case R_METAG_LOADDR16
:
2284 /* Let's help debug shared library creation. These relocs
2285 cannot be used in shared libs. Don't error out for
2286 sections we don't care about, such as debug sections or
2287 non-constant sections. */
2288 if (bfd_link_pic (info
)
2289 && (sec
->flags
& SEC_ALLOC
) != 0
2290 && (sec
->flags
& SEC_READONLY
) != 0)
2295 name
= hh
->eh
.root
.root
.string
;
2297 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
, NULL
);
2299 /* xgettext:c-format */
2300 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2301 abfd
, elf_metag_howto_table
[r_type
].name
, name
);
2302 bfd_set_error (bfd_error_bad_value
);
2307 case R_METAG_ADDR32
:
2308 case R_METAG_RELBRANCH
:
2309 case R_METAG_GETSETOFF
:
2310 if (hh
!= NULL
&& !bfd_link_pic (info
))
2312 hh
->eh
.non_got_ref
= 1;
2313 hh
->eh
.plt
.refcount
+= 1;
2316 /* If we are creating a shared library, and this is a reloc
2317 against a global symbol, or a non PC relative reloc
2318 against a local symbol, then we need to copy the reloc
2319 into the shared library. However, if we are linking with
2320 -Bsymbolic, we do not need to copy a reloc against a
2321 global symbol which is defined in an object we are
2322 including in the link (i.e., DEF_REGULAR is set). At
2323 this point we have not seen all the input files, so it is
2324 possible that DEF_REGULAR is not set now but will be set
2325 later (it is never cleared). We account for that
2326 possibility below by storing information in the
2327 dyn_relocs field of the hash table entry. A similar
2328 situation occurs when creating shared libraries and symbol
2329 visibility changes render the symbol local.
2331 If on the other hand, we are creating an executable, we
2332 may need to keep relocations for symbols satisfied by a
2333 dynamic library if we manage to avoid copy relocs for the
2335 if ((bfd_link_pic (info
)
2336 && (sec
->flags
& SEC_ALLOC
) != 0
2337 && (r_type
!= R_METAG_RELBRANCH
2339 && (! info
->symbolic
2340 || hh
->eh
.root
.type
== bfd_link_hash_defweak
2341 || !hh
->eh
.def_regular
))))
2342 || (!bfd_link_pic (info
)
2343 && (sec
->flags
& SEC_ALLOC
) != 0
2345 && (hh
->eh
.root
.type
== bfd_link_hash_defweak
2346 || !hh
->eh
.def_regular
)))
2348 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2349 struct elf_metag_dyn_reloc_entry
**hdh_head
;
2352 htab
->etab
.dynobj
= dynobj
= abfd
;
2354 /* When creating a shared object, we must copy these
2355 relocs into the output file. We create a reloc
2356 section in dynobj and make room for the reloc. */
2359 sreloc
= _bfd_elf_make_dynamic_reloc_section
2360 (sec
, htab
->etab
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
2364 bfd_set_error (bfd_error_bad_value
);
2368 elf_section_data (sec
)->sreloc
= sreloc
;
2371 /* If this is a global symbol, we count the number of
2372 relocations we need for this symbol. */
2374 hdh_head
= &((struct elf_metag_link_hash_entry
*) hh
)->dyn_relocs
;
2377 /* Track dynamic relocs needed for local syms too. */
2381 sr
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2385 vpp
= &elf_section_data (sr
)->local_dynrel
;
2386 hdh_head
= (struct elf_metag_dyn_reloc_entry
**) vpp
;
2390 if (hdh_p
== NULL
|| hdh_p
->sec
!= sec
)
2392 hdh_p
= ((struct elf_metag_dyn_reloc_entry
*)
2393 bfd_alloc (dynobj
, sizeof *hdh_p
));
2396 hdh_p
->hdh_next
= *hdh_head
;
2400 hdh_p
->relative_count
= 0;
2404 if (ELF32_R_TYPE (rel
->r_info
) == R_METAG_RELBRANCH
)
2405 hdh_p
->relative_count
+= 1;
2409 /* This relocation describes the C++ object vtable hierarchy.
2410 Reconstruct it for later use during GC. */
2411 case R_METAG_GNU_VTINHERIT
:
2412 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, &hh
->eh
,
2417 /* This relocation describes which C++ vtable entries are actually
2418 used. Record for later use during GC. */
2419 case R_METAG_GNU_VTENTRY
:
2420 BFD_ASSERT (hh
!= NULL
);
2422 && !bfd_elf_gc_record_vtentry (abfd
, sec
, &hh
->eh
, rel
->r_addend
))
2431 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2434 elf_metag_copy_indirect_symbol (struct bfd_link_info
*info
,
2435 struct elf_link_hash_entry
*eh_dir
,
2436 struct elf_link_hash_entry
*eh_ind
)
2438 struct elf_metag_link_hash_entry
*hh_dir
, *hh_ind
;
2440 hh_dir
= metag_elf_hash_entry (eh_dir
);
2441 hh_ind
= metag_elf_hash_entry (eh_ind
);
2443 if (hh_ind
->dyn_relocs
!= NULL
)
2445 if (hh_dir
->dyn_relocs
!= NULL
)
2447 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
2448 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2450 if (eh_ind
->root
.type
== bfd_link_hash_indirect
)
2453 /* Add reloc counts against the weak sym to the strong sym
2454 list. Merge any entries against the same section. */
2455 for (hdh_pp
= &hh_ind
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
; )
2457 struct elf_metag_dyn_reloc_entry
*hdh_q
;
2459 for (hdh_q
= hh_dir
->dyn_relocs
; hdh_q
!= NULL
;
2460 hdh_q
= hdh_q
->hdh_next
)
2461 if (hdh_q
->sec
== hdh_p
->sec
)
2463 hdh_q
->relative_count
+= hdh_p
->relative_count
;
2464 hdh_q
->count
+= hdh_p
->count
;
2465 *hdh_pp
= hdh_p
->hdh_next
;
2469 hdh_pp
= &hdh_p
->hdh_next
;
2471 *hdh_pp
= hh_dir
->dyn_relocs
;
2474 hh_dir
->dyn_relocs
= hh_ind
->dyn_relocs
;
2475 hh_ind
->dyn_relocs
= NULL
;
2478 if (eh_ind
->root
.type
== bfd_link_hash_indirect
2479 && eh_dir
->got
.refcount
<= 0)
2481 hh_dir
->tls_type
= hh_ind
->tls_type
;
2482 hh_ind
->tls_type
= GOT_UNKNOWN
;
2485 _bfd_elf_link_hash_copy_indirect (info
, eh_dir
, eh_ind
);
2488 /* Adjust a symbol defined by a dynamic object and referenced by a
2489 regular object. The current definition is in some section of the
2490 dynamic object, but we're not including those sections. We have to
2491 change the definition to something the rest of the link can
2495 elf_metag_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2496 struct elf_link_hash_entry
*eh
)
2498 struct elf_metag_link_hash_table
*htab
;
2499 struct elf_metag_link_hash_entry
*hh
;
2500 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2503 /* If this is a function, put it in the procedure linkage table. We
2504 will fill in the contents of the procedure linkage table later,
2505 when we know the address of the .got section. */
2506 if (eh
->type
== STT_FUNC
2509 if (eh
->plt
.refcount
<= 0
2510 || SYMBOL_CALLS_LOCAL (info
, eh
)
2511 || (ELF_ST_VISIBILITY (eh
->other
) != STV_DEFAULT
2512 && eh
->root
.type
== bfd_link_hash_undefweak
))
2514 /* This case can occur if we saw a PLT reloc in an input
2515 file, but the symbol was never referred to by a dynamic
2516 object. In such a case, we don't actually need to build
2517 a procedure linkage table, and we can just do a PCREL
2519 eh
->plt
.offset
= (bfd_vma
) -1;
2526 eh
->plt
.offset
= (bfd_vma
) -1;
2528 /* If this is a weak symbol, and there is a real definition, the
2529 processor independent code will have arranged for us to see the
2530 real definition first, and we can just use the same value. */
2531 if (eh
->u
.weakdef
!= NULL
)
2533 if (eh
->u
.weakdef
->root
.type
!= bfd_link_hash_defined
2534 && eh
->u
.weakdef
->root
.type
!= bfd_link_hash_defweak
)
2536 eh
->root
.u
.def
.section
= eh
->u
.weakdef
->root
.u
.def
.section
;
2537 eh
->root
.u
.def
.value
= eh
->u
.weakdef
->root
.u
.def
.value
;
2538 eh
->non_got_ref
= eh
->u
.weakdef
->non_got_ref
;
2542 /* This is a reference to a symbol defined by a dynamic object which
2543 is not a function. */
2545 /* If we are creating a shared library, we must presume that the
2546 only references to the symbol are via the global offset table.
2547 For such cases we need not do anything here; the relocations will
2548 be handled correctly by relocate_section. */
2549 if (bfd_link_pic (info
))
2552 /* If there are no references to this symbol that do not use the
2553 GOT, we don't need to generate a copy reloc. */
2554 if (!eh
->non_got_ref
)
2557 /* If -z nocopyreloc was given, we won't generate them either. */
2558 if (info
->nocopyreloc
)
2560 eh
->non_got_ref
= 0;
2564 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2565 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2567 s
= hdh_p
->sec
->output_section
;
2568 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2572 /* If we didn't find any dynamic relocs in read-only sections, then
2573 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2576 eh
->non_got_ref
= 0;
2580 /* We must allocate the symbol in our .dynbss section, which will
2581 become part of the .bss section of the executable. There will be
2582 an entry for this symbol in the .dynsym section. The dynamic
2583 object will contain position independent code, so all references
2584 from the dynamic object to this symbol will go through the global
2585 offset table. The dynamic linker will use the .dynsym entry to
2586 determine the address it must put in the global offset table, so
2587 both the dynamic object and the regular object will refer to the
2588 same memory location for the variable. */
2590 htab
= metag_link_hash_table (info
);
2592 /* We must generate a COPY reloc to tell the dynamic linker to
2593 copy the initial value out of the dynamic object and into the
2594 runtime process image. */
2595 if ((eh
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && eh
->size
!= 0)
2597 htab
->srelbss
->size
+= sizeof (Elf32_External_Rela
);
2603 return _bfd_elf_adjust_dynamic_copy (info
, eh
, s
);
2606 /* Allocate space in .plt, .got and associated reloc sections for
2610 allocate_dynrelocs (struct elf_link_hash_entry
*eh
, void *inf
)
2612 struct bfd_link_info
*info
;
2613 struct elf_metag_link_hash_table
*htab
;
2614 struct elf_metag_link_hash_entry
*hh
;
2615 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2617 if (eh
->root
.type
== bfd_link_hash_indirect
)
2620 if (eh
->root
.type
== bfd_link_hash_warning
)
2621 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
2624 htab
= metag_link_hash_table (info
);
2626 if (htab
->etab
.dynamic_sections_created
2627 && eh
->plt
.refcount
> 0)
2629 /* Make sure this symbol is output as a dynamic symbol.
2630 Undefined weak syms won't yet be marked as dynamic. */
2631 if (eh
->dynindx
== -1
2632 && !eh
->forced_local
)
2634 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2638 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), eh
))
2640 asection
*s
= htab
->splt
;
2642 /* If this is the first .plt entry, make room for the special
2645 s
->size
+= PLT_ENTRY_SIZE
;
2647 eh
->plt
.offset
= s
->size
;
2649 /* If this symbol is not defined in a regular file, and we are
2650 not generating a shared library, then set the symbol to this
2651 location in the .plt. This is required to make function
2652 pointers compare as equal between the normal executable and
2653 the shared library. */
2654 if (! bfd_link_pic (info
)
2655 && !eh
->def_regular
)
2657 eh
->root
.u
.def
.section
= s
;
2658 eh
->root
.u
.def
.value
= eh
->plt
.offset
;
2661 /* Make room for this entry. */
2662 s
->size
+= PLT_ENTRY_SIZE
;
2664 /* We also need to make an entry in the .got.plt section, which
2665 will be placed in the .got section by the linker script. */
2666 htab
->sgotplt
->size
+= 4;
2668 /* We also need to make an entry in the .rel.plt section. */
2669 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
2673 eh
->plt
.offset
= (bfd_vma
) -1;
2679 eh
->plt
.offset
= (bfd_vma
) -1;
2683 if (eh
->got
.refcount
> 0)
2687 int tls_type
= metag_elf_hash_entry (eh
)->tls_type
;
2689 /* Make sure this symbol is output as a dynamic symbol.
2690 Undefined weak syms won't yet be marked as dynamic. */
2691 if (eh
->dynindx
== -1
2692 && !eh
->forced_local
)
2694 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2700 eh
->got
.offset
= s
->size
;
2702 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */
2703 if (tls_type
== GOT_TLS_GD
)
2705 dyn
= htab
->etab
.dynamic_sections_created
;
2706 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2707 R_METAG_TLS_GD needs one if local symbol and two if global. */
2708 if ((tls_type
== GOT_TLS_GD
&& eh
->dynindx
== -1)
2709 || (tls_type
== GOT_TLS_IE
&& dyn
))
2710 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2711 else if (tls_type
== GOT_TLS_GD
)
2712 htab
->srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2713 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
2714 bfd_link_pic (info
),
2716 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2719 eh
->got
.offset
= (bfd_vma
) -1;
2721 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2722 if (hh
->dyn_relocs
== NULL
)
2725 /* If this is a -Bsymbolic shared link, then we need to discard all
2726 space allocated for dynamic pc-relative relocs against symbols
2727 defined in a regular object. For the normal shared case, discard
2728 space for relocs that have become local due to symbol visibility
2730 if (bfd_link_pic (info
))
2732 if (SYMBOL_CALLS_LOCAL (info
, eh
))
2734 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
2736 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
; )
2738 hdh_p
->count
-= hdh_p
->relative_count
;
2739 hdh_p
->relative_count
= 0;
2740 if (hdh_p
->count
== 0)
2741 *hdh_pp
= hdh_p
->hdh_next
;
2743 hdh_pp
= &hdh_p
->hdh_next
;
2747 /* Also discard relocs on undefined weak syms with non-default
2749 if (hh
->dyn_relocs
!= NULL
2750 && eh
->root
.type
== bfd_link_hash_undefweak
)
2752 if (ELF_ST_VISIBILITY (eh
->other
) != STV_DEFAULT
)
2753 hh
->dyn_relocs
= NULL
;
2755 /* Make sure undefined weak symbols are output as a dynamic
2757 else if (eh
->dynindx
== -1
2758 && !eh
->forced_local
)
2760 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2767 /* For the non-shared case, discard space for relocs against
2768 symbols which turn out to need copy relocs or are not
2770 if (!eh
->non_got_ref
2771 && ((eh
->def_dynamic
2772 && !eh
->def_regular
)
2773 || (htab
->etab
.dynamic_sections_created
2774 && (eh
->root
.type
== bfd_link_hash_undefweak
2775 || eh
->root
.type
== bfd_link_hash_undefined
))))
2777 /* Make sure this symbol is output as a dynamic symbol.
2778 Undefined weak syms won't yet be marked as dynamic. */
2779 if (eh
->dynindx
== -1
2780 && !eh
->forced_local
)
2782 if (! bfd_elf_link_record_dynamic_symbol (info
, eh
))
2786 /* If that succeeded, we know we'll be keeping all the
2788 if (eh
->dynindx
!= -1)
2792 hh
->dyn_relocs
= NULL
;
2798 /* Finally, allocate space. */
2799 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2801 asection
*sreloc
= elf_section_data (hdh_p
->sec
)->sreloc
;
2802 sreloc
->size
+= hdh_p
->count
* sizeof (Elf32_External_Rela
);
2808 /* Find any dynamic relocs that apply to read-only sections. */
2811 readonly_dynrelocs (struct elf_link_hash_entry
*eh
, void *inf
)
2813 struct elf_metag_link_hash_entry
*hh
;
2814 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2816 if (eh
->root
.type
== bfd_link_hash_warning
)
2817 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
2819 hh
= (struct elf_metag_link_hash_entry
*) eh
;
2820 for (hdh_p
= hh
->dyn_relocs
; hdh_p
!= NULL
; hdh_p
= hdh_p
->hdh_next
)
2822 asection
*s
= hdh_p
->sec
->output_section
;
2824 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2826 struct bfd_link_info
*info
= inf
;
2828 info
->flags
|= DF_TEXTREL
;
2830 /* Not an error, just cut short the traversal. */
2837 /* Set the sizes of the dynamic sections. */
2840 elf_metag_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2841 struct bfd_link_info
*info
)
2843 struct elf_metag_link_hash_table
*htab
;
2849 htab
= metag_link_hash_table (info
);
2850 dynobj
= htab
->etab
.dynobj
;
2854 if (htab
->etab
.dynamic_sections_created
)
2856 /* Set the contents of the .interp section to the interpreter. */
2857 if (bfd_link_executable (info
) && !info
->nointerp
)
2859 s
= bfd_get_linker_section (dynobj
, ".interp");
2862 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2863 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2867 /* Set up .got offsets for local syms, and space for local dynamic
2869 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
2871 bfd_signed_vma
*local_got
;
2872 bfd_signed_vma
*end_local_got
;
2873 bfd_size_type locsymcount
;
2874 Elf_Internal_Shdr
*symtab_hdr
;
2876 char *local_tls_type
;
2878 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
2881 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2883 struct elf_metag_dyn_reloc_entry
*hdh_p
;
2885 for (hdh_p
= ((struct elf_metag_dyn_reloc_entry
*)
2886 elf_section_data (s
)->local_dynrel
);
2888 hdh_p
= hdh_p
->hdh_next
)
2890 if (!bfd_is_abs_section (hdh_p
->sec
)
2891 && bfd_is_abs_section (hdh_p
->sec
->output_section
))
2893 /* Input section has been discarded, either because
2894 it is a copy of a linkonce section or due to
2895 linker script /DISCARD/, so we'll be discarding
2898 else if (hdh_p
->count
!= 0)
2900 srel
= elf_section_data (hdh_p
->sec
)->sreloc
;
2901 srel
->size
+= hdh_p
->count
* sizeof (Elf32_External_Rela
);
2902 if ((hdh_p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2903 info
->flags
|= DF_TEXTREL
;
2908 local_got
= elf_local_got_refcounts (ibfd
);
2912 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2913 locsymcount
= symtab_hdr
->sh_info
;
2914 end_local_got
= local_got
+ locsymcount
;
2915 local_tls_type
= metag_elf_local_got_tls_type (ibfd
);
2917 srel
= htab
->srelgot
;
2918 for (; local_got
< end_local_got
; ++local_got
)
2922 *local_got
= s
->size
;
2923 s
->size
+= GOT_ENTRY_SIZE
;
2924 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */
2925 if (*local_tls_type
== GOT_TLS_GD
)
2927 if (bfd_link_pic (info
))
2928 srel
->size
+= sizeof (Elf32_External_Rela
);
2931 *local_got
= (bfd_vma
) -1;
2936 if (htab
->tls_ldm_got
.refcount
> 0)
2938 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2940 htab
->tls_ldm_got
.offset
= htab
->sgot
->size
;
2941 htab
->sgot
->size
+= 8;
2942 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2945 htab
->tls_ldm_got
.offset
= -1;
2947 /* Allocate global sym .plt and .got entries, and space for global
2948 sym dynamic relocs. */
2949 elf_link_hash_traverse (&htab
->etab
, allocate_dynrelocs
, info
);
2951 /* We now have determined the sizes of the various dynamic sections.
2952 Allocate memory for them. */
2954 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2956 bfd_boolean reloc_section
= FALSE
;
2958 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2963 || s
== htab
->sgotplt
2964 || s
== htab
->sdynbss
)
2966 /* Strip this section if we don't need it; see the
2969 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
2971 if (s
->size
!= 0 && s
!= htab
->srelplt
)
2974 /* We use the reloc_count field as a counter if we need
2975 to copy relocs into the output file. */
2977 reloc_section
= TRUE
;
2981 /* It's not one of our sections, so don't allocate space. */
2987 /* If we don't need this section, strip it from the
2988 output file. This is mostly to handle .rela.bss and
2989 .rela.plt. We must create both sections in
2990 create_dynamic_sections, because they must be created
2991 before the linker maps input sections to output
2992 sections. The linker does that before
2993 adjust_dynamic_symbol is called, and it is that
2994 function which decides whether anything needs to go
2995 into these sections. */
2996 s
->flags
|= SEC_EXCLUDE
;
3000 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3003 /* Allocate memory for the section contents. */
3004 s
->contents
= bfd_zalloc (dynobj
, s
->size
);
3005 if (s
->contents
== NULL
)
3007 else if (reloc_section
)
3009 unsigned char *contents
= s
->contents
;
3010 Elf32_External_Rela reloc
;
3012 /* Fill the reloc section with a R_METAG_NONE type reloc. */
3013 memset(&reloc
, 0, sizeof(Elf32_External_Rela
));
3014 reloc
.r_info
[0] = R_METAG_NONE
;
3015 for (; contents
< (s
->contents
+ s
->size
);
3016 contents
+= sizeof(Elf32_External_Rela
))
3018 memcpy(contents
, &reloc
, sizeof(Elf32_External_Rela
));
3023 if (htab
->etab
.dynamic_sections_created
)
3025 /* Add some entries to the .dynamic section. We fill in the
3026 values later, in elf_metag_finish_dynamic_sections, but we
3027 must add the entries now so that we get the correct size for
3028 the .dynamic section. The DT_DEBUG entry is filled in by the
3029 dynamic linker and used by the debugger. */
3030 #define add_dynamic_entry(TAG, VAL) \
3031 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3033 if (!add_dynamic_entry (DT_PLTGOT
, 0))
3036 if (bfd_link_executable (info
))
3038 if (!add_dynamic_entry (DT_DEBUG
, 0))
3042 if (htab
->srelplt
->size
!= 0)
3044 if (!add_dynamic_entry (DT_PLTRELSZ
, 0)
3045 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3046 || !add_dynamic_entry (DT_JMPREL
, 0))
3052 if (!add_dynamic_entry (DT_RELA
, 0)
3053 || !add_dynamic_entry (DT_RELASZ
, 0)
3054 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf32_External_Rela
)))
3057 /* If any dynamic relocs apply to a read-only section,
3058 then we need a DT_TEXTREL entry. */
3059 if ((info
->flags
& DF_TEXTREL
) == 0)
3060 elf_link_hash_traverse (&htab
->etab
, readonly_dynrelocs
, info
);
3062 if ((info
->flags
& DF_TEXTREL
) != 0)
3064 if (!add_dynamic_entry (DT_TEXTREL
, 0))
3069 #undef add_dynamic_entry
3074 /* Finish up dynamic symbol handling. We set the contents of various
3075 dynamic sections here. */
3078 elf_metag_finish_dynamic_symbol (bfd
*output_bfd
,
3079 struct bfd_link_info
*info
,
3080 struct elf_link_hash_entry
*eh
,
3081 Elf_Internal_Sym
*sym
)
3083 struct elf_metag_link_hash_table
*htab
;
3084 Elf_Internal_Rela rel
;
3087 htab
= metag_link_hash_table (info
);
3089 if (eh
->plt
.offset
!= (bfd_vma
) -1)
3099 if (eh
->plt
.offset
& 1)
3102 BFD_ASSERT (eh
->dynindx
!= -1);
3105 sgot
= htab
->sgotplt
;
3106 srela
= htab
->srelplt
;
3107 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srela
!= NULL
);
3109 /* Get the index in the procedure linkage table which
3110 corresponds to this symbol. This is the index of this symbol
3111 in all the symbols for which we are making plt entries. The
3112 first entry in the procedure linkage table is reserved. */
3113 plt_index
= eh
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3115 /* Get the offset into the .got.plt table of the entry that
3116 corresponds to this function. */
3117 got_offset
= plt_index
* GOT_ENTRY_SIZE
;
3119 BFD_ASSERT (got_offset
< (1 << 16));
3121 got_entry
= sgot
->output_section
->vma
3122 + sgot
->output_offset
3125 BFD_ASSERT (plt_index
< (1 << 16));
3127 /* Fill in the entry in the procedure linkage table. */
3128 if (! bfd_link_pic (info
))
3130 bfd_put_32 (output_bfd
,
3132 | (((got_entry
>> 16) & 0xffff) << 3)),
3133 splt
->contents
+ eh
->plt
.offset
);
3134 bfd_put_32 (output_bfd
,
3136 | ((got_entry
& 0xffff) << 3)),
3137 splt
->contents
+ eh
->plt
.offset
+ 4);
3138 bfd_put_32 (output_bfd
, plt_entry
[2],
3139 splt
->contents
+ eh
->plt
.offset
+ 8);
3140 bfd_put_32 (output_bfd
,
3141 (plt_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);
3150 bfd_vma addr
= got_entry
- (splt
->output_section
->vma
+
3151 splt
->output_offset
+ eh
->plt
.offset
);
3153 bfd_put_32 (output_bfd
,
3154 plt_pic_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3155 splt
->contents
+ eh
->plt
.offset
);
3156 bfd_put_32 (output_bfd
,
3157 plt_pic_entry
[1] | ((addr
& 0xffff) << 3),
3158 splt
->contents
+ eh
->plt
.offset
+ 4);
3159 bfd_put_32 (output_bfd
, plt_pic_entry
[2],
3160 splt
->contents
+ eh
->plt
.offset
+ 8);
3161 bfd_put_32 (output_bfd
,
3162 (plt_pic_entry
[3] | (plt_index
<< 3)),
3163 splt
->contents
+ eh
->plt
.offset
+ 12);
3164 bfd_put_32 (output_bfd
,
3166 + ((((unsigned int) ((- (eh
->plt
.offset
+ 16)) >> 2)) & 0x7ffff) << 5)),
3167 splt
->contents
+ eh
->plt
.offset
+ 16);
3170 /* Fill in the entry in the global offset table. */
3171 bfd_put_32 (output_bfd
,
3172 (splt
->output_section
->vma
3173 + splt
->output_offset
3175 + 12), /* offset within PLT entry */
3176 sgot
->contents
+ got_offset
);
3178 /* Fill in the entry in the .rela.plt section. */
3179 rel
.r_offset
= (sgot
->output_section
->vma
3180 + sgot
->output_offset
3182 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_JMP_SLOT
);
3184 loc
= htab
->srelplt
->contents
;
3185 loc
+= plt_index
* sizeof(Elf32_External_Rela
);
3186 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3188 if (!eh
->def_regular
)
3190 /* Mark the symbol as undefined, rather than as defined in
3191 the .plt section. Leave the value alone. */
3192 sym
->st_shndx
= SHN_UNDEF
;
3196 if (eh
->got
.offset
!= (bfd_vma
) -1
3197 && (metag_elf_hash_entry (eh
)->tls_type
& GOT_TLS_GD
) == 0
3198 && (metag_elf_hash_entry (eh
)->tls_type
& GOT_TLS_IE
) == 0)
3200 /* This symbol has an entry in the global offset table. Set it
3203 rel
.r_offset
= ((eh
->got
.offset
&~ (bfd_vma
) 1)
3204 + htab
->sgot
->output_offset
3205 + htab
->sgot
->output_section
->vma
);
3207 /* If this is a -Bsymbolic link and the symbol is defined
3208 locally or was forced to be local because of a version file,
3209 we just want to emit a RELATIVE reloc. The entry in the
3210 global offset table will already have been initialized in the
3211 relocate_section function. */
3212 if (bfd_link_pic (info
)
3213 && (info
->symbolic
|| eh
->dynindx
== -1)
3216 rel
.r_info
= ELF32_R_INFO (0, R_METAG_RELATIVE
);
3217 rel
.r_addend
= (eh
->root
.u
.def
.value
3218 + eh
->root
.u
.def
.section
->output_offset
3219 + eh
->root
.u
.def
.section
->output_section
->vma
);
3223 if ((eh
->got
.offset
& 1) != 0)
3225 bfd_put_32 (output_bfd
, 0, htab
->sgot
->contents
+ eh
->got
.offset
);
3226 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_GLOB_DAT
);
3230 loc
= htab
->srelgot
->contents
;
3231 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3232 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3239 /* This symbol needs a copy reloc. Set it up. */
3241 if (! (eh
->dynindx
!= -1
3242 && (eh
->root
.type
== bfd_link_hash_defined
3243 || eh
->root
.type
== bfd_link_hash_defweak
)))
3248 rel
.r_offset
= (eh
->root
.u
.def
.value
3249 + eh
->root
.u
.def
.section
->output_offset
3250 + eh
->root
.u
.def
.section
->output_section
->vma
);
3252 rel
.r_info
= ELF32_R_INFO (eh
->dynindx
, R_METAG_COPY
);
3253 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3254 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
3257 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3258 if (eh
->root
.root
.string
[0] == '_'
3259 && (strcmp (eh
->root
.root
.string
, "_DYNAMIC") == 0
3260 || eh
== htab
->etab
.hgot
))
3262 sym
->st_shndx
= SHN_ABS
;
3268 /* Set the Meta ELF ABI version. */
3271 elf_metag_post_process_headers (bfd
* abfd
, struct bfd_link_info
* link_info
)
3273 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
3275 _bfd_elf_post_process_headers (abfd
, link_info
);
3276 i_ehdrp
= elf_elfheader (abfd
);
3277 i_ehdrp
->e_ident
[EI_ABIVERSION
] = METAG_ELF_ABI_VERSION
;
3280 /* Used to decide how to sort relocs in an optimal manner for the
3281 dynamic linker, before writing them out. */
3283 static enum elf_reloc_type_class
3284 elf_metag_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3285 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3286 const Elf_Internal_Rela
*rela
)
3288 switch ((int) ELF32_R_TYPE (rela
->r_info
))
3290 case R_METAG_RELATIVE
:
3291 return reloc_class_relative
;
3292 case R_METAG_JMP_SLOT
:
3293 return reloc_class_plt
;
3295 return reloc_class_copy
;
3297 return reloc_class_normal
;
3301 /* Finish up the dynamic sections. */
3304 elf_metag_finish_dynamic_sections (bfd
*output_bfd
,
3305 struct bfd_link_info
*info
)
3308 struct elf_metag_link_hash_table
*htab
;
3311 htab
= metag_link_hash_table (info
);
3312 dynobj
= htab
->etab
.dynobj
;
3314 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3316 if (htab
->etab
.dynamic_sections_created
)
3319 Elf32_External_Dyn
*dyncon
, *dynconend
;
3324 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
3325 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3326 for (; dyncon
< dynconend
; dyncon
++)
3328 Elf_Internal_Dyn dyn
;
3331 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3340 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3341 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3346 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3347 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3352 dyn
.d_un
.d_val
= s
->size
;
3353 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3357 /* Don't count procedure linkage table relocs in the
3358 overall reloc count. */
3359 if (htab
->srelplt
) {
3361 dyn
.d_un
.d_val
-= s
->size
;
3363 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3367 /* We may not be using the standard ELF linker script.
3368 If .rela.plt is the first .rela section, we adjust
3369 DT_RELA to not include it. */
3370 if (htab
->srelplt
) {
3372 if (dyn
.d_un
.d_ptr
== s
->output_section
->vma
+ s
->output_offset
)
3373 dyn
.d_un
.d_ptr
+= s
->size
;
3375 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3381 /* Fill in the first entry in the procedure linkage table. */
3383 if (splt
&& splt
->size
> 0)
3386 /* addr = .got + 4 */
3387 addr
= htab
->sgot
->output_section
->vma
+
3388 htab
->sgot
->output_offset
+ 4;
3389 if (bfd_link_pic (info
))
3391 addr
-= splt
->output_section
->vma
+ splt
->output_offset
;
3392 bfd_put_32 (output_bfd
,
3393 plt0_pic_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3395 bfd_put_32 (output_bfd
,
3396 plt0_pic_entry
[1] | ((addr
& 0xffff) << 3),
3397 splt
->contents
+ 4);
3398 bfd_put_32 (output_bfd
, plt0_pic_entry
[2], splt
->contents
+ 8);
3399 bfd_put_32 (output_bfd
, plt0_pic_entry
[3], splt
->contents
+ 12);
3400 bfd_put_32 (output_bfd
, plt0_pic_entry
[4], splt
->contents
+ 16);
3404 bfd_put_32 (output_bfd
,
3405 plt0_entry
[0] | (((addr
>> 16) & 0xffff) << 3),
3407 bfd_put_32 (output_bfd
,
3408 plt0_entry
[1] | ((addr
& 0xffff) << 3),
3409 splt
->contents
+ 4);
3410 bfd_put_32 (output_bfd
, plt0_entry
[2], splt
->contents
+ 8);
3411 bfd_put_32 (output_bfd
, plt0_entry
[3], splt
->contents
+ 12);
3412 bfd_put_32 (output_bfd
, plt0_entry
[4], splt
->contents
+ 16);
3415 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
=
3420 if (htab
->sgot
!= NULL
&& htab
->sgot
->size
!= 0)
3422 /* Fill in the first entry in the global offset table.
3423 We use it to point to our dynamic section, if we have one. */
3424 bfd_put_32 (output_bfd
,
3425 sdyn
? sdyn
->output_section
->vma
+ sdyn
->output_offset
: 0,
3426 htab
->sgot
->contents
);
3428 /* The second entry is reserved for use by the dynamic linker. */
3429 memset (htab
->sgot
->contents
+ GOT_ENTRY_SIZE
, 0, GOT_ENTRY_SIZE
);
3431 /* Set .got entry size. */
3432 elf_section_data (htab
->sgot
->output_section
)
3433 ->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3439 /* Return the section that should be marked against GC for a given
3443 elf_metag_gc_mark_hook (asection
*sec
,
3444 struct bfd_link_info
*info
,
3445 Elf_Internal_Rela
*rela
,
3446 struct elf_link_hash_entry
*hh
,
3447 Elf_Internal_Sym
*sym
)
3450 switch ((unsigned int) ELF32_R_TYPE (rela
->r_info
))
3452 case R_METAG_GNU_VTINHERIT
:
3453 case R_METAG_GNU_VTENTRY
:
3457 return _bfd_elf_gc_mark_hook (sec
, info
, rela
, hh
, sym
);
3460 /* Update the got and plt entry reference counts for the section being
3464 elf_metag_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
3465 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3466 asection
*sec ATTRIBUTE_UNUSED
,
3467 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
3469 Elf_Internal_Shdr
*symtab_hdr
;
3470 struct elf_link_hash_entry
**eh_syms
;
3471 bfd_signed_vma
*local_got_refcounts
;
3472 bfd_signed_vma
*local_plt_refcounts
;
3473 const Elf_Internal_Rela
*rel
, *relend
;
3475 if (bfd_link_relocatable (info
))
3478 elf_section_data (sec
)->local_dynrel
= NULL
;
3480 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3481 eh_syms
= elf_sym_hashes (abfd
);
3482 local_got_refcounts
= elf_local_got_refcounts (abfd
);
3483 local_plt_refcounts
= local_got_refcounts
;
3484 if (local_plt_refcounts
!= NULL
)
3485 local_plt_refcounts
+= symtab_hdr
->sh_info
;
3487 relend
= relocs
+ sec
->reloc_count
;
3488 for (rel
= relocs
; rel
< relend
; rel
++)
3490 unsigned long r_symndx
;
3491 unsigned int r_type
;
3492 struct elf_link_hash_entry
*eh
= NULL
;
3494 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3495 if (r_symndx
>= symtab_hdr
->sh_info
)
3497 struct elf_metag_link_hash_entry
*hh
;
3498 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
3499 struct elf_metag_dyn_reloc_entry
*hdh_p
;
3501 eh
= eh_syms
[r_symndx
- symtab_hdr
->sh_info
];
3502 while (eh
->root
.type
== bfd_link_hash_indirect
3503 || eh
->root
.type
== bfd_link_hash_warning
)
3504 eh
= (struct elf_link_hash_entry
*) eh
->root
.u
.i
.link
;
3505 hh
= (struct elf_metag_link_hash_entry
*) eh
;
3507 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
;
3508 hdh_pp
= &hdh_p
->hdh_next
)
3509 if (hdh_p
->sec
== sec
)
3511 /* Everything must go for SEC. */
3512 *hdh_pp
= hdh_p
->hdh_next
;
3517 r_type
= ELF32_R_TYPE (rel
->r_info
);
3520 case R_METAG_TLS_LDM
:
3521 if (metag_link_hash_table (info
)->tls_ldm_got
.refcount
> 0)
3522 metag_link_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
3524 case R_METAG_TLS_IE
:
3525 case R_METAG_TLS_GD
:
3526 case R_METAG_GETSET_GOT
:
3529 if (eh
->got
.refcount
> 0)
3530 eh
->got
.refcount
-= 1;
3532 else if (local_got_refcounts
!= NULL
)
3534 if (local_got_refcounts
[r_symndx
] > 0)
3535 local_got_refcounts
[r_symndx
] -= 1;
3539 case R_METAG_RELBRANCH_PLT
:
3542 if (eh
->plt
.refcount
> 0)
3543 eh
->plt
.refcount
-= 1;
3547 case R_METAG_ADDR32
:
3548 case R_METAG_HIADDR16
:
3549 case R_METAG_LOADDR16
:
3550 case R_METAG_GETSETOFF
:
3551 case R_METAG_RELBRANCH
:
3554 struct elf_metag_link_hash_entry
*hh
;
3555 struct elf_metag_dyn_reloc_entry
**hdh_pp
;
3556 struct elf_metag_dyn_reloc_entry
*hdh_p
;
3558 if (!bfd_link_pic (info
) && eh
->plt
.refcount
> 0)
3559 eh
->plt
.refcount
-= 1;
3561 hh
= (struct elf_metag_link_hash_entry
*) eh
;
3563 for (hdh_pp
= &hh
->dyn_relocs
; (hdh_p
= *hdh_pp
) != NULL
;
3564 hdh_pp
= &hdh_p
->hdh_next
)
3565 if (hdh_p
->sec
== sec
)
3567 if (ELF32_R_TYPE (rel
->r_info
) == R_METAG_RELBRANCH
)
3568 hdh_p
->relative_count
-= 1;
3570 if (hdh_p
->count
== 0)
3571 *hdh_pp
= hdh_p
->hdh_next
;
3585 /* Determine the type of stub needed, if any, for a call. */
3587 static enum elf_metag_stub_type
3588 metag_type_of_stub (asection
*input_sec
,
3589 const Elf_Internal_Rela
*rel
,
3590 struct elf_metag_link_hash_entry
*hh
,
3591 bfd_vma destination
,
3592 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3595 bfd_vma branch_offset
;
3596 bfd_vma max_branch_offset
;
3599 !(hh
->eh
.root
.type
== bfd_link_hash_defined
3600 || hh
->eh
.root
.type
== bfd_link_hash_defweak
))
3601 return metag_stub_none
;
3603 /* Determine where the call point is. */
3604 location
= (input_sec
->output_offset
3605 + input_sec
->output_section
->vma
3608 branch_offset
= destination
- location
;
3610 /* Determine if a long branch stub is needed. Meta branch offsets
3611 are signed 19 bits 4 byte aligned. */
3612 max_branch_offset
= (1 << (BRANCH_BITS
-1)) << 2;
3614 if (branch_offset
+ max_branch_offset
>= 2*max_branch_offset
)
3616 if (bfd_link_pic (info
))
3617 return metag_stub_long_branch_shared
;
3619 return metag_stub_long_branch
;
3622 return metag_stub_none
;
3625 #define MOVT_A0_3 0x82180005
3626 #define JUMP_A0_3 0xac180003
3628 #define MOVT_A1LBP 0x83080005
3629 #define ADD_A1LBP 0x83080000
3631 #define ADDT_A0_3_CPC 0x82980001
3632 #define ADD_A0_3_A0_3 0x82180000
3633 #define MOV_PC_A0_3 0xa3180ca0
3636 metag_build_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
3638 struct elf_metag_stub_hash_entry
*hsh
;
3645 /* Massage our args to the form they really have. */
3646 hsh
= (struct elf_metag_stub_hash_entry
*) gen_entry
;
3648 stub_sec
= hsh
->stub_sec
;
3650 /* Make a note of the offset within the stubs for this entry. */
3651 hsh
->stub_offset
= stub_sec
->size
;
3652 loc
= stub_sec
->contents
+ hsh
->stub_offset
;
3654 stub_bfd
= stub_sec
->owner
;
3656 switch (hsh
->stub_type
)
3658 case metag_stub_long_branch_shared
:
3659 /* A PIC long branch stub is an ADDT and an ADD instruction used to
3660 calculate the jump target using A0.3 as a temporary. Then a MOV
3661 to PC carries out the jump. */
3662 sym_value
= (hsh
->target_value
3663 + hsh
->target_section
->output_offset
3664 + hsh
->target_section
->output_section
->vma
3667 sym_value
-= (hsh
->stub_offset
3668 + stub_sec
->output_offset
3669 + stub_sec
->output_section
->vma
);
3671 bfd_put_32 (stub_bfd
, ADDT_A0_3_CPC
| (((sym_value
>> 16) & 0xffff) << 3),
3674 bfd_put_32 (stub_bfd
, ADD_A0_3_A0_3
| ((sym_value
& 0xffff) << 3),
3677 bfd_put_32 (stub_bfd
, MOV_PC_A0_3
, loc
+ 8);
3681 case metag_stub_long_branch
:
3682 /* A standard long branch stub is a MOVT instruction followed by a
3683 JUMP instruction using the A0.3 register as a temporary. This is
3684 the same method used by the LDLK linker (patch.c). */
3685 sym_value
= (hsh
->target_value
3686 + hsh
->target_section
->output_offset
3687 + hsh
->target_section
->output_section
->vma
3690 bfd_put_32 (stub_bfd
, MOVT_A0_3
| (((sym_value
>> 16) & 0xffff) << 3),
3693 bfd_put_32 (stub_bfd
, JUMP_A0_3
| ((sym_value
& 0xffff) << 3), loc
+ 4);
3702 stub_sec
->size
+= size
;
3706 /* As above, but don't actually build the stub. Just bump offset so
3707 we know stub section sizes. */
3710 metag_size_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg ATTRIBUTE_UNUSED
)
3712 struct elf_metag_stub_hash_entry
*hsh
;
3715 /* Massage our args to the form they really have. */
3716 hsh
= (struct elf_metag_stub_hash_entry
*) gen_entry
;
3718 if (hsh
->stub_type
== metag_stub_long_branch
)
3720 else if (hsh
->stub_type
== metag_stub_long_branch_shared
)
3723 hsh
->stub_sec
->size
+= size
;
3727 /* Set up various things so that we can make a list of input sections
3728 for each output section included in the link. Returns -1 on error,
3729 0 when no stubs will be needed, and 1 on success. */
3732 elf_metag_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
3735 unsigned int bfd_count
;
3736 unsigned int top_id
, top_index
;
3738 asection
**input_list
, **list
;
3740 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3742 /* Count the number of input BFDs and find the top input section id. */
3743 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
3745 input_bfd
= input_bfd
->link
.next
)
3748 for (section
= input_bfd
->sections
;
3750 section
= section
->next
)
3752 if (top_id
< section
->id
)
3753 top_id
= section
->id
;
3757 htab
->bfd_count
= bfd_count
;
3759 amt
= sizeof (struct map_stub
) * (top_id
+ 1);
3760 htab
->stub_group
= bfd_zmalloc (amt
);
3761 if (htab
->stub_group
== NULL
)
3764 /* We can't use output_bfd->section_count here to find the top output
3765 section index as some sections may have been removed, and
3766 strip_excluded_output_sections doesn't renumber the indices. */
3767 for (section
= output_bfd
->sections
, top_index
= 0;
3769 section
= section
->next
)
3771 if (top_index
< section
->index
)
3772 top_index
= section
->index
;
3775 htab
->top_index
= top_index
;
3776 amt
= sizeof (asection
*) * (top_index
+ 1);
3777 input_list
= bfd_malloc (amt
);
3778 htab
->input_list
= input_list
;
3779 if (input_list
== NULL
)
3782 /* For sections we aren't interested in, mark their entries with a
3783 value we can check later. */
3784 list
= input_list
+ top_index
;
3786 *list
= bfd_abs_section_ptr
;
3787 while (list
-- != input_list
);
3789 for (section
= output_bfd
->sections
;
3791 section
= section
->next
)
3793 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3794 * have PC relative relocs in them but no code flag set. */
3795 if (((section
->flags
& SEC_CODE
) != 0) ||
3796 strcmp(".ctors", section
->name
) ||
3797 strcmp(".dtors", section
->name
))
3798 input_list
[section
->index
] = NULL
;
3804 /* The linker repeatedly calls this function for each input section,
3805 in the order that input sections are linked into output sections.
3806 Build lists of input sections to determine groupings between which
3807 we may insert linker stubs. */
3810 elf_metag_next_input_section (struct bfd_link_info
*info
, asection
*isec
)
3812 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3814 if (isec
->output_section
->index
<= htab
->top_index
)
3816 asection
**list
= htab
->input_list
+ isec
->output_section
->index
;
3817 if (*list
!= bfd_abs_section_ptr
)
3819 /* Steal the link_sec pointer for our list. */
3820 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3821 /* This happens to make the list in reverse order,
3822 which is what we want. */
3823 PREV_SEC (isec
) = *list
;
3829 /* See whether we can group stub sections together. Grouping stub
3830 sections may result in fewer stubs. More importantly, we need to
3831 put all .init* and .fini* stubs at the beginning of the .init or
3832 .fini output sections respectively, because glibc splits the
3833 _init and _fini functions into multiple parts. Putting a stub in
3834 the middle of a function is not a good idea. */
3837 group_sections (struct elf_metag_link_hash_table
*htab
,
3838 bfd_size_type stub_group_size
,
3839 bfd_boolean stubs_always_before_branch
)
3841 asection
**list
= htab
->input_list
+ htab
->top_index
;
3844 asection
*tail
= *list
;
3845 if (tail
== bfd_abs_section_ptr
)
3847 while (tail
!= NULL
)
3851 bfd_size_type total
;
3852 bfd_boolean big_sec
;
3856 big_sec
= total
>= stub_group_size
;
3858 while ((prev
= PREV_SEC (curr
)) != NULL
3859 && ((total
+= curr
->output_offset
- prev
->output_offset
)
3863 /* OK, the size from the start of CURR to the end is less
3864 than stub_group_size bytes and thus can be handled by one stub
3865 section. (or the tail section is itself larger than
3866 stub_group_size bytes, in which case we may be toast.)
3867 We should really be keeping track of the total size of
3868 stubs added here, as stubs contribute to the final output
3872 prev
= PREV_SEC (tail
);
3873 /* Set up this stub group. */
3874 htab
->stub_group
[tail
->id
].link_sec
= curr
;
3876 while (tail
!= curr
&& (tail
= prev
) != NULL
);
3878 /* But wait, there's more! Input sections up to stub_group_size
3879 bytes before the stub section can be handled by it too.
3880 Don't do this if we have a really large section after the
3881 stubs, as adding more stubs increases the chance that
3882 branches may not reach into the stub section. */
3883 if (!stubs_always_before_branch
&& !big_sec
)
3887 && ((total
+= tail
->output_offset
- prev
->output_offset
)
3891 prev
= PREV_SEC (tail
);
3892 htab
->stub_group
[tail
->id
].link_sec
= curr
;
3898 while (list
-- != htab
->input_list
);
3899 free (htab
->input_list
);
3903 /* Read in all local syms for all input bfds.
3904 Returns -1 on error, 0 otherwise. */
3907 get_local_syms (bfd
*output_bfd ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
3908 struct bfd_link_info
*info
)
3910 unsigned int bfd_indx
;
3911 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
3912 int stub_changed
= 0;
3913 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3915 /* We want to read in symbol extension records only once. To do this
3916 we need to read in the local symbols in parallel and save them for
3917 later use; so hold pointers to the local symbols in an array. */
3918 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
3919 all_local_syms
= bfd_zmalloc (amt
);
3920 htab
->all_local_syms
= all_local_syms
;
3921 if (all_local_syms
== NULL
)
3924 /* Walk over all the input BFDs, swapping in local symbols. */
3927 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3929 Elf_Internal_Shdr
*symtab_hdr
;
3931 /* We'll need the symbol table in a second. */
3932 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3933 if (symtab_hdr
->sh_info
== 0)
3936 /* We need an array of the local symbols attached to the input bfd. */
3937 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3938 if (local_syms
== NULL
)
3940 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3941 symtab_hdr
->sh_info
, 0,
3943 /* Cache them for elf_link_input_bfd. */
3944 symtab_hdr
->contents
= (unsigned char *) local_syms
;
3946 if (local_syms
== NULL
)
3949 all_local_syms
[bfd_indx
] = local_syms
;
3952 return stub_changed
;
3955 /* Determine and set the size of the stub section for a final link.
3957 The basic idea here is to examine all the relocations looking for
3958 PC-relative calls to a target that is unreachable with a "CALLR"
3961 /* See elf32-hppa.c and elf64-ppc.c. */
3964 elf_metag_size_stubs(bfd
*output_bfd
, bfd
*stub_bfd
,
3965 struct bfd_link_info
*info
,
3966 bfd_signed_vma group_size
,
3967 asection
* (*add_stub_section
) (const char *, asection
*),
3968 void (*layout_sections_again
) (void))
3970 bfd_size_type stub_group_size
;
3971 bfd_boolean stubs_always_before_branch
;
3972 bfd_boolean stub_changed
;
3973 struct elf_metag_link_hash_table
*htab
= metag_link_hash_table (info
);
3975 /* Stash our params away. */
3976 htab
->stub_bfd
= stub_bfd
;
3977 htab
->add_stub_section
= add_stub_section
;
3978 htab
->layout_sections_again
= layout_sections_again
;
3979 stubs_always_before_branch
= group_size
< 0;
3981 stub_group_size
= -group_size
;
3983 stub_group_size
= group_size
;
3984 if (stub_group_size
== 1)
3986 /* Default values. */
3987 /* FIXME: not sure what these values should be */
3988 if (stubs_always_before_branch
)
3990 stub_group_size
= (1 << BRANCH_BITS
);
3994 stub_group_size
= (1 << BRANCH_BITS
);
3998 group_sections (htab
, stub_group_size
, stubs_always_before_branch
);
4000 switch (get_local_syms (output_bfd
, info
->input_bfds
, info
))
4003 if (htab
->all_local_syms
)
4004 goto error_ret_free_local
;
4008 stub_changed
= FALSE
;
4012 stub_changed
= TRUE
;
4019 unsigned int bfd_indx
;
4022 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
4024 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
4026 Elf_Internal_Shdr
*symtab_hdr
;
4028 Elf_Internal_Sym
*local_syms
;
4030 /* We'll need the symbol table in a second. */
4031 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4032 if (symtab_hdr
->sh_info
== 0)
4035 local_syms
= htab
->all_local_syms
[bfd_indx
];
4037 /* Walk over each section attached to the input bfd. */
4038 for (section
= input_bfd
->sections
;
4040 section
= section
->next
)
4042 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
4044 /* If there aren't any relocs, then there's nothing more
4046 if ((section
->flags
& SEC_RELOC
) == 0
4047 || section
->reloc_count
== 0)
4050 /* If this section is a link-once section that will be
4051 discarded, then don't create any stubs. */
4052 if (section
->output_section
== NULL
4053 || section
->output_section
->owner
!= output_bfd
)
4056 /* Get the relocs. */
4058 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
4060 if (internal_relocs
== NULL
)
4061 goto error_ret_free_local
;
4063 /* Now examine each relocation. */
4064 irela
= internal_relocs
;
4065 irelaend
= irela
+ section
->reloc_count
;
4066 for (; irela
< irelaend
; irela
++)
4068 unsigned int r_type
, r_indx
;
4069 enum elf_metag_stub_type stub_type
;
4070 struct elf_metag_stub_hash_entry
*hsh
;
4073 bfd_vma destination
;
4074 struct elf_metag_link_hash_entry
*hh
;
4076 const asection
*id_sec
;
4078 r_type
= ELF32_R_TYPE (irela
->r_info
);
4079 r_indx
= ELF32_R_SYM (irela
->r_info
);
4081 if (r_type
>= (unsigned int) R_METAG_MAX
)
4083 bfd_set_error (bfd_error_bad_value
);
4084 error_ret_free_internal
:
4085 if (elf_section_data (section
)->relocs
== NULL
)
4086 free (internal_relocs
);
4087 goto error_ret_free_local
;
4090 /* Only look for stubs on CALLR and B instructions. */
4091 if (!(r_type
== (unsigned int) R_METAG_RELBRANCH
||
4092 r_type
== (unsigned int) R_METAG_RELBRANCH_PLT
))
4095 /* Now determine the call target, its name, value,
4101 if (r_indx
< symtab_hdr
->sh_info
)
4103 /* It's a local symbol. */
4104 Elf_Internal_Sym
*sym
;
4105 Elf_Internal_Shdr
*hdr
;
4108 sym
= local_syms
+ r_indx
;
4109 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
4110 sym_value
= sym
->st_value
;
4111 shndx
= sym
->st_shndx
;
4112 if (shndx
< elf_numsections (input_bfd
))
4114 hdr
= elf_elfsections (input_bfd
)[shndx
];
4115 sym_sec
= hdr
->bfd_section
;
4116 destination
= (sym_value
+ irela
->r_addend
4117 + sym_sec
->output_offset
4118 + sym_sec
->output_section
->vma
);
4123 /* It's an external symbol. */
4126 e_indx
= r_indx
- symtab_hdr
->sh_info
;
4127 hh
= ((struct elf_metag_link_hash_entry
*)
4128 elf_sym_hashes (input_bfd
)[e_indx
]);
4130 while (hh
->eh
.root
.type
== bfd_link_hash_indirect
4131 || hh
->eh
.root
.type
== bfd_link_hash_warning
)
4132 hh
= ((struct elf_metag_link_hash_entry
*)
4133 hh
->eh
.root
.u
.i
.link
);
4135 if (hh
->eh
.root
.type
== bfd_link_hash_defined
4136 || hh
->eh
.root
.type
== bfd_link_hash_defweak
)
4138 sym_sec
= hh
->eh
.root
.u
.def
.section
;
4139 sym_value
= hh
->eh
.root
.u
.def
.value
;
4140 if (hh
->eh
.plt
.offset
!= (bfd_vma
) -1
4141 && hh
->eh
.dynindx
!= -1
4142 && r_type
== (unsigned int) R_METAG_RELBRANCH_PLT
)
4144 sym_sec
= htab
->splt
;
4145 sym_value
= hh
->eh
.plt
.offset
;
4148 if (sym_sec
->output_section
!= NULL
)
4149 destination
= (sym_value
+ irela
->r_addend
4150 + sym_sec
->output_offset
4151 + sym_sec
->output_section
->vma
);
4155 else if (hh
->eh
.root
.type
== bfd_link_hash_undefweak
)
4157 if (! bfd_link_pic (info
))
4160 else if (hh
->eh
.root
.type
== bfd_link_hash_undefined
)
4162 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
4163 && (ELF_ST_VISIBILITY (hh
->eh
.other
)
4169 bfd_set_error (bfd_error_bad_value
);
4170 goto error_ret_free_internal
;
4174 /* Determine what (if any) linker stub is needed. */
4175 stub_type
= metag_type_of_stub (section
, irela
, hh
,
4177 if (stub_type
== metag_stub_none
)
4180 /* Support for grouping stub sections. */
4181 id_sec
= htab
->stub_group
[section
->id
].link_sec
;
4183 /* Get the name of this stub. */
4184 stub_name
= metag_stub_name (id_sec
, sym_sec
, hh
, irela
);
4186 goto error_ret_free_internal
;
4188 hsh
= metag_stub_hash_lookup (&htab
->bstab
,
4193 /* The proper stub has already been created. */
4198 hsh
= metag_add_stub (stub_name
, section
, htab
);
4202 goto error_ret_free_internal
;
4204 hsh
->target_value
= sym_value
;
4205 hsh
->target_section
= sym_sec
;
4206 hsh
->stub_type
= stub_type
;
4208 hsh
->addend
= irela
->r_addend
;
4209 stub_changed
= TRUE
;
4212 /* We're done with the internal relocs, free them. */
4213 if (elf_section_data (section
)->relocs
== NULL
)
4214 free (internal_relocs
);
4221 /* OK, we've added some stubs. Find out the new size of the
4223 for (stub_sec
= htab
->stub_bfd
->sections
;
4225 stub_sec
= stub_sec
->next
)
4228 bfd_hash_traverse (&htab
->bstab
, metag_size_one_stub
, htab
);
4230 /* Ask the linker to do its stuff. */
4231 (*htab
->layout_sections_again
) ();
4232 stub_changed
= FALSE
;
4235 free (htab
->all_local_syms
);
4238 error_ret_free_local
:
4239 free (htab
->all_local_syms
);
4243 /* Build all the stubs associated with the current output file. The
4244 stubs are kept in a hash table attached to the main linker hash
4245 table. This function is called via metagelf_finish in the linker. */
4248 elf_metag_build_stubs (struct bfd_link_info
*info
)
4251 struct bfd_hash_table
*table
;
4252 struct elf_metag_link_hash_table
*htab
;
4254 htab
= metag_link_hash_table (info
);
4256 for (stub_sec
= htab
->stub_bfd
->sections
;
4258 stub_sec
= stub_sec
->next
)
4262 /* Allocate memory to hold the linker stubs. */
4263 size
= stub_sec
->size
;
4264 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
4265 if (stub_sec
->contents
== NULL
&& size
!= 0)
4270 /* Build the stubs as directed by the stub hash table. */
4271 table
= &htab
->bstab
;
4272 bfd_hash_traverse (table
, metag_build_one_stub
, info
);
4277 /* Return TRUE if SYM represents a local label symbol. */
4280 elf_metag_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
, const char *name
)
4282 if (name
[0] == '$' && name
[1] == 'L')
4284 return _bfd_elf_is_local_label_name (abfd
, name
);
4287 /* Return address for Ith PLT stub in section PLT, for relocation REL
4288 or (bfd_vma) -1 if it should not be included. */
4291 elf_metag_plt_sym_val (bfd_vma i
, const asection
*plt
,
4292 const arelent
*rel ATTRIBUTE_UNUSED
)
4294 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
4297 #define ELF_ARCH bfd_arch_metag
4298 #define ELF_TARGET_ID METAG_ELF_DATA
4299 #define ELF_MACHINE_CODE EM_METAG
4300 #define ELF_MINPAGESIZE 0x1000
4301 #define ELF_MAXPAGESIZE 0x4000
4302 #define ELF_COMMONPAGESIZE 0x1000
4304 #define TARGET_LITTLE_SYM metag_elf32_vec
4305 #define TARGET_LITTLE_NAME "elf32-metag"
4307 #define elf_symbol_leading_char '_'
4309 #define elf_info_to_howto_rel NULL
4310 #define elf_info_to_howto metag_info_to_howto_rela
4312 #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name
4313 #define bfd_elf32_bfd_link_hash_table_create \
4314 elf_metag_link_hash_table_create
4315 #define elf_backend_relocate_section elf_metag_relocate_section
4316 #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook
4317 #define elf_backend_gc_sweep_hook elf_metag_gc_sweep_hook
4318 #define elf_backend_check_relocs elf_metag_check_relocs
4319 #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections
4320 #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol
4321 #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol
4322 #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections
4323 #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections
4324 #define elf_backend_omit_section_dynsym \
4325 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4326 #define elf_backend_post_process_headers elf_metag_post_process_headers
4327 #define elf_backend_reloc_type_class elf_metag_reloc_type_class
4328 #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol
4329 #define elf_backend_plt_sym_val elf_metag_plt_sym_val
4331 #define elf_backend_can_gc_sections 1
4332 #define elf_backend_can_refcount 1
4333 #define elf_backend_got_header_size 12
4334 #define elf_backend_rela_normal 1
4335 #define elf_backend_want_got_sym 0
4336 #define elf_backend_want_plt_sym 0
4337 #define elf_backend_plt_readonly 1
4339 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4340 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4342 #include "elf32-target.h"