1 /* M32R-specific support for 32-bit ELF.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 static bfd_reloc_status_type m32r_elf_10_pcrel_reloc
28 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29 static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc
30 PARAMS ((bfd *, reloc_howto_type *, asection *,
31 bfd_byte *, bfd_vma, asection *, bfd_vma, bfd_vma));
32 static bfd_reloc_status_type m32r_elf_hi16_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static void m32r_elf_relocate_hi16
35 PARAMS ((bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *,
36 bfd_byte *, bfd_vma));
37 bfd_reloc_status_type m32r_elf_lo16_reloc
38 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 bfd_reloc_status_type m32r_elf_generic_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static bfd_reloc_status_type m32r_elf_sda16_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
44 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
45 static void m32r_info_to_howto_rel
46 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
47 static void m32r_info_to_howto
48 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
49 bfd_boolean _bfd_m32r_elf_section_from_bfd_section
50 PARAMS ((bfd *, asection *, int *));
51 void _bfd_m32r_elf_symbol_processing
52 PARAMS ((bfd *, asymbol *));
53 static bfd_boolean m32r_elf_add_symbol_hook
54 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
55 const char **, flagword *, asection **, bfd_vma *));
56 static bfd_boolean m32r_elf_relocate_section
57 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
58 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
60 static bfd_boolean m32r_elf_relax_delete_bytes
61 PARAMS ((bfd *, asection *, bfd_vma, int));
63 static bfd_reloc_status_type m32r_elf_final_sda_base
64 PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
65 static bfd_boolean m32r_elf_object_p
67 static void m32r_elf_final_write_processing
68 PARAMS ((bfd *, bfd_boolean));
69 static bfd_boolean m32r_elf_set_private_flags
70 PARAMS ((bfd *, flagword));
71 static bfd_boolean m32r_elf_merge_private_bfd_data
72 PARAMS ((bfd *, bfd *));
73 static bfd_boolean m32r_elf_print_private_bfd_data
74 PARAMS ((bfd *, PTR));
75 static bfd_boolean m32r_elf_gc_sweep_hook
76 PARAMS ((bfd *, struct bfd_link_info *, asection *,
77 const Elf_Internal_Rela *));
78 static bfd_boolean m32r_elf_check_relocs
79 PARAMS ((bfd *, struct bfd_link_info *, asection *,
80 const Elf_Internal_Rela *));
82 static bfd_boolean m32r_elf_adjust_dynamic_symbol
83 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
84 static bfd_boolean m32r_elf_size_dynamic_sections
85 PARAMS ((bfd *, struct bfd_link_info *));
87 asection * m32r_elf_gc_mark_hook
88 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
89 struct elf_link_hash_entry *, Elf_Internal_Sym *));
91 static bfd_boolean m32r_elf_create_dynamic_sections
92 PARAMS ((bfd *, struct bfd_link_info *));
94 static bfd_boolean m32r_elf_finish_dynamic_sections
95 PARAMS ((bfd *, struct bfd_link_info *));
97 static bfd_boolean m32r_elf_finish_dynamic_symbol
98 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
101 static bfd_boolean allocate_dynrelocs
102 PARAMS ((struct elf_link_hash_entry *, PTR));
103 static bfd_boolean readonly_dynrelocs
104 PARAMS ((struct elf_link_hash_entry *, PTR));
105 static enum elf_reloc_type_class m32r_elf_reloc_type_class
106 PARAMS ((const Elf_Internal_Rela *));
107 static bfd_boolean m32r_elf_fake_sections
108 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
110 #define NOP_INSN 0x7000
111 #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
113 /* Use REL instead of RELA to save space.
114 This only saves space in libraries and object files, but perhaps
115 relocs will be put in ROM? All in all though, REL relocs are a pain
122 /* Use RELA. But use REL to link old objects for backwords compatibility. */
124 /* Functions for the M32R ELF linker. */
126 /* The name of the dynamic interpreter. This is put in the .interp
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
131 /* The nop opcode we use. */
133 #define M32R_NOP 0x7000f000
135 #define PLT_EMPTY 0x10101010 /* RIE -> RIE */
137 /* The size in bytes of an entry in the procedure linkage table. */
139 #define PLT_ENTRY_SIZE 20
140 #define PLT_HEADER_SIZE 20
142 /* The first one entries in a procedure linkage table are reserved,
143 and the initial contents are unimportant (we zero them out).
144 Subsequent entries look like this. */
146 #define PLT0_ENTRY_WORD0 0xd6c00000 /* seth r6, #high(.got+4) */
147 #define PLT0_ENTRY_WORD1 0x86e60000 /* or3 r6, r6, #low(.got)+4) */
148 #define PLT0_ENTRY_WORD2 0x24e626c6 /* ld r4, @r6+ -> ld r6, @r6 */
149 #define PLT0_ENTRY_WORD3 0x1fc6f000 /* jmp r6 || pnop */
150 #define PLT0_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
152 #define PLT0_PIC_ENTRY_WORD0 0xa4cc0004 /* ld r4, @(4,r12) */
153 #define PLT0_PIC_ENTRY_WORD1 0xa6cc0008 /* ld r6, @(8,r12) */
154 #define PLT0_PIC_ENTRY_WORD2 0x1fc6f000 /* jmp r6 || nop */
155 #define PLT0_PIC_ENTRY_WORD3 PLT_EMPTY /* RIE -> RIE */
156 #define PLT0_PIC_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
158 #define PLT_ENTRY_WORD0 0xe6000000 /* ld24 r6, .name_in_GOT */
159 #define PLT_ENTRY_WORD1 0x06acf000 /* add r6, r12 || nop */
160 #define PLT_ENTRY_WORD0b 0xd6c00000 /* seth r6, #high(.name_in_GOT) */
161 #define PLT_ENTRY_WORD1b 0x86e60000 /* or3 r6, r6, #low(.name_in_GOT) */
162 #define PLT_ENTRY_WORD2 0x26c61fc6 /* ld r6, @r6 -> jmp r6 */
163 #define PLT_ENTRY_WORD3 0xe5000000 /* ld24 r5, $offset */
164 #define PLT_ENTRY_WORD4 0xff000000 /* bra .plt0. */
167 static reloc_howto_type m32r_elf_howto_table[] =
169 /* This reloc does nothing. */
170 HOWTO (R_M32R_NONE, /* type */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
174 FALSE, /* pc_relative */
176 complain_overflow_bitfield, /* complain_on_overflow */
177 bfd_elf_generic_reloc, /* special_function */
178 "R_M32R_NONE", /* name */
179 FALSE, /* partial_inplace */
182 FALSE), /* pcrel_offset */
184 /* A 16 bit absolute relocation. */
185 HOWTO (R_M32R_16, /* type */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
189 FALSE, /* pc_relative */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 m32r_elf_generic_reloc,/* special_function */
193 "R_M32R_16", /* name */
194 TRUE, /* partial_inplace */
195 0xffff, /* src_mask */
196 0xffff, /* dst_mask */
197 FALSE), /* pcrel_offset */
199 /* A 32 bit absolute relocation. */
200 HOWTO (R_M32R_32, /* type */
202 2, /* size (0 = byte, 1 = short, 2 = long) */
204 FALSE, /* pc_relative */
206 complain_overflow_bitfield, /* complain_on_overflow */
207 m32r_elf_generic_reloc,/* special_function */
208 "R_M32R_32", /* name */
209 TRUE, /* partial_inplace */
210 0xffffffff, /* src_mask */
211 0xffffffff, /* dst_mask */
212 FALSE), /* pcrel_offset */
214 /* A 24 bit address. */
215 HOWTO (R_M32R_24, /* type */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
219 FALSE, /* pc_relative */
221 complain_overflow_unsigned, /* complain_on_overflow */
222 m32r_elf_generic_reloc,/* special_function */
223 "R_M32R_24", /* name */
224 TRUE, /* partial_inplace */
225 0xffffff, /* src_mask */
226 0xffffff, /* dst_mask */
227 FALSE), /* pcrel_offset */
229 /* An PC Relative 10-bit relocation, shifted by 2.
230 This reloc is complicated because relocations are relative to pc & -4.
231 i.e. branches in the right insn slot use the address of the left insn
233 /* ??? It's not clear whether this should have partial_inplace set or not.
234 Branch relaxing in the assembler can store the addend in the insn,
235 and if bfd_install_relocation gets called the addend may get added
237 HOWTO (R_M32R_10_PCREL, /* type */
239 1, /* size (0 = byte, 1 = short, 2 = long) */
241 TRUE, /* pc_relative */
243 complain_overflow_signed, /* complain_on_overflow */
244 m32r_elf_10_pcrel_reloc, /* special_function */
245 "R_M32R_10_PCREL", /* name */
246 FALSE, /* partial_inplace */
249 TRUE), /* pcrel_offset */
251 /* A relative 18 bit relocation, right shifted by 2. */
252 HOWTO (R_M32R_18_PCREL, /* type */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
256 TRUE, /* pc_relative */
258 complain_overflow_signed, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_M32R_18_PCREL", /* name */
261 FALSE, /* partial_inplace */
262 0xffff, /* src_mask */
263 0xffff, /* dst_mask */
264 TRUE), /* pcrel_offset */
266 /* A relative 26 bit relocation, right shifted by 2. */
267 /* ??? It's not clear whether this should have partial_inplace set or not.
268 Branch relaxing in the assembler can store the addend in the insn,
269 and if bfd_install_relocation gets called the addend may get added
271 HOWTO (R_M32R_26_PCREL, /* type */
273 2, /* size (0 = byte, 1 = short, 2 = long) */
275 TRUE, /* pc_relative */
277 complain_overflow_signed, /* complain_on_overflow */
278 bfd_elf_generic_reloc, /* special_function */
279 "R_M32R_26_PCREL", /* name */
280 FALSE, /* partial_inplace */
281 0xffffff, /* src_mask */
282 0xffffff, /* dst_mask */
283 TRUE), /* pcrel_offset */
285 /* High 16 bits of address when lower 16 is or'd in. */
286 HOWTO (R_M32R_HI16_ULO, /* type */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
290 FALSE, /* pc_relative */
292 complain_overflow_dont, /* complain_on_overflow */
293 m32r_elf_hi16_reloc, /* special_function */
294 "R_M32R_HI16_ULO", /* name */
295 TRUE, /* partial_inplace */
296 0x0000ffff, /* src_mask */
297 0x0000ffff, /* dst_mask */
298 FALSE), /* pcrel_offset */
300 /* High 16 bits of address when lower 16 is added in. */
301 HOWTO (R_M32R_HI16_SLO, /* type */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
305 FALSE, /* pc_relative */
307 complain_overflow_dont, /* complain_on_overflow */
308 m32r_elf_hi16_reloc, /* special_function */
309 "R_M32R_HI16_SLO", /* name */
310 TRUE, /* partial_inplace */
311 0x0000ffff, /* src_mask */
312 0x0000ffff, /* dst_mask */
313 FALSE), /* pcrel_offset */
315 /* Lower 16 bits of address. */
316 HOWTO (R_M32R_LO16, /* type */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
320 FALSE, /* pc_relative */
322 complain_overflow_dont, /* complain_on_overflow */
323 m32r_elf_lo16_reloc, /* special_function */
324 "R_M32R_LO16", /* name */
325 TRUE, /* partial_inplace */
326 0x0000ffff, /* src_mask */
327 0x0000ffff, /* dst_mask */
328 FALSE), /* pcrel_offset */
330 /* Small data area 16 bits offset. */
331 HOWTO (R_M32R_SDA16, /* type */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
335 FALSE, /* pc_relative */
337 complain_overflow_signed, /* complain_on_overflow */
338 m32r_elf_sda16_reloc, /* special_function */
339 "R_M32R_SDA16", /* name */
340 TRUE, /* partial_inplace */ /* FIXME: correct? */
341 0x0000ffff, /* src_mask */
342 0x0000ffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
345 /* GNU extension to record C++ vtable hierarchy */
346 HOWTO (R_M32R_GNU_VTINHERIT, /* type */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
350 FALSE, /* pc_relative */
352 complain_overflow_dont, /* complain_on_overflow */
353 NULL, /* special_function */
354 "R_M32R_GNU_VTINHERIT", /* name */
355 FALSE, /* partial_inplace */
358 FALSE), /* pcrel_offset */
360 /* GNU extension to record C++ vtable member usage */
361 HOWTO (R_M32R_GNU_VTENTRY, /* type */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
365 FALSE, /* pc_relative */
367 complain_overflow_dont, /* complain_on_overflow */
368 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
369 "R_M32R_GNU_VTENTRY", /* name */
370 FALSE, /* partial_inplace */
373 FALSE), /* pcrel_offset */
396 /* A 16 bit absolute relocation. */
397 HOWTO (R_M32R_16_RELA, /* type */
399 1, /* size (0 = byte, 1 = short, 2 = long) */
401 FALSE, /* pc_relative */
403 complain_overflow_bitfield, /* complain_on_overflow */
404 bfd_elf_generic_reloc, /* special_function */
405 "R_M32R_16_RELA", /* name */
406 FALSE, /* partial_inplace */
407 0xffff, /* src_mask */
408 0xffff, /* dst_mask */
409 FALSE), /* pcrel_offset */
411 /* A 32 bit absolute relocation. */
412 HOWTO (R_M32R_32_RELA, /* type */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
416 FALSE, /* pc_relative */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 bfd_elf_generic_reloc,/* special_function */
420 "R_M32R_32_RELA", /* name */
421 FALSE, /* partial_inplace */
422 0xffffffff, /* src_mask */
423 0xffffffff, /* dst_mask */
424 FALSE), /* pcrel_offset */
426 /* A 24 bit address. */
427 HOWTO (R_M32R_24_RELA, /* type */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
431 FALSE, /* pc_relative */
433 complain_overflow_unsigned, /* complain_on_overflow */
434 bfd_elf_generic_reloc,/* special_function */
435 "R_M32R_24_RELA", /* name */
436 FALSE, /* partial_inplace */
437 0xffffff, /* src_mask */
438 0xffffff, /* dst_mask */
439 FALSE), /* pcrel_offset */
441 HOWTO (R_M32R_10_PCREL_RELA, /* type */
443 1, /* size (0 = byte, 1 = short, 2 = long) */
445 TRUE, /* pc_relative */
447 complain_overflow_signed, /* complain_on_overflow */
448 m32r_elf_10_pcrel_reloc, /* special_function */
449 "R_M32R_10_PCREL_RELA",/* name */
450 FALSE, /* partial_inplace */
453 TRUE), /* pcrel_offset */
455 /* A relative 18 bit relocation, right shifted by 2. */
456 HOWTO (R_M32R_18_PCREL_RELA, /* type */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
460 TRUE, /* pc_relative */
462 complain_overflow_signed, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_M32R_18_PCREL_RELA",/* name */
465 FALSE, /* partial_inplace */
466 0xffff, /* src_mask */
467 0xffff, /* dst_mask */
468 TRUE), /* pcrel_offset */
470 /* A relative 26 bit relocation, right shifted by 2. */
471 HOWTO (R_M32R_26_PCREL_RELA, /* type */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
475 TRUE, /* pc_relative */
477 complain_overflow_signed, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_M32R_26_PCREL_RELA",/* name */
480 FALSE, /* partial_inplace */
481 0xffffff, /* src_mask */
482 0xffffff, /* dst_mask */
483 TRUE), /* pcrel_offset */
485 /* High 16 bits of address when lower 16 is or'd in. */
486 HOWTO (R_M32R_HI16_ULO_RELA, /* type */
488 2, /* size (0 = byte, 1 = short, 2 = long) */
490 FALSE, /* pc_relative */
492 complain_overflow_dont, /* complain_on_overflow */
493 bfd_elf_generic_reloc, /* special_function */
494 "R_M32R_HI16_ULO_RELA",/* name */
495 FALSE, /* partial_inplace */
496 0x0000ffff, /* src_mask */
497 0x0000ffff, /* dst_mask */
498 FALSE), /* pcrel_offset */
500 /* High 16 bits of address when lower 16 is added in. */
501 HOWTO (R_M32R_HI16_SLO_RELA, /* type */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
505 FALSE, /* pc_relative */
507 complain_overflow_dont, /* complain_on_overflow */
508 bfd_elf_generic_reloc, /* special_function */
509 "R_M32R_HI16_SLO_RELA",/* name */
510 FALSE, /* partial_inplace */
511 0x0000ffff, /* src_mask */
512 0x0000ffff, /* dst_mask */
513 FALSE), /* pcrel_offset */
515 /* Lower 16 bits of address. */
516 HOWTO (R_M32R_LO16_RELA, /* type */
518 2, /* size (0 = byte, 1 = short, 2 = long) */
520 FALSE, /* pc_relative */
522 complain_overflow_dont, /* complain_on_overflow */
523 bfd_elf_generic_reloc, /* special_function */
524 "R_M32R_LO16_RELA", /* name */
525 FALSE, /* partial_inplace */
526 0x0000ffff, /* src_mask */
527 0x0000ffff, /* dst_mask */
528 FALSE), /* pcrel_offset */
530 /* Small data area 16 bits offset. */
531 HOWTO (R_M32R_SDA16_RELA, /* type */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
535 FALSE, /* pc_relative */
537 complain_overflow_signed, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_M32R_SDA16_RELA", /* name */
540 TRUE, /* partial_inplace */ /* FIXME: correct? */
541 0x0000ffff, /* src_mask */
542 0x0000ffff, /* dst_mask */
543 FALSE), /* pcrel_offset */
545 /* GNU extension to record C++ vtable hierarchy */
546 HOWTO (R_M32R_RELA_GNU_VTINHERIT, /* type */
548 2, /* size (0 = byte, 1 = short, 2 = long) */
550 FALSE, /* pc_relative */
552 complain_overflow_dont, /* complain_on_overflow */
553 NULL, /* special_function */
554 "R_M32R_RELA_GNU_VTINHERIT", /* name */
555 FALSE, /* partial_inplace */
558 FALSE), /* pcrel_offset */
560 /* GNU extension to record C++ vtable member usage */
561 HOWTO (R_M32R_RELA_GNU_VTENTRY, /* type */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
565 FALSE, /* pc_relative */
567 complain_overflow_dont, /* complain_on_overflow */
568 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
569 "R_M32R_RELA_GNU_VTENTRY", /* name */
570 FALSE, /* partial_inplace */
573 FALSE), /* pcrel_offset */
579 /* Like R_M32R_24, but referring to the GOT table entry for
581 HOWTO (R_M32R_GOT24, /* type */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
585 FALSE, /* pc_relative */
587 complain_overflow_unsigned, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_M32R_GOT24", /* name */
590 FALSE, /* partial_inplace */
591 0xffffff, /* src_mask */
592 0xffffff, /* dst_mask */
593 FALSE), /* pcrel_offset */
595 /* Like R_M32R_PCREL, but referring to the procedure linkage table
596 entry for the symbol. */
597 HOWTO (R_M32R_26_PLTREL, /* type */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
601 TRUE, /* pc_relative */
603 complain_overflow_signed, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_M32R_26_PLTREL", /* name */
606 FALSE, /* partial_inplace */
607 0xffffff, /* src_mask */
608 0xffffff, /* dst_mask */
609 TRUE), /* pcrel_offset */
611 /* This is used only by the dynamic linker. The symbol should exist
612 both in the object being run and in some shared library. The
613 dynamic linker copies the data addressed by the symbol from the
614 shared library into the object, because the object being
615 run has to have the data at some particular address. */
616 HOWTO (R_M32R_COPY, /* type */
618 2, /* size (0 = byte, 1 = short, 2 = long) */
620 FALSE, /* pc_relative */
622 complain_overflow_bitfield, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_M32R_COPY", /* name */
625 FALSE, /* partial_inplace */
626 0xffffffff, /* src_mask */
627 0xffffffff, /* dst_mask */
628 FALSE), /* pcrel_offset */
630 /* Like R_M32R_24, but used when setting global offset table
632 HOWTO (R_M32R_GLOB_DAT, /* type */
634 2, /* size (0 = byte, 1 = short, 2 = long) */
636 FALSE, /* pc_relative */
638 complain_overflow_bitfield, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_M32R_GLOB_DAT", /* name */
641 FALSE, /* partial_inplace */
642 0xffffffff, /* src_mask */
643 0xffffffff, /* dst_mask */
644 FALSE), /* pcrel_offset */
646 /* Marks a procedure linkage table entry for a symbol. */
647 HOWTO (R_M32R_JMP_SLOT, /* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 FALSE, /* pc_relative */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_M32R_JMP_SLOT", /* name */
656 FALSE, /* partial_inplace */
657 0xffffffff, /* src_mask */
658 0xffffffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* Used only by the dynamic linker. When the object is run, this
662 longword is set to the load address of the object, plus the
664 HOWTO (R_M32R_RELATIVE, /* type */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
668 FALSE, /* pc_relative */
670 complain_overflow_bitfield, /* complain_on_overflow */
671 bfd_elf_generic_reloc, /* special_function */
672 "R_M32R_RELATIVE", /* name */
673 FALSE, /* partial_inplace */
674 0xffffffff, /* src_mask */
675 0xffffffff, /* dst_mask */
676 FALSE), /* pcrel_offset */
678 HOWTO (R_M32R_GOTOFF, /* type */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
682 FALSE, /* pc_relative */
684 complain_overflow_bitfield, /* complain_on_overflow */
685 bfd_elf_generic_reloc, /* special_function */
686 "R_M32R_GOTOFF", /* name */
687 FALSE, /* partial_inplace */
688 0xffffffff, /* src_mask */
689 0xffffffff, /* dst_mask */
690 FALSE), /* pcrel_offset */
692 /* An PC Relative 24-bit relocation used when setting PIC offset
694 HOWTO (R_M32R_GOTPC24, /* type */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
698 TRUE, /* pc_relative */
700 complain_overflow_unsigned, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_M32R_GOTPC24", /* name */
703 FALSE, /* partial_inplace */
704 0xffffff, /* src_mask */
705 0xffffff, /* dst_mask */
706 TRUE), /* pcrel_offset */
708 /* Like R_M32R_HI16_ULO, but referring to the GOT table entry for
710 HOWTO (R_M32R_GOT16_HI_ULO, /* type */
712 2, /* size (0 = byte, 1 = short, 2 = long) */
714 FALSE, /* pc_relative */
716 complain_overflow_dont, /* complain_on_overflow */
717 bfd_elf_generic_reloc, /* special_function */
718 "R_M32R_GOT16_HI_ULO", /* name */
719 FALSE, /* partial_inplace */
720 0x0000ffff, /* src_mask */
721 0x0000ffff, /* dst_mask */
722 FALSE), /* pcrel_offset */
724 /* Like R_M32R_HI16_SLO, but referring to the GOT table entry for
726 HOWTO (R_M32R_GOT16_HI_SLO, /* type */
728 2, /* size (0 = byte, 1 = short, 2 = long) */
730 FALSE, /* pc_relative */
732 complain_overflow_dont, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
734 "R_M32R_GOT16_HI_SLO", /* name */
735 FALSE, /* partial_inplace */
736 0x0000ffff, /* src_mask */
737 0x0000ffff, /* dst_mask */
738 FALSE), /* pcrel_offset */
740 /* Like R_M32R_LO16, but referring to the GOT table entry for
742 HOWTO (R_M32R_GOT16_LO, /* type */
744 2, /* size (0 = byte, 1 = short, 2 = long) */
746 FALSE, /* pc_relative */
748 complain_overflow_dont, /* complain_on_overflow */
749 bfd_elf_generic_reloc, /* special_function */
750 "R_M32R_GOT16_LO", /* name */
751 FALSE, /* partial_inplace */
752 0x0000ffff, /* src_mask */
753 0x0000ffff, /* dst_mask */
754 FALSE), /* pcrel_offset */
756 /* An PC Relative relocation used when setting PIC offset table register.
757 Like R_M32R_HI16_ULO, but referring to the GOT table entry for
759 HOWTO (R_M32R_GOTPC_HI_ULO, /* type */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
763 FALSE, /* pc_relative */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_M32R_GOTPC_HI_ULO", /* name */
768 FALSE, /* partial_inplace */
769 0x0000ffff, /* src_mask */
770 0x0000ffff, /* dst_mask */
771 TRUE), /* pcrel_offset */
773 /* An PC Relative relocation used when setting PIC offset table register.
774 Like R_M32R_HI16_SLO, but referring to the GOT table entry for
776 HOWTO (R_M32R_GOTPC_HI_SLO, /* type */
778 2, /* size (0 = byte, 1 = short, 2 = long) */
780 FALSE, /* pc_relative */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_M32R_GOTPC_HI_SLO", /* name */
785 FALSE, /* partial_inplace */
786 0x0000ffff, /* src_mask */
787 0x0000ffff, /* dst_mask */
788 TRUE), /* pcrel_offset */
790 /* An PC Relative relocation used when setting PIC offset table register.
791 Like R_M32R_LO16, but referring to the GOT table entry for
793 HOWTO (R_M32R_GOTPC_LO, /* type */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
797 FALSE, /* pc_relative */
799 complain_overflow_dont, /* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 "R_M32R_GOTPC_LO", /* name */
802 FALSE, /* partial_inplace */
803 0x0000ffff, /* src_mask */
804 0x0000ffff, /* dst_mask */
805 TRUE), /* pcrel_offset */
808 /* Handle the R_M32R_10_PCREL reloc. */
810 static bfd_reloc_status_type
811 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
812 input_section, output_bfd, error_message)
814 arelent * reloc_entry;
817 asection * input_section;
819 char ** error_message ATTRIBUTE_UNUSED;
821 /* This part is from bfd_elf_generic_reloc. */
822 if (output_bfd != (bfd *) NULL
823 && (symbol->flags & BSF_SECTION_SYM) == 0
824 && (! reloc_entry->howto->partial_inplace
825 || reloc_entry->addend == 0))
827 reloc_entry->address += input_section->output_offset;
831 if (output_bfd != NULL)
833 /* FIXME: See bfd_perform_relocation. Is this right? */
834 return bfd_reloc_continue;
837 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
839 data, reloc_entry->address,
842 + symbol->section->output_section->vma
843 + symbol->section->output_offset),
844 reloc_entry->addend);
847 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
849 static bfd_reloc_status_type
850 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
851 symbol_section, symbol_value, addend)
853 reloc_howto_type *howto;
854 asection *input_section;
857 asection *symbol_section ATTRIBUTE_UNUSED;
858 bfd_vma symbol_value;
861 bfd_signed_vma relocation;
864 bfd_reloc_status_type status;
866 /* Sanity check the address (offset in section). */
867 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
869 return bfd_reloc_outofrange;
871 relocation = symbol_value + addend;
872 /* Make it pc relative. */
873 relocation -= (input_section->output_section->vma
874 + input_section->output_offset);
875 /* These jumps mask off the lower two bits of the current address
876 before doing pcrel calculations. */
877 relocation -= (offset & -(bfd_vma) 4);
879 if (relocation < -0x200 || relocation > 0x1ff)
880 status = bfd_reloc_overflow;
882 status = bfd_reloc_ok;
884 x = bfd_get_16 (abfd, data + offset);
885 relocation >>= howto->rightshift;
886 relocation <<= howto->bitpos;
887 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
888 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
893 /* Handle the R_M32R_HI16_[SU]LO relocs.
894 HI16_SLO is for the add3 and load/store with displacement instructions.
895 HI16_ULO is for the or3 instruction.
896 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
897 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
898 we must add one to the high 16 bytes (which will get subtracted off when
899 the low 16 bits are added).
900 These relocs have to be done in combination with an R_M32R_LO16 reloc
901 because there is a carry from the LO16 to the HI16. Here we just save
902 the information we need; we do the actual relocation when we see the LO16.
903 This code is copied from the elf32-mips.c. We also support an arbitrary
904 number of HI16 relocs to be associated with a single LO16 reloc. The
905 assembler sorts the relocs to ensure each HI16 immediately precedes its
906 LO16. However if there are multiple copies, the assembler may not find
907 the real LO16 so it picks the first one it finds. */
911 struct m32r_hi16 *next;
916 /* FIXME: This should not be a static variable. */
918 static struct m32r_hi16 *m32r_hi16_list;
920 static bfd_reloc_status_type
921 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
922 input_section, output_bfd, error_message)
923 bfd *abfd ATTRIBUTE_UNUSED;
924 arelent *reloc_entry;
927 asection *input_section;
929 char **error_message ATTRIBUTE_UNUSED;
931 bfd_reloc_status_type ret;
936 /* This part is from bfd_elf_generic_reloc.
937 If we're relocating, and this an external symbol, we don't want
938 to change anything. */
939 if (output_bfd != (bfd *) NULL
940 && (symbol->flags & BSF_SECTION_SYM) == 0
941 && reloc_entry->addend == 0)
943 reloc_entry->address += input_section->output_offset;
947 /* Sanity check the address (offset in section). */
948 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
949 if (reloc_entry->address > sz)
950 return bfd_reloc_outofrange;
953 if (bfd_is_und_section (symbol->section)
954 && output_bfd == (bfd *) NULL)
955 ret = bfd_reloc_undefined;
957 if (bfd_is_com_section (symbol->section))
960 relocation = symbol->value;
962 relocation += symbol->section->output_section->vma;
963 relocation += symbol->section->output_offset;
964 relocation += reloc_entry->addend;
966 /* Save the information, and let LO16 do the actual relocation. */
967 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
969 return bfd_reloc_outofrange;
970 n->addr = (bfd_byte *) data + reloc_entry->address;
971 n->addend = relocation;
972 n->next = m32r_hi16_list;
975 if (output_bfd != (bfd *) NULL)
976 reloc_entry->address += input_section->output_offset;
981 /* Handle an M32R ELF HI16 reloc. */
984 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
987 Elf_Internal_Rela *relhi;
988 Elf_Internal_Rela *rello;
995 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
997 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
998 if (type == R_M32R_HI16_SLO)
999 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
1003 addend += ((insn & 0xffff) << 16) + addlo;
1005 /* Reaccount for sign extension of low part. */
1006 if (type == R_M32R_HI16_SLO
1007 && (addend & 0x8000) != 0)
1010 bfd_put_32 (input_bfd,
1011 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1012 contents + relhi->r_offset);
1015 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1016 inplace relocation; this function exists in order to do the
1017 R_M32R_HI16_[SU]LO relocation described above. */
1019 bfd_reloc_status_type
1020 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1021 input_section, output_bfd, error_message)
1023 arelent *reloc_entry;
1026 asection *input_section;
1028 char **error_message;
1030 /* This part is from bfd_elf_generic_reloc.
1031 If we're relocating, and this an external symbol, we don't want
1032 to change anything. */
1033 if (output_bfd != (bfd *) NULL
1034 && (symbol->flags & BSF_SECTION_SYM) == 0
1035 && reloc_entry->addend == 0)
1037 reloc_entry->address += input_section->output_offset;
1038 return bfd_reloc_ok;
1041 if (m32r_hi16_list != NULL)
1043 struct m32r_hi16 *l;
1050 unsigned long vallo;
1051 struct m32r_hi16 *next;
1053 /* Do the HI16 relocation. Note that we actually don't need
1054 to know anything about the LO16 itself, except where to
1055 find the low 16 bits of the addend needed by the LO16. */
1056 insn = bfd_get_32 (input_bfd, l->addr);
1057 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
1058 & 0xffff) ^ 0x8000) - 0x8000;
1059 val = ((insn & 0xffff) << 16) + vallo;
1062 /* Reaccount for sign extension of low part. */
1063 if ((val & 0x8000) != 0)
1066 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1067 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1074 m32r_hi16_list = NULL;
1077 /* Now do the LO16 reloc in the usual way.
1078 ??? It would be nice to call bfd_elf_generic_reloc here,
1079 but we have partial_inplace set. bfd_elf_generic_reloc will
1080 pass the handling back to bfd_install_relocation which will install
1081 a section relative addend which is wrong. */
1082 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1083 input_section, output_bfd, error_message);
1086 /* Do generic partial_inplace relocation.
1087 This is a local replacement for bfd_elf_generic_reloc. */
1089 bfd_reloc_status_type
1090 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1091 input_section, output_bfd, error_message)
1093 arelent *reloc_entry;
1096 asection *input_section;
1098 char **error_message ATTRIBUTE_UNUSED;
1100 bfd_reloc_status_type ret;
1103 bfd_byte *inplace_address;
1105 /* This part is from bfd_elf_generic_reloc.
1106 If we're relocating, and this an external symbol, we don't want
1107 to change anything. */
1108 if (output_bfd != (bfd *) NULL
1109 && (symbol->flags & BSF_SECTION_SYM) == 0
1110 && reloc_entry->addend == 0)
1112 reloc_entry->address += input_section->output_offset;
1113 return bfd_reloc_ok;
1116 /* Now do the reloc in the usual way.
1117 ??? It would be nice to call bfd_elf_generic_reloc here,
1118 but we have partial_inplace set. bfd_elf_generic_reloc will
1119 pass the handling back to bfd_install_relocation which will install
1120 a section relative addend which is wrong. */
1122 /* Sanity check the address (offset in section). */
1123 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
1124 if (reloc_entry->address > sz)
1125 return bfd_reloc_outofrange;
1128 if (bfd_is_und_section (symbol->section)
1129 && output_bfd == (bfd *) NULL)
1130 ret = bfd_reloc_undefined;
1132 if (bfd_is_com_section (symbol->section)
1133 || output_bfd != (bfd *) NULL)
1136 relocation = symbol->value;
1138 /* Only do this for a final link. */
1139 if (output_bfd == (bfd *) NULL)
1141 relocation += symbol->section->output_section->vma;
1142 relocation += symbol->section->output_offset;
1145 relocation += reloc_entry->addend;
1146 inplace_address = (bfd_byte *) data + reloc_entry->address;
1149 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1150 (((x & reloc_entry->howto->src_mask) + relocation) & \
1151 reloc_entry->howto->dst_mask))
1153 switch (reloc_entry->howto->size)
1157 short x = bfd_get_16 (input_bfd, inplace_address);
1159 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1164 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1166 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1173 if (output_bfd != (bfd *) NULL)
1174 reloc_entry->address += input_section->output_offset;
1179 /* Handle the R_M32R_SDA16 reloc.
1180 This reloc is used to compute the address of objects in the small data area
1181 and to perform loads and stores from that area.
1182 The lower 16 bits are sign extended and added to the register specified
1183 in the instruction, which is assumed to point to _SDA_BASE_. */
1185 static bfd_reloc_status_type
1186 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1187 input_section, output_bfd, error_message)
1188 bfd *abfd ATTRIBUTE_UNUSED;
1189 arelent *reloc_entry;
1191 PTR data ATTRIBUTE_UNUSED;
1192 asection *input_section;
1194 char **error_message ATTRIBUTE_UNUSED;
1196 /* This part is from bfd_elf_generic_reloc. */
1197 if (output_bfd != (bfd *) NULL
1198 && (symbol->flags & BSF_SECTION_SYM) == 0
1199 && (! reloc_entry->howto->partial_inplace
1200 || reloc_entry->addend == 0))
1202 reloc_entry->address += input_section->output_offset;
1203 return bfd_reloc_ok;
1206 if (output_bfd != NULL)
1208 /* FIXME: See bfd_perform_relocation. Is this right? */
1209 return bfd_reloc_continue;
1212 /* FIXME: not sure what to do here yet. But then again, the linker
1213 may never call us. */
1217 /* Map BFD reloc types to M32R ELF reloc types. */
1219 struct m32r_reloc_map
1221 bfd_reloc_code_real_type bfd_reloc_val;
1222 unsigned char elf_reloc_val;
1225 static const struct m32r_reloc_map m32r_reloc_map_old[] =
1227 { BFD_RELOC_NONE, R_M32R_NONE },
1228 { BFD_RELOC_16, R_M32R_16 },
1229 { BFD_RELOC_32, R_M32R_32 },
1230 { BFD_RELOC_M32R_24, R_M32R_24 },
1231 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1232 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1233 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1234 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1235 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1236 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1237 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1238 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1239 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1242 static const struct m32r_reloc_map m32r_reloc_map[] =
1244 { BFD_RELOC_NONE, R_M32R_NONE },
1245 { BFD_RELOC_16, R_M32R_16_RELA },
1246 { BFD_RELOC_32, R_M32R_32_RELA },
1247 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1248 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1249 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1250 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1251 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1252 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1253 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1254 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1255 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1256 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
1258 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1259 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1260 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1261 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1262 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1263 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1264 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1265 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1266 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1267 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1268 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1269 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1270 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1271 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
1274 static reloc_howto_type *
1275 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1276 bfd *abfd ATTRIBUTE_UNUSED;
1277 bfd_reloc_code_real_type code;
1281 #ifdef USE_M32R_OLD_RELOC
1283 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1286 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1287 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1289 #else /* ! USE_M32R_OLD_RELOC */
1292 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1295 if (m32r_reloc_map[i].bfd_reloc_val == code)
1296 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1303 /* Set the howto pointer for an M32R ELF reloc. */
1306 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1307 bfd *abfd ATTRIBUTE_UNUSED;
1309 Elf_Internal_Rela *dst;
1311 unsigned int r_type;
1313 r_type = ELF32_R_TYPE (dst->r_info);
1314 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY)
1315 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1319 m32r_info_to_howto (abfd, cache_ptr, dst)
1320 bfd *abfd ATTRIBUTE_UNUSED;
1322 Elf_Internal_Rela *dst;
1324 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1325 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1326 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1327 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1331 /* Given a BFD section, try to locate the corresponding ELF section
1335 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1336 bfd *abfd ATTRIBUTE_UNUSED;
1340 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1342 *retval = SHN_M32R_SCOMMON;
1348 /* M32R ELF uses two common sections. One is the usual one, and the other
1349 is for small objects. All the small objects are kept together, and then
1350 referenced via one register, which yields faster assembler code. It is
1351 up to the compiler to emit an instruction to load the register with
1352 _SDA_BASE. This is what we use for the small common section. This
1353 approach is copied from elf32-mips.c. */
1354 static asection m32r_elf_scom_section;
1355 static asymbol m32r_elf_scom_symbol;
1356 static asymbol *m32r_elf_scom_symbol_ptr;
1358 /* Handle the special M32R section numbers that a symbol may use. */
1361 _bfd_m32r_elf_symbol_processing (abfd, asym)
1362 bfd *abfd ATTRIBUTE_UNUSED;
1365 elf_symbol_type *elfsym;
1367 elfsym = (elf_symbol_type *) asym;
1369 switch (elfsym->internal_elf_sym.st_shndx)
1371 case SHN_M32R_SCOMMON:
1372 if (m32r_elf_scom_section.name == NULL)
1374 /* Initialize the small common section. */
1375 m32r_elf_scom_section.name = ".scommon";
1376 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1377 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1378 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1379 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1380 m32r_elf_scom_symbol.name = ".scommon";
1381 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1382 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1383 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1385 asym->section = &m32r_elf_scom_section;
1386 asym->value = elfsym->internal_elf_sym.st_size;
1391 /* Hook called by the linker routine which adds symbols from an object
1392 file. We must handle the special M32R section numbers here.
1393 We also keep watching for whether we need to create the sdata special
1397 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1399 struct bfd_link_info *info;
1400 Elf_Internal_Sym *sym;
1402 flagword *flagsp ATTRIBUTE_UNUSED;
1406 if (! info->relocatable
1407 && (*namep)[0] == '_' && (*namep)[1] == 'S'
1408 && strcmp (*namep, "_SDA_BASE_") == 0
1409 && is_elf_hash_table (info->hash))
1411 /* This is simpler than using _bfd_elf_create_linker_section
1412 (our needs are simpler than ppc's needs). Also
1413 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1414 section already exists a new one is created that follows it which
1415 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1416 struct elf_link_hash_entry *h;
1417 struct bfd_link_hash_entry *bh;
1418 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1420 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1424 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1425 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1427 s = bfd_make_section_anyway (abfd, ".sdata");
1430 bfd_set_section_flags (abfd, s, flags);
1431 bfd_set_section_alignment (abfd, s, 2);
1434 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1435 FALSE, FALSE, FALSE);
1437 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1438 && !(_bfd_generic_link_add_one_symbol (info,
1444 (const char *) NULL,
1446 get_elf_backend_data (abfd)->collect,
1449 h = (struct elf_link_hash_entry *) bh;
1450 h->type = STT_OBJECT;
1453 switch (sym->st_shndx)
1455 case SHN_M32R_SCOMMON:
1456 *secp = bfd_make_section_old_way (abfd, ".scommon");
1457 (*secp)->flags |= SEC_IS_COMMON;
1458 *valp = sym->st_size;
1465 /* We have to figure out the SDA_BASE value, so that we can adjust the
1466 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1467 BFD. If we can't find it, we're stuck. We cache it in the ELF
1468 target data. We don't need to adjust the symbol value for an
1469 external symbol if we are producing relocatable output. */
1471 static bfd_reloc_status_type
1472 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1474 struct bfd_link_info *info;
1475 const char **error_message;
1478 if (elf_gp (output_bfd) == 0)
1480 struct bfd_link_hash_entry *h;
1482 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1483 if (h != (struct bfd_link_hash_entry *) NULL
1484 && h->type == bfd_link_hash_defined)
1485 elf_gp (output_bfd) = (h->u.def.value
1486 + h->u.def.section->output_section->vma
1487 + h->u.def.section->output_offset);
1490 /* Only get the error once. */
1491 *psb = elf_gp (output_bfd) = 4;
1493 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1494 return bfd_reloc_dangerous;
1497 *psb = elf_gp (output_bfd);
1498 return bfd_reloc_ok;
1501 /* Return size of a PLT entry. */
1502 #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1504 /* The m32r linker needs to keep track of the number of relocs that it
1505 decides to copy in check_relocs for each symbol. This is so that
1506 it can discard PC relative relocs if it doesn't need them when
1507 linking with -Bsymbolic. We store the information in a field
1508 extending the regular ELF linker hash table. */
1510 /* This structure keeps track of the number of PC relative relocs we
1511 have copied for a given symbol. */
1513 struct elf_m32r_pcrel_relocs_copied
1516 struct elf_m32r_pcrel_relocs_copied *next;
1517 /* A section in dynobj. */
1519 /* Number of relocs copied in this section. */
1520 bfd_size_type count;
1523 /* The sh linker needs to keep track of the number of relocs that it
1524 decides to copy as dynamic relocs in check_relocs for each symbol.
1525 This is so that it can later discard them if they are found to be
1526 unnecessary. We store the information in a field extending the
1527 regular ELF linker hash table. */
1529 struct elf_m32r_dyn_relocs
1531 struct elf_m32r_dyn_relocs *next;
1533 /* The input section of the reloc. */
1536 /* Total number of relocs copied for the input section. */
1537 bfd_size_type count;
1539 /* Number of pc-relative relocs copied for the input section. */
1540 bfd_size_type pc_count;
1544 /* m32r ELF linker hash entry. */
1546 struct elf_m32r_link_hash_entry
1548 struct elf_link_hash_entry root;
1550 /* Track dynamic relocs copied for this symbol. */
1551 struct elf_m32r_dyn_relocs *dyn_relocs;
1553 // bfd_signed_vma gotplt_refcount;
1555 /* Number of PC relative relocs copied for this symbol. */
1556 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1559 /* m32r ELF linker hash table. */
1561 struct elf_m32r_link_hash_table
1563 struct elf_link_hash_table root;
1565 /* Short-cuts to get to dynamic linker sections. */
1574 /* Small local sym to section mapping cache. */
1575 struct sym_sec_cache sym_sec;
1578 /* Traverse an m32r ELF linker hash table. */
1580 #define m32r_elf_link_hash_traverse(table, func, info) \
1581 (elf_link_hash_traverse \
1583 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1586 /* Get the m32r ELF linker hash table from a link_info structure. */
1589 #define m32r_elf_hash_table(p) \
1590 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1592 /* Create an entry in an m32r ELF linker hash table. */
1593 static struct bfd_hash_entry *
1594 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1597 static struct bfd_hash_entry *
1598 m32r_elf_link_hash_newfunc (entry, table, string)
1599 struct bfd_hash_entry *entry;
1600 struct bfd_hash_table *table;
1603 struct elf_m32r_link_hash_entry *ret =
1604 (struct elf_m32r_link_hash_entry *) entry;
1606 /* Allocate the structure if it has not already been allocated by a
1608 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1609 ret = ((struct elf_m32r_link_hash_entry *)
1610 bfd_hash_allocate (table,
1611 sizeof (struct elf_m32r_link_hash_entry)));
1612 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1613 return (struct bfd_hash_entry *) ret;
1615 /* Call the allocation method of the superclass. */
1616 ret = ((struct elf_m32r_link_hash_entry *)
1617 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1619 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1621 struct elf_m32r_link_hash_entry *eh;
1623 eh = (struct elf_m32r_link_hash_entry *) ret;
1624 eh->dyn_relocs = NULL;
1625 // eh->gotplt_refcount = 0;
1626 /* eh->pcrel_relocs_copied = NULL; FIXME */
1629 return (struct bfd_hash_entry *) ret;
1632 /* Create an m32r ELF linker hash table. */
1633 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1635 static struct bfd_link_hash_table *
1636 m32r_elf_link_hash_table_create (abfd)
1639 struct elf_m32r_link_hash_table *ret;
1640 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
1642 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1643 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1646 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1647 m32r_elf_link_hash_newfunc))
1654 ret->sgotplt = NULL;
1655 ret->srelgot = NULL;
1657 ret->srelplt = NULL;
1658 ret->sdynbss = NULL;
1659 ret->srelbss = NULL;
1660 ret->sym_sec.abfd = NULL;
1662 return &ret->root.root;
1665 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1666 shortcuts to them in our hash table. */
1667 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1670 create_got_section (dynobj, info)
1672 struct bfd_link_info *info;
1674 struct elf_m32r_link_hash_table *htab;
1676 if (! _bfd_elf_create_got_section (dynobj, info))
1679 htab = m32r_elf_hash_table (info);
1680 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1681 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1682 if (! htab->sgot || ! htab->sgotplt)
1685 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1686 if (htab->srelgot == NULL
1687 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1692 | SEC_LINKER_CREATED
1694 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1700 /* Create dynamic sections when linking against a dynamic object. */
1703 m32r_elf_create_dynamic_sections (abfd, info)
1705 struct bfd_link_info *info;
1707 struct elf_m32r_link_hash_table *htab;
1708 flagword flags, pltflags;
1709 register asection *s;
1710 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1711 int ptralign = 2; /* 32bit */
1713 htab = m32r_elf_hash_table (info);
1715 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1716 .rel[a].bss sections. */
1718 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1719 | SEC_LINKER_CREATED);
1722 pltflags |= SEC_CODE;
1723 if (bed->plt_not_loaded)
1724 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1725 if (bed->plt_readonly)
1726 pltflags |= SEC_READONLY;
1728 s = bfd_make_section (abfd, ".plt");
1731 || ! bfd_set_section_flags (abfd, s, pltflags)
1732 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1735 if (bed->want_plt_sym)
1737 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1739 struct bfd_link_hash_entry *bh = NULL;
1740 struct elf_link_hash_entry *h;
1741 if (! (_bfd_generic_link_add_one_symbol
1742 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1743 (bfd_vma) 0, (const char *) NULL, FALSE,
1744 get_elf_backend_data (abfd)->collect, &bh)))
1746 h = (struct elf_link_hash_entry *) bh;
1747 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1748 h->type = STT_OBJECT;
1751 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1755 s = bfd_make_section (abfd,
1756 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1759 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1760 || ! bfd_set_section_alignment (abfd, s, ptralign))
1763 if (htab->sgot == NULL
1764 && ! create_got_section (abfd, info))
1768 const char *secname;
1773 for (sec = abfd->sections; sec; sec = sec->next)
1775 secflags = bfd_get_section_flags (abfd, sec);
1776 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1777 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1779 secname = bfd_get_section_name (abfd, sec);
1780 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1781 strcpy (relname, ".rela");
1782 strcat (relname, secname);
1783 if (bfd_get_section_by_name (abfd, secname))
1785 s = bfd_make_section (abfd, relname);
1787 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1788 || ! bfd_set_section_alignment (abfd, s, ptralign))
1793 if (bed->want_dynbss)
1795 /* The .dynbss section is a place to put symbols which are defined
1796 by dynamic objects, are referenced by regular objects, and are
1797 not functions. We must allocate space for them in the process
1798 image and use a R_*_COPY reloc to tell the dynamic linker to
1799 initialize them at run time. The linker script puts the .dynbss
1800 section into the .bss section of the final image. */
1801 s = bfd_make_section (abfd, ".dynbss");
1804 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1806 /* The .rel[a].bss section holds copy relocs. This section is not
1807 normally needed. We need to create it here, though, so that the
1808 linker will map it to an output section. We can't just create it
1809 only if we need it, because we will not know whether we need it
1810 until we have seen all the input files, and the first time the
1811 main linker code calls BFD after examining all the input files
1812 (size_dynamic_sections) the input sections have already been
1813 mapped to the output sections. If the section turns out not to
1814 be needed, we can discard it later. We will never need this
1815 section when generating a shared object, since they do not use
1819 s = bfd_make_section (abfd,
1820 (bed->default_use_rela_p
1821 ? ".rela.bss" : ".rel.bss"));
1824 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1825 || ! bfd_set_section_alignment (abfd, s, ptralign))
1833 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1834 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1835 struct elf_link_hash_entry *,
1836 struct elf_link_hash_entry *);
1839 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1840 struct elf_link_hash_entry *dir,
1841 struct elf_link_hash_entry *ind)
1843 struct elf_m32r_link_hash_entry *edir, *eind;
1845 edir = (struct elf_m32r_link_hash_entry *) dir;
1846 eind = (struct elf_m32r_link_hash_entry *) ind;
1848 if (eind->dyn_relocs != NULL)
1850 if (edir->dyn_relocs != NULL)
1852 struct elf_m32r_dyn_relocs **pp;
1853 struct elf_m32r_dyn_relocs *p;
1855 if (ind->root.type == bfd_link_hash_indirect)
1858 /* Add reloc counts against the weak sym to the strong sym
1859 list. Merge any entries against the same section. */
1860 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1862 struct elf_m32r_dyn_relocs *q;
1864 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1865 if (q->sec == p->sec)
1867 q->pc_count += p->pc_count;
1868 q->count += p->count;
1875 *pp = edir->dyn_relocs;
1878 edir->dyn_relocs = eind->dyn_relocs;
1879 eind->dyn_relocs = NULL;
1882 // if (ind->root.type == bfd_link_hash_indirect
1883 // && dir->got.refcount <= 0)
1885 // edir->tls_type = eind->tls_type;
1886 // eind->tls_type = GOT_UNKNOWN;
1888 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1892 /* Adjust a symbol defined by a dynamic object and referenced by a
1893 regular object. The current definition is in some section of the
1894 dynamic object, but we're not including those sections. We have to
1895 change the definition to something the rest of the link can
1899 m32r_elf_adjust_dynamic_symbol (info, h)
1900 struct bfd_link_info *info;
1901 struct elf_link_hash_entry *h;
1903 struct elf_m32r_link_hash_table *htab;
1904 struct elf_m32r_link_hash_entry *eh;
1905 struct elf_m32r_dyn_relocs *p;
1908 unsigned int power_of_two;
1911 printf("m32r_elf_adjust_dynamic_symbol()\n");
1914 dynobj = elf_hash_table (info)->dynobj;
1916 /* Make sure we know what is going on here. */
1917 BFD_ASSERT (dynobj != NULL
1918 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1919 || h->weakdef != NULL
1920 || ((h->elf_link_hash_flags
1921 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1922 && (h->elf_link_hash_flags
1923 & ELF_LINK_HASH_REF_REGULAR) != 0
1924 && (h->elf_link_hash_flags
1925 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1928 /* If this is a function, put it in the procedure linkage table. We
1929 will fill in the contents of the procedure linkage table later,
1930 when we know the address of the .got section. */
1931 if (h->type == STT_FUNC
1932 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1935 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1936 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1937 && h->root.type != bfd_link_hash_undefweak
1938 && h->root.type != bfd_link_hash_undefined)
1940 /* This case can occur if we saw a PLT reloc in an input
1941 file, but the symbol was never referred to by a dynamic
1942 object. In such a case, we don't actually need to build
1943 a procedure linkage table, and we can just do a PCREL
1945 h->plt.offset = (bfd_vma) -1;
1946 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1952 h->plt.offset = (bfd_vma) -1;
1954 /* If this is a weak symbol, and there is a real definition, the
1955 processor independent code will have arranged for us to see the
1956 real definition first, and we can just use the same value. */
1957 if (h->weakdef != NULL)
1959 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1960 || h->weakdef->root.type == bfd_link_hash_defweak);
1961 h->root.u.def.section = h->weakdef->root.u.def.section;
1962 h->root.u.def.value = h->weakdef->root.u.def.value;
1966 /* This is a reference to a symbol defined by a dynamic object which
1967 is not a function. */
1969 /* If we are creating a shared library, we must presume that the
1970 only references to the symbol are via the global offset table.
1971 For such cases we need not do anything here; the relocations will
1972 be handled correctly by relocate_section. */
1976 /* If there are no references to this symbol that do not use the
1977 GOT, we don't need to generate a copy reloc. */
1978 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1981 /* If -z nocopyreloc was given, we won't generate them either. */
1982 if (info->nocopyreloc)
1984 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1988 eh = (struct elf_m32r_link_hash_entry *) h;
1989 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1991 s = p->sec->output_section;
1992 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
1996 /* If we didn't find any dynamic relocs in sections which needs the
1997 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2001 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2005 /* We must allocate the symbol in our .dynbss section, which will
2006 become part of the .bss section of the executable. There will be
2007 an entry for this symbol in the .dynsym section. The dynamic
2008 object will contain position independent code, so all references
2009 from the dynamic object to this symbol will go through the global
2010 offset table. The dynamic linker will use the .dynsym entry to
2011 determine the address it must put in the global offset table, so
2012 both the dynamic object and the regular object will refer to the
2013 same memory location for the variable. */
2015 htab = m32r_elf_hash_table (info);
2017 BFD_ASSERT (s != NULL);
2019 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2020 to copy the initial value out of the dynamic object and into the
2021 runtime process image. We need to remember the offset into the
2022 .rela.bss section we are going to use. */
2023 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2027 srel = htab->srelbss;
2028 BFD_ASSERT (srel != NULL);
2029 srel->size += sizeof (Elf32_External_Rela);
2030 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2033 /* We need to figure out the alignment required for this symbol. I
2034 have no idea how ELF linkers handle this. */
2035 power_of_two = bfd_log2 (h->size);
2036 if (power_of_two > 3)
2039 /* Apply the required alignment. */
2040 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2041 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2043 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2047 /* Define the symbol as being at this point in the section. */
2048 h->root.u.def.section = s;
2049 h->root.u.def.value = s->size;
2051 /* Increment the section size to make room for the symbol. */
2057 /* Allocate space in .plt, .got and associated reloc sections for
2061 allocate_dynrelocs (h, inf)
2062 struct elf_link_hash_entry *h;
2065 struct bfd_link_info *info;
2066 struct elf_m32r_link_hash_table *htab;
2067 struct elf_m32r_link_hash_entry *eh;
2068 struct elf_m32r_dyn_relocs *p;
2070 if (h->root.type == bfd_link_hash_indirect)
2073 if (h->root.type == bfd_link_hash_warning)
2074 /* When warning symbols are created, they **replace** the "real"
2075 entry in the hash table, thus we never get to see the real
2076 symbol in a hash traversal. So look at it now. */
2077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2079 info = (struct bfd_link_info *) inf;
2080 htab = m32r_elf_hash_table (info);
2082 eh = (struct elf_m32r_link_hash_entry *) h;
2083 // if ((h->got.refcount > 0
2084 // || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2085 // && eh->gotplt_refcount > 0)
2087 // /* The symbol has been forced local, or we have some direct got refs,
2088 // so treat all the gotplt refs as got refs. */
2089 // h->got.refcount += eh->gotplt_refcount;
2090 // if (h->plt.refcount >= eh->gotplt_refcount)
2091 // h->plt.refcount -= eh->gotplt_refcount;
2094 if (htab->root.dynamic_sections_created
2095 && h->plt.refcount > 0)
2097 /* Make sure this symbol is output as a dynamic symbol.
2098 Undefined weak syms won't yet be marked as dynamic. */
2099 if (h->dynindx == -1
2100 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2102 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2106 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2108 asection *s = htab->splt;
2110 /* If this is the first .plt entry, make room for the special
2113 s->size += PLT_ENTRY_SIZE;
2115 h->plt.offset = s->size;
2117 /* If this symbol is not defined in a regular file, and we are
2118 not generating a shared library, then set the symbol to this
2119 location in the .plt. This is required to make function
2120 pointers compare as equal between the normal executable and
2121 the shared library. */
2123 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2125 h->root.u.def.section = s;
2126 h->root.u.def.value = h->plt.offset;
2129 /* Make room for this entry. */
2130 s->size += PLT_ENTRY_SIZE;
2132 /* We also need to make an entry in the .got.plt section, which
2133 will be placed in the .got section by the linker script. */
2134 htab->sgotplt->size += 4;
2136 /* We also need to make an entry in the .rel.plt section. */
2137 htab->srelplt->size += sizeof (Elf32_External_Rela);
2141 h->plt.offset = (bfd_vma) -1;
2142 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2147 h->plt.offset = (bfd_vma) -1;
2148 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2151 if (h->got.refcount > 0)
2156 /* Make sure this symbol is output as a dynamic symbol.
2157 Undefined weak syms won't yet be marked as dynamic. */
2158 if (h->dynindx == -1
2159 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2161 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2167 h->got.offset = s->size;
2169 dyn = htab->root.dynamic_sections_created;
2170 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2171 htab->srelgot->size += sizeof (Elf32_External_Rela);
2174 h->got.offset = (bfd_vma) -1;
2176 if (eh->dyn_relocs == NULL)
2179 /* In the shared -Bsymbolic case, discard space allocated for
2180 dynamic pc-relative relocs against symbols which turn out to be
2181 defined in regular objects. For the normal shared case, discard
2182 space for pc-relative relocs that have become local due to symbol
2183 visibility changes. */
2187 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2188 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2191 struct elf_m32r_dyn_relocs **pp;
2192 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2194 p->count -= p->pc_count;
2205 /* For the non-shared case, discard space for relocs against
2206 symbols which turn out to need copy relocs or are not
2209 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2210 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2211 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2212 || (htab->root.dynamic_sections_created
2213 && (h->root.type == bfd_link_hash_undefweak
2214 || h->root.type == bfd_link_hash_undefined))))
2216 /* Make sure this symbol is output as a dynamic symbol.
2217 Undefined weak syms won't yet be marked as dynamic. */
2218 if (h->dynindx == -1
2219 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2221 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2225 /* If that succeeded, we know we'll be keeping all the
2227 if (h->dynindx != -1)
2231 eh->dyn_relocs = NULL;
2236 /* Finally, allocate space. */
2237 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2239 asection *sreloc = elf_section_data (p->sec)->sreloc;
2240 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2245 /* Find any dynamic relocs that apply to read-only sections. */
2248 readonly_dynrelocs (h, inf)
2249 struct elf_link_hash_entry *h;
2252 struct elf_m32r_link_hash_entry *eh;
2253 struct elf_m32r_dyn_relocs *p;
2255 if (h->root.type == bfd_link_hash_warning)
2256 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2258 eh = (struct elf_m32r_link_hash_entry *) h;
2259 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2261 asection *s = p->sec->output_section;
2263 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2265 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2267 info->flags |= DF_TEXTREL;
2269 /* Not an error, just cut short the traversal. */
2276 /* Set the sizes of the dynamic sections. */
2279 m32r_elf_size_dynamic_sections (output_bfd, info)
2280 bfd *output_bfd ATTRIBUTE_UNUSED;
2281 struct bfd_link_info *info;
2283 struct elf_m32r_link_hash_table *htab;
2290 printf("m32r_elf_size_dynamic_sections()\n");
2293 htab = m32r_elf_hash_table (info);
2294 dynobj = htab->root.dynobj;
2295 BFD_ASSERT (dynobj != NULL);
2297 if (htab->root.dynamic_sections_created)
2299 /* Set the contents of the .interp section to the interpreter. */
2302 s = bfd_get_section_by_name (dynobj, ".interp");
2303 BFD_ASSERT (s != NULL);
2304 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2305 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2309 /* Set up .got offsets for local syms, and space for local dynamic
2311 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2313 bfd_signed_vma *local_got;
2314 bfd_signed_vma *end_local_got;
2315 bfd_size_type locsymcount;
2316 Elf_Internal_Shdr *symtab_hdr;
2319 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2322 for (s = ibfd->sections; s != NULL; s = s->next)
2324 struct elf_m32r_dyn_relocs *p;
2326 for (p = ((struct elf_m32r_dyn_relocs *)
2327 elf_section_data (s)->local_dynrel);
2331 if (! bfd_is_abs_section (p->sec)
2332 && bfd_is_abs_section (p->sec->output_section))
2334 /* Input section has been discarded, either because
2335 it is a copy of a linkonce section or due to
2336 linker script /DISCARD/, so we'll be discarding
2339 else if (p->count != 0)
2341 srel = elf_section_data (p->sec)->sreloc;
2342 srel->size += p->count * sizeof (Elf32_External_Rela);
2343 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2344 info->flags |= DF_TEXTREL;
2349 local_got = elf_local_got_refcounts (ibfd);
2353 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2354 locsymcount = symtab_hdr->sh_info;
2355 end_local_got = local_got + locsymcount;
2357 srel = htab->srelgot;
2358 for (; local_got < end_local_got; ++local_got)
2362 *local_got = s->size;
2365 srel->size += sizeof (Elf32_External_Rela);
2368 *local_got = (bfd_vma) -1;
2372 /* Allocate global sym .plt and .got entries, and space for global
2373 sym dynamic relocs. */
2374 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2376 /* We now have determined the sizes of the various dynamic sections.
2377 Allocate memory for them. */
2379 for (s = dynobj->sections; s != NULL; s = s->next)
2381 if ((s->flags & SEC_LINKER_CREATED) == 0)
2386 || s == htab->sgotplt)
2388 /* Strip this section if we don't need it; see the
2391 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2393 if (s->size != 0 && s != htab->srelplt)
2396 /* We use the reloc_count field as a counter if we need
2397 to copy relocs into the output file. */
2402 /* It's not one of our sections, so don't allocate space. */
2408 /* If we don't need this section, strip it from the
2409 output file. This is mostly to handle .rela.bss and
2410 .rela.plt. We must create both sections in
2411 create_dynamic_sections, because they must be created
2412 before the linker maps input sections to output
2413 sections. The linker does that before
2414 adjust_dynamic_symbol is called, and it is that
2415 function which decides whether anything needs to go
2416 into these sections. */
2417 _bfd_strip_section_from_output (info, s);
2421 /* Allocate memory for the section contents. We use bfd_zalloc
2422 here in case unused entries are not reclaimed before the
2423 section's contents are written out. This should not happen,
2424 but this way if it does, we get a R_M32R_NONE reloc instead
2426 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2427 if (s->contents == NULL)
2431 if (htab->root.dynamic_sections_created)
2433 /* Add some entries to the .dynamic section. We fill in the
2434 values later, in m32r_elf_finish_dynamic_sections, but we
2435 must add the entries now so that we get the correct size for
2436 the .dynamic section. The DT_DEBUG entry is filled in by the
2437 dynamic linker and used by the debugger. */
2438 #define add_dynamic_entry(TAG, VAL) \
2439 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2443 if (! add_dynamic_entry (DT_DEBUG, 0))
2447 if (htab->splt->size != 0)
2449 if (! add_dynamic_entry (DT_PLTGOT, 0)
2450 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2451 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2452 || ! add_dynamic_entry (DT_JMPREL, 0))
2458 if (! add_dynamic_entry (DT_RELA, 0)
2459 || ! add_dynamic_entry (DT_RELASZ, 0)
2460 || ! add_dynamic_entry (DT_RELAENT,
2461 sizeof (Elf32_External_Rela)))
2464 /* If any dynamic relocs apply to a read-only section,
2465 then we need a DT_TEXTREL entry. */
2466 if ((info->flags & DF_TEXTREL) == 0)
2467 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2470 if ((info->flags & DF_TEXTREL) != 0)
2472 if (! add_dynamic_entry (DT_TEXTREL, 0))
2477 #undef add_dynamic_entry
2481 /* Relocate an M32R/D ELF section.
2482 There is some attempt to make this function usable for many architectures,
2483 both for RELA and REL type relocs, if only to serve as a learning tool.
2485 The RELOCATE_SECTION function is called by the new ELF backend linker
2486 to handle the relocations for a section.
2488 The relocs are always passed as Rela structures; if the section
2489 actually uses Rel structures, the r_addend field will always be
2492 This function is responsible for adjust the section contents as
2493 necessary, and (if using Rela relocs and generating a
2494 relocatable output file) adjusting the reloc addend as
2497 This function does not have to worry about setting the reloc
2498 address or the reloc symbol index.
2500 LOCAL_SYMS is a pointer to the swapped in local symbols.
2502 LOCAL_SECTIONS is an array giving the section in the input file
2503 corresponding to the st_shndx field of each local symbol.
2505 The global hash table entry for the global symbols can be found
2506 via elf_sym_hashes (input_bfd).
2508 When generating relocatable output, this function must handle
2509 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2510 going to be the section symbol corresponding to the output
2511 section, which means that the addend must be adjusted
2515 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2516 contents, relocs, local_syms, local_sections)
2517 bfd *output_bfd ATTRIBUTE_UNUSED;
2518 struct bfd_link_info *info;
2520 asection *input_section;
2522 Elf_Internal_Rela *relocs;
2523 Elf_Internal_Sym *local_syms;
2524 asection **local_sections;
2526 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2527 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2528 Elf_Internal_Rela *rel, *relend;
2529 /* Assume success. */
2530 bfd_boolean ret = TRUE;
2532 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2534 bfd_vma *local_got_offsets;
2535 asection *sgot, *splt, *sreloc;
2537 dynobj = htab->root.dynobj;
2538 local_got_offsets = elf_local_got_offsets (input_bfd);
2545 relend = relocs + input_section->reloc_count;
2546 for (; rel < relend; rel++)
2549 reloc_howto_type *howto;
2550 unsigned long r_symndx;
2551 struct elf_link_hash_entry *h;
2552 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2553 ensure it's zero (we use REL relocs, not RELA). Therefore this
2554 should be assigning zero to `addend', but for clarity we use
2556 bfd_vma addend = rel->r_addend;
2557 bfd_vma offset = rel->r_offset;
2558 Elf_Internal_Sym *sym;
2560 const char *sym_name;
2561 bfd_reloc_status_type r;
2562 const char *errmsg = NULL;
2563 bfd_boolean use_rel = FALSE;
2566 r_type = ELF32_R_TYPE (rel->r_info);
2567 if (r_type < 0 || r_type >= (int) R_M32R_max)
2569 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2570 bfd_archive_filename (input_bfd),
2572 bfd_set_error (bfd_error_bad_value);
2577 if (r_type == R_M32R_GNU_VTENTRY
2578 || r_type == R_M32R_GNU_VTINHERIT
2579 || r_type == R_M32R_NONE
2580 || r_type == R_M32R_RELA_GNU_VTENTRY
2581 || r_type == R_M32R_RELA_GNU_VTINHERIT)
2584 if (r_type <= R_M32R_GNU_VTENTRY)
2587 howto = m32r_elf_howto_table + r_type;
2588 r_symndx = ELF32_R_SYM (rel->r_info);
2590 if (info->relocatable && (use_rel == TRUE))
2592 /* This is a relocatable link. We don't have to change
2593 anything, unless the reloc is against a section symbol,
2594 in which case we have to adjust according to where the
2595 section symbol winds up in the output section. */
2597 if (r_symndx >= symtab_hdr->sh_info)
2599 /* External symbol. */
2604 sym = local_syms + r_symndx;
2605 sym_name = "<local symbol>";
2606 /* STT_SECTION: symbol is associated with a section. */
2607 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2609 /* Symbol isn't associated with a section. Nothing to do. */
2613 sec = local_sections[r_symndx];
2614 addend += sec->output_offset + sym->st_value;
2616 /* If partial_inplace, we need to store any additional addend
2617 back in the section. */
2618 if (! howto->partial_inplace)
2620 /* ??? Here is a nice place to call a special_function
2622 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2623 r = _bfd_relocate_contents (howto, input_bfd,
2624 addend, contents + offset);
2627 Elf_Internal_Rela *lorel;
2629 /* We allow an arbitrary number of HI16 relocs before the
2630 LO16 reloc. This permits gcc to emit the HI and LO relocs
2632 for (lorel = rel + 1;
2634 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2635 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2639 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2641 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2646 r = _bfd_relocate_contents (howto, input_bfd,
2647 addend, contents + offset);
2655 /* This is a final link. */
2660 if (r_symndx < symtab_hdr->sh_info)
2663 sym = local_syms + r_symndx;
2664 sec = local_sections[r_symndx];
2665 sym_name = "<local symbol>";
2667 if (use_rel == FALSE)
2669 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2670 addend = rel->r_addend;
2672 if (info->relocatable)
2674 /* This is a relocatable link. We don't have to change
2675 anything, unless the reloc is against a section symbol,
2676 in which case we have to adjust according to where the
2677 section symbol winds up in the output section. */
2678 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2679 rel->r_addend += sec->output_offset + sym->st_value;
2686 relocation = (sec->output_section->vma
2687 + sec->output_offset
2693 /* External symbol. */
2694 if (info->relocatable && (use_rel == FALSE))
2697 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2698 while (h->root.type == bfd_link_hash_indirect
2699 || h->root.type == bfd_link_hash_warning)
2700 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2701 sym_name = h->root.root.string;
2703 if (h->root.type == bfd_link_hash_defined
2704 || h->root.type == bfd_link_hash_defweak)
2707 sec = h->root.u.def.section;
2709 dyn = htab->root.dynamic_sections_created;
2710 sec = h->root.u.def.section;
2711 if (r_type == R_M32R_GOTPC24
2712 || (r_type == R_M32R_GOTPC_HI_ULO
2713 || r_type == R_M32R_GOTPC_HI_SLO
2714 || r_type == R_M32R_GOTPC_LO)
2715 || (r_type == R_M32R_26_PLTREL
2716 && h->plt.offset != (bfd_vma) -1)
2717 || ((r_type == R_M32R_GOT24
2718 || r_type == R_M32R_GOT16_HI_ULO
2719 || r_type == R_M32R_GOT16_HI_SLO
2720 || r_type == R_M32R_GOT16_LO)
2721 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2724 || (! info->symbolic && h->dynindx != -1)
2725 || (h->elf_link_hash_flags
2726 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2728 && ((! info->symbolic && h->dynindx != -1)
2729 || (h->elf_link_hash_flags
2730 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2731 && (((r_type == R_M32R_16_RELA
2732 || r_type == R_M32R_32_RELA
2733 || r_type == R_M32R_24_RELA
2734 || r_type == R_M32R_HI16_ULO_RELA
2735 || r_type == R_M32R_HI16_SLO_RELA
2736 || r_type == R_M32R_LO16_RELA)
2737 && (h->elf_link_hash_flags
2738 & ELF_LINK_FORCED_LOCAL) == 0)
2739 || r_type == R_M32R_10_PCREL_RELA
2740 || r_type == R_M32R_18_PCREL_RELA
2741 || r_type == R_M32R_26_PCREL_RELA)
2742 && ((input_section->flags & SEC_ALLOC) != 0
2743 /* DWARF will emit R_M32R_16(24,32) relocations
2744 in its sections against symbols defined
2745 externally in shared libraries. We can't do
2746 anything with them here. */
2747 || ((input_section->flags & SEC_DEBUGGING) != 0
2748 && (h->elf_link_hash_flags
2749 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
2751 /* In these cases, we don't need the relocation
2752 value. We check specially because in some
2753 obscure cases sec->output_section will be NULL. */
2756 else if (sec->output_section == NULL)
2758 (*_bfd_error_handler)
2759 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2760 bfd_get_filename (input_bfd), h->root.root.string,
2761 bfd_get_section_name (input_bfd, input_section));
2766 relocation = (h->root.u.def.value
2767 + sec->output_section->vma
2768 + sec->output_offset);
2770 else if (h->root.type == bfd_link_hash_undefweak)
2772 else if (info->unresolved_syms_in_objects == RM_IGNORE
2773 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2777 if (! ((*info->callbacks->undefined_symbol)
2778 (info, h->root.root.string, input_bfd,
2779 input_section, offset,
2780 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2781 || ELF_ST_VISIBILITY (h->other)))))
2787 /* Sanity check the address. */
2788 sz = (input_section->rawsize
2789 ? input_section->rawsize
2790 : input_section->size);
2791 if (offset > input_section->size)
2793 r = bfd_reloc_outofrange;
2797 switch ((int) r_type)
2799 case R_M32R_GOTPC24:
2800 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2801 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2803 relocation = sgot->output_section->vma;
2806 case R_M32R_GOTPC_HI_ULO:
2807 case R_M32R_GOTPC_HI_SLO:
2808 case R_M32R_GOTPC_LO:
2810 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2812 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2813 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2816 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2817 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2819 relocation = sgot->output_section->vma;
2820 relocation -= (input_section->output_section->vma
2821 + input_section->output_offset
2823 if ((r_type == R_M32R_GOTPC_HI_SLO)
2824 && ((relocation + rel->r_addend) & 0x8000))
2825 rel->r_addend += 0x10000;
2829 case R_M32R_GOT16_HI_ULO:
2830 case R_M32R_GOT16_HI_SLO:
2831 case R_M32R_GOT16_LO:
2834 /* Relocation is to the entry for this symbol in the global
2836 BFD_ASSERT (sgot != NULL);
2843 off = h->got.offset;
2844 BFD_ASSERT (off != (bfd_vma) -1);
2846 dyn = htab->root.dynamic_sections_created;
2847 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2851 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2852 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2854 /* This is actually a static link, or it is a
2855 -Bsymbolic link and the symbol is defined
2856 locally, or the symbol was forced to be local
2857 because of a version file. We must initialize
2858 this entry in the global offset table. Since the
2859 offset must always be a multiple of 4, we use the
2860 least significant bit to record whether we have
2861 initialized it already.
2863 When doing a dynamic link, we create a .rela.got
2864 relocation entry to initialize the value. This
2865 is done in the finish_dynamic_symbol routine. */
2870 bfd_put_32 (output_bfd, relocation,
2871 sgot->contents + off);
2876 relocation = sgot->output_offset + off;
2883 BFD_ASSERT (local_got_offsets != NULL
2884 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2886 off = local_got_offsets[r_symndx];
2888 /* The offset must always be a multiple of 4. We use
2889 the least significant bit to record whether we have
2890 already processed this entry. */
2895 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2900 Elf_Internal_Rela outrel;
2902 /* We need to generate a R_M32R_RELATIVE reloc
2903 for the dynamic linker. */
2904 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2905 BFD_ASSERT (srelgot != NULL);
2907 outrel.r_offset = (sgot->output_section->vma
2908 + sgot->output_offset
2910 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2911 outrel.r_addend = relocation;
2912 loc = srelgot->contents;
2913 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2914 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2915 ++srelgot->reloc_count;
2918 local_got_offsets[r_symndx] |= 1;
2921 relocation = sgot->output_offset + off;
2923 if ((r_type == R_M32R_GOT16_HI_SLO)
2924 && ((relocation + rel->r_addend) & 0x8000))
2925 rel->r_addend += 0x10000;
2929 case R_M32R_26_PLTREL:
2930 /* Relocation is to the entry for this symbol in the
2931 procedure linkage table. */
2933 /* The native assembler will generate a 26_PLTREL reloc
2934 for a local symbol if you assemble a call from one
2935 section to another when using -K pic. */
2939 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2940 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2942 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2945 if (h->plt.offset == (bfd_vma) -1)
2947 /* We didn't make a PLT entry for this symbol. This
2948 happens when statically linking PIC code, or when
2949 using -Bsymbolic. */
2953 relocation = (splt->output_section->vma
2954 + splt->output_offset
2958 case R_M32R_HI16_SLO_RELA:
2960 if ((relocation + rel->r_addend) & 0x8000)
2962 rel->r_addend += 0x10000;
2966 case R_M32R_16_RELA:
2967 case R_M32R_24_RELA:
2968 case R_M32R_32_RELA:
2969 case R_M32R_18_PCREL_RELA:
2970 case R_M32R_26_PCREL_RELA:
2971 case R_M32R_HI16_ULO_RELA:
2972 case R_M32R_LO16_RELA:
2973 case R_M32R_SDA16_RELA:
2976 && (input_section->flags & SEC_ALLOC) != 0
2977 && ((r_type != R_M32R_18_PCREL_RELA
2978 && r_type != R_M32R_26_PCREL_RELA)
2981 && (! info->symbolic
2982 || (h->elf_link_hash_flags
2983 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2985 Elf_Internal_Rela outrel;
2986 bfd_boolean skip, relocate;
2989 /* When generating a shared object, these relocations
2990 are copied into the output file to be resolved at run
2997 name = (bfd_elf_string_from_elf_section
2999 elf_elfheader (input_bfd)->e_shstrndx,
3000 elf_section_data (input_section)->rel_hdr.sh_name));
3004 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3005 && strcmp (bfd_get_section_name (input_bfd,
3009 sreloc = bfd_get_section_by_name (dynobj, name);
3010 BFD_ASSERT (sreloc != NULL);
3016 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3020 if (outrel.r_offset == (bfd_vma) -1)
3022 else if (outrel.r_offset == (bfd_vma) -2)
3023 skip = TRUE, relocate = TRUE;
3024 outrel.r_offset += (input_section->output_section->vma
3025 + input_section->output_offset);
3028 memset (&outrel, 0, sizeof outrel);
3029 else if (r_type == R_M32R_18_PCREL_RELA
3030 || r_type == R_M32R_26_PCREL_RELA)
3032 BFD_ASSERT (h != NULL && h->dynindx != -1);
3033 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3034 outrel.r_addend = rel->r_addend;
3038 /* h->dynindx may be -1 if this symbol was marked to
3041 || ((info->symbolic || h->dynindx == -1)
3042 && (h->elf_link_hash_flags
3043 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3046 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3047 outrel.r_addend = relocation + rel->r_addend;
3051 BFD_ASSERT (h->dynindx != -1);
3052 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3053 outrel.r_addend = relocation + rel->r_addend;
3057 loc = sreloc->contents;
3058 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3059 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3060 ++sreloc->reloc_count;
3062 /* If this reloc is against an external symbol, we do
3063 not want to fiddle with the addend. Otherwise, we
3064 need to include the symbol value so that it becomes
3065 an addend for the dynamic reloc. */
3071 case (int) R_M32R_10_PCREL :
3072 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3074 sec, relocation, addend);
3077 case (int) R_M32R_HI16_SLO :
3078 case (int) R_M32R_HI16_ULO :
3080 Elf_Internal_Rela *lorel;
3082 /* We allow an arbitrary number of HI16 relocs before the
3083 LO16 reloc. This permits gcc to emit the HI and LO relocs
3085 for (lorel = rel + 1;
3087 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3088 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3092 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3094 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3095 contents, relocation + addend);
3099 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3101 relocation, addend);
3106 case (int) R_M32R_SDA16 :
3110 BFD_ASSERT (sec != NULL);
3111 name = bfd_get_section_name (abfd, sec);
3113 if (strcmp (name, ".sdata") == 0
3114 || strcmp (name, ".sbss") == 0
3115 || strcmp (name, ".scommon") == 0)
3118 bfd *out_bfd = sec->output_section->owner;
3120 r = m32r_elf_final_sda_base (out_bfd, info,
3123 if (r != bfd_reloc_ok)
3129 /* At this point `relocation' contains the object's
3131 relocation -= sda_base;
3132 /* Now it contains the offset from _SDA_BASE_. */
3136 (*_bfd_error_handler)
3137 (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
3138 bfd_archive_filename (input_bfd),
3140 m32r_elf_howto_table[(int) r_type].name,
3141 bfd_get_section_name (abfd, sec));
3142 /*bfd_set_error (bfd_error_bad_value); ??? why? */
3149 default : /* OLD_M32R_RELOC */
3151 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3153 relocation, addend);
3157 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3158 contents, rel->r_offset,
3159 relocation, rel->r_addend);
3165 if (r != bfd_reloc_ok)
3167 /* FIXME: This should be generic enough to go in a utility. */
3171 name = h->root.root.string;
3174 name = (bfd_elf_string_from_elf_section
3175 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3176 if (name == NULL || *name == '\0')
3177 name = bfd_section_name (input_bfd, sec);
3185 case bfd_reloc_overflow:
3186 if (! ((*info->callbacks->reloc_overflow)
3187 (info, name, howto->name, (bfd_vma) 0,
3188 input_bfd, input_section, offset)))
3192 case bfd_reloc_undefined:
3193 if (! ((*info->callbacks->undefined_symbol)
3194 (info, name, input_bfd, input_section,
3199 case bfd_reloc_outofrange:
3200 errmsg = _("internal error: out of range error");
3203 case bfd_reloc_notsupported:
3204 errmsg = _("internal error: unsupported relocation error");
3207 case bfd_reloc_dangerous:
3208 errmsg = _("internal error: dangerous error");
3212 errmsg = _("internal error: unknown error");
3216 if (!((*info->callbacks->warning)
3217 (info, errmsg, name, input_bfd, input_section,
3228 /* Finish up dynamic symbol handling. We set the contents of various
3229 dynamic sections here. */
3231 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3233 struct bfd_link_info *info;
3234 struct elf_link_hash_entry *h;
3235 Elf_Internal_Sym *sym;
3237 struct elf_m32r_link_hash_table *htab;
3242 printf("m32r_elf_finish_dynamic_symbol()\n");
3245 htab = m32r_elf_hash_table (info);
3246 dynobj = htab->root.dynobj;
3248 if (h->plt.offset != (bfd_vma) -1)
3256 Elf_Internal_Rela rela;
3258 /* This symbol has an entry in the procedure linkage table. Set
3261 BFD_ASSERT (h->dynindx != -1);
3264 sgot = htab->sgotplt;
3265 srela = htab->srelplt;
3266 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3268 /* Get the index in the procedure linkage table which
3269 corresponds to this symbol. This is the index of this symbol
3270 in all the symbols for which we are making plt entries. The
3271 first entry in the procedure linkage table is reserved. */
3272 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3274 /* Get the offset into the .got table of the entry that
3275 corresponds to this function. Each .got entry is 4 bytes.
3276 The first three are reserved. */
3277 got_offset = (plt_index + 3) * 4;
3279 /* Fill in the entry in the procedure linkage table. */
3282 bfd_put_32 (output_bfd,
3284 + (((sgot->output_section->vma
3285 + sgot->output_offset
3286 + got_offset) >> 16) & 0xffff)),
3287 splt->contents + h->plt.offset);
3288 bfd_put_32 (output_bfd,
3290 + ((sgot->output_section->vma
3291 + sgot->output_offset
3292 + got_offset) & 0xffff)),
3293 splt->contents + h->plt.offset + 4);
3294 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3295 splt->contents + h->plt.offset + 8);
3296 bfd_put_32 (output_bfd,
3298 + plt_index * sizeof (Elf32_External_Rela)),
3299 splt->contents + h->plt.offset + 12);
3300 bfd_put_32 (output_bfd,
3302 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3303 splt->contents + h->plt.offset + 16);
3307 bfd_put_32 (output_bfd,
3308 PLT_ENTRY_WORD0 + got_offset,
3309 splt->contents + h->plt.offset);
3310 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3311 splt->contents + h->plt.offset + 4);
3312 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3313 splt->contents + h->plt.offset + 8);
3314 bfd_put_32 (output_bfd,
3316 + plt_index * sizeof (Elf32_External_Rela)),
3317 splt->contents + h->plt.offset + 12);
3318 bfd_put_32 (output_bfd,
3320 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3321 splt->contents + h->plt.offset + 16);
3324 /* Fill in the entry in the global offset table. */
3325 bfd_put_32 (output_bfd,
3326 (splt->output_section->vma
3327 + splt->output_offset
3329 + 12), /* same offset */
3330 sgot->contents + got_offset);
3332 /* Fill in the entry in the .rela.plt section. */
3333 rela.r_offset = (sgot->output_section->vma
3334 + sgot->output_offset
3336 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3338 loc = srela->contents;
3339 loc += plt_index * sizeof(Elf32_External_Rela);
3340 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3342 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3344 /* Mark the symbol as undefined, rather than as defined in
3345 the .plt section. Leave the value alone. */
3346 sym->st_shndx = SHN_UNDEF;
3350 if (h->got.offset != (bfd_vma) -1)
3354 Elf_Internal_Rela rela;
3356 /* This symbol has an entry in the global offset table. Set it
3360 srela = htab->srelgot;
3361 BFD_ASSERT (sgot != NULL && srela != NULL);
3363 rela.r_offset = (sgot->output_section->vma
3364 + sgot->output_offset
3365 + (h->got.offset &~ 1));
3367 /* If this is a -Bsymbolic link, and the symbol is defined
3368 locally, we just want to emit a RELATIVE reloc. Likewise if
3369 the symbol was forced to be local because of a version file.
3370 The entry in the global offset table will already have been
3371 initialized in the relocate_section function. */
3375 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3376 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3378 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3379 rela.r_addend = (h->root.u.def.value
3380 + h->root.u.def.section->output_section->vma
3381 + h->root.u.def.section->output_offset);
3385 BFD_ASSERT((h->got.offset & 1) == 0);
3386 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3387 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3391 loc = srela->contents;
3392 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3393 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3394 ++srela->reloc_count;
3397 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3400 Elf_Internal_Rela rela;
3402 /* This symbols needs a copy reloc. Set it up. */
3404 BFD_ASSERT (h->dynindx != -1
3405 && (h->root.type == bfd_link_hash_defined
3406 || h->root.type == bfd_link_hash_defweak));
3408 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3410 BFD_ASSERT (s != NULL);
3412 rela.r_offset = (h->root.u.def.value
3413 + h->root.u.def.section->output_section->vma
3414 + h->root.u.def.section->output_offset);
3415 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3418 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3419 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3423 /* Mark some specially defined symbols as absolute. */
3424 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3425 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3426 sym->st_shndx = SHN_ABS;
3432 /* Finish up the dynamic sections. */
3435 m32r_elf_finish_dynamic_sections (output_bfd, info)
3437 struct bfd_link_info *info;
3439 struct elf_m32r_link_hash_table *htab;
3445 printf("m32r_elf_finish_dynamic_sections()\n");
3448 htab = m32r_elf_hash_table (info);
3449 dynobj = htab->root.dynobj;
3451 sgot = htab->sgotplt;
3452 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3454 if (htab->root.dynamic_sections_created)
3457 Elf32_External_Dyn *dyncon, *dynconend;
3459 BFD_ASSERT (sgot != NULL && sdyn != NULL);
3461 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3462 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3464 for (; dyncon < dynconend; dyncon++)
3466 Elf_Internal_Dyn dyn;
3470 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3479 s = htab->sgot->output_section;
3483 s = htab->srelplt->output_section;
3485 BFD_ASSERT (s != NULL);
3486 dyn.d_un.d_ptr = s->vma;
3487 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3491 s = htab->srelplt->output_section;
3492 BFD_ASSERT (s != NULL);
3493 dyn.d_un.d_val = s->size;
3494 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3498 /* My reading of the SVR4 ABI indicates that the
3499 procedure linkage table relocs (DT_JMPREL) should be
3500 included in the overall relocs (DT_RELA). This is
3501 what Solaris does. However, UnixWare can not handle
3502 that case. Therefore, we override the DT_RELASZ entry
3503 here to make it not include the JMPREL relocs. Since
3504 the linker script arranges for .rela.plt to follow all
3505 other relocation sections, we don't have to worry
3506 about changing the DT_RELA entry. */
3507 if (htab->srelplt != NULL)
3509 s = htab->srelplt->output_section;
3510 dyn.d_un.d_val -= s->size;
3512 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3517 /* Fill in the first entry in the procedure linkage table. */
3519 if (splt && splt->size > 0)
3523 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3524 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3525 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3526 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3527 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3532 /* addr = .got + 4 */
3533 addr = sgot->output_section->vma + sgot->output_offset + 4;
3534 bfd_put_32 (output_bfd,
3535 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3537 bfd_put_32 (output_bfd,
3538 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3539 splt->contents + 4);
3540 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3541 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3542 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3545 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3550 /* Fill in the first three entries in the global offset table. */
3551 if (sgot && sgot->size > 0)
3554 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3556 bfd_put_32 (output_bfd,
3557 sdyn->output_section->vma + sdyn->output_offset,
3559 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3560 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3562 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3569 #if 0 /* relaxing not supported yet */
3571 /* This function handles relaxing for the m32r.
3572 Relaxing on the m32r is tricky because of instruction alignment
3573 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3575 The following relaxing opportunities are handled:
3577 seth/add3/jl -> bl24 or bl8
3580 It would be nice to handle bl24 -> bl8 but given:
3582 - 4 byte insns must be on 4 byte boundaries
3583 - branch instructions only branch to insns on 4 byte boundaries
3585 this isn't much of a win because the insn in the 2 "deleted" bytes
3586 must become a nop. With some complexity some real relaxation could be
3587 done but the frequency just wouldn't make it worth it; it's better to
3588 try to do all the code compaction one can elsewhere.
3589 When the chip supports parallel 16 bit insns, things may change.
3593 m32r_elf_relax_section (abfd, sec, link_info, again)
3596 struct bfd_link_info *link_info;
3599 Elf_Internal_Shdr *symtab_hdr;
3600 /* The Rela structures are used here because that's what
3601 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
3603 Elf_Internal_Rela *internal_relocs = NULL;
3604 Elf_Internal_Rela *irel, *irelend;
3605 bfd_byte *contents = NULL;
3606 Elf_Internal_Sym *isymbuf = NULL;
3608 /* Assume nothing changes. */
3611 /* We don't have to do anything for a relocatable link, if
3612 this section does not have relocs, or if this is not a
3614 if (link_info->relocatable
3615 || (sec->flags & SEC_RELOC) == 0
3616 || sec->reloc_count == 0
3617 || (sec->flags & SEC_CODE) == 0
3618 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
3621 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3623 /* Get a copy of the native relocations. */
3624 internal_relocs = (_bfd_elf_link_read_relocs
3625 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3626 link_info->keep_memory));
3627 if (internal_relocs == NULL)
3630 /* Walk through them looking for relaxing opportunities. */
3631 irelend = internal_relocs + sec->reloc_count;
3632 for (irel = internal_relocs; irel < irelend; irel++)
3636 /* If this isn't something that can be relaxed, then ignore
3638 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3641 /* Get the section contents if we haven't done so already. */
3642 if (contents == NULL)
3644 /* Get cached copy if it exists. */
3645 if (elf_section_data (sec)->this_hdr.contents != NULL)
3646 contents = elf_section_data (sec)->this_hdr.contents;
3649 /* Go get them off disk. */
3650 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3655 /* Read this BFD's local symbols if we haven't done so already. */
3656 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3658 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3659 if (isymbuf == NULL)
3660 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3661 symtab_hdr->sh_info, 0,
3663 if (isymbuf == NULL)
3667 /* Get the value of the symbol referred to by the reloc. */
3668 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3670 /* A local symbol. */
3671 Elf_Internal_Sym *isym;
3674 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3675 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3676 symval = (isym->st_value
3677 + sym_sec->output_section->vma
3678 + sym_sec->output_offset);
3683 struct elf_link_hash_entry *h;
3685 /* An external symbol. */
3686 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3687 h = elf_sym_hashes (abfd)[indx];
3688 BFD_ASSERT (h != NULL);
3689 if (h->root.type != bfd_link_hash_defined
3690 && h->root.type != bfd_link_hash_defweak)
3692 /* This appears to be a reference to an undefined
3693 symbol. Just ignore it--it will be caught by the
3694 regular reloc processing. */
3698 symval = (h->root.u.def.value
3699 + h->root.u.def.section->output_section->vma
3700 + h->root.u.def.section->output_offset);
3703 /* For simplicity of coding, we are going to modify the section
3704 contents, the section relocs, and the BFD symbol table. We
3705 must tell the rest of the code not to free up this
3706 information. It would be possible to instead create a table
3707 of changes which have to be made, as is done in coff-mips.c;
3708 that would be more work, but would require less memory when
3709 the linker is run. */
3711 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3712 This sequence is generated by the compiler when compiling in
3713 32 bit mode. Also look for seth/add3 -> ld24. */
3715 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3717 Elf_Internal_Rela *nrel;
3718 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3720 bfd_signed_vma pcrel_value = symval - pc;
3721 unsigned int code,reg;
3722 int addend,nop_p,bl8_p,to_delete;
3724 /* The tests are ordered so that we get out as quickly as possible
3725 if this isn't something we can relax, taking into account that
3726 we are looking for two separate possibilities (jl/ld24). */
3728 /* Do nothing if no room in the section for this to be what we're
3730 if (irel->r_offset > sec->size - 8)
3733 /* Make sure the next relocation applies to the next
3734 instruction and that it's the add3's reloc. */
3737 || irel->r_offset + 4 != nrel->r_offset
3738 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3741 /* See if the instructions are seth/add3. */
3742 /* FIXME: This is where macros from cgen can come in. */
3743 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3744 if ((code & 0xf0ff) != 0xd0c0)
3745 continue; /* not seth rN,foo */
3746 reg = (code & 0x0f00) >> 8;
3747 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3748 if (code != (0x80a0 | reg | (reg << 8)))
3749 continue; /* not add3 rN,rN,foo */
3751 /* At this point we've confirmed we have seth/add3. Now check
3752 whether the next insn is a jl, in which case try to change this
3755 /* Ensure the branch target is in range.
3756 The bl24 instruction has a 24 bit operand which is the target
3757 address right shifted by 2, giving a signed range of 26 bits.
3758 Note that 4 bytes are added to the high value because the target
3759 will be at least 4 bytes closer if we can relax. It'll actually
3760 be 4 or 8 bytes closer, but we don't know which just yet and
3761 the difference isn't significant enough to worry about. */
3762 #if !USE_REL /* put in for learning purposes */
3763 pcrel_value += irel->r_addend;
3765 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3766 pcrel_value += addend;
3769 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3770 /* Do nothing if no room in the section for this to be what we're
3772 && (irel->r_offset <= sec->size - 12)
3773 /* Ensure the next insn is "jl rN". */
3774 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3775 code != (0x1ec0 | reg)))
3777 /* We can relax to bl24/bl8. */
3779 /* See if there's a nop following the jl.
3780 Also see if we can use a bl8 insn. */
3781 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3782 nop_p = (code & 0x7fff) == NOP_INSN;
3783 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3787 /* Change "seth rN,foo" to "bl8 foo || nop".
3788 We OR in CODE just in case it's not a nop (technically,
3789 CODE currently must be a nop, but for cleanness we
3790 allow it to be anything). */
3791 #if !USE_REL /* put in for learning purposes */
3792 code = 0x7e000000 | MAKE_PARALLEL (code);
3794 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3800 /* Change the seth rN,foo to a bl24 foo. */
3801 #if !USE_REL /* put in for learning purposes */
3804 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3806 to_delete = nop_p ? 8 : 4;
3809 bfd_put_32 (abfd, code, contents + irel->r_offset);
3811 /* Set the new reloc type. */
3812 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3813 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3815 /* Delete the add3 reloc by making it a null reloc. */
3816 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3819 else if (addend >= 0
3820 && symval + addend <= 0xffffff)
3822 /* We can relax to ld24. */
3824 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3825 bfd_put_32 (abfd, code, contents + irel->r_offset);
3827 /* Tell the following code a nop filler isn't needed. */
3832 /* Can't do anything here. */
3836 /* Note that we've changed the relocs, section contents, etc. */
3837 elf_section_data (sec)->relocs = internal_relocs;
3838 elf_section_data (sec)->this_hdr.contents = contents;
3839 symtab_hdr->contents = (unsigned char *) isymbuf;
3841 /* Delete TO_DELETE bytes of data. */
3842 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3843 irel->r_offset + 4, to_delete))
3846 /* Now that the following bytes have been moved into place, see if
3847 we need to replace the jl with a nop. This happens when we had
3848 to use a bl24 insn and the insn following the jl isn't a nop.
3849 Technically, this situation can't happen (since the insn can
3850 never be executed) but to be clean we do this. When the chip
3851 supports parallel 16 bit insns things may change.
3852 We don't need to do this in the case of relaxing to ld24,
3853 and the above code sets nop_p so this isn't done. */
3854 if (! nop_p && to_delete == 4)
3855 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3857 /* That will change things, so we should relax again.
3858 Note that this is not required, and it may be slow. */
3864 /* loop to try the next reloc */
3868 && symtab_hdr->contents != (unsigned char *) isymbuf)
3870 if (! link_info->keep_memory)
3874 /* Cache the symbols for elf_link_input_bfd. */
3875 symtab_hdr->contents = (unsigned char *) isymbuf;
3879 if (contents != NULL
3880 && elf_section_data (sec)->this_hdr.contents != contents)
3882 if (! link_info->keep_memory)
3886 /* Cache the section contents for elf_link_input_bfd. */
3887 elf_section_data (sec)->this_hdr.contents = contents;
3891 if (internal_relocs != NULL
3892 && elf_section_data (sec)->relocs != internal_relocs)
3893 free (internal_relocs);
3899 && symtab_hdr->contents != (unsigned char *) isymbuf)
3901 if (contents != NULL
3902 && elf_section_data (sec)->this_hdr.contents != contents)
3904 if (internal_relocs != NULL
3905 && elf_section_data (sec)->relocs != internal_relocs)
3906 free (internal_relocs);
3911 /* Delete some bytes from a section while relaxing. */
3914 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3920 Elf_Internal_Shdr *symtab_hdr;
3923 Elf_Internal_Rela *irel, *irelend;
3924 Elf_Internal_Rela *irelalign;
3926 Elf_Internal_Sym *isym, *isymend;
3927 struct elf_link_hash_entry **sym_hashes;
3928 struct elf_link_hash_entry **end_hashes;
3929 unsigned int symcount;
3931 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3933 contents = elf_section_data (sec)->this_hdr.contents;
3935 /* The deletion must stop at the next ALIGN reloc for an aligment
3936 power larger than the number of bytes we are deleting. */
3941 irel = elf_section_data (sec)->relocs;
3942 irelend = irel + sec->reloc_count;
3944 /* Actually delete the bytes. */
3945 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3948 /* Adjust all the relocs. */
3949 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3951 /* Get the new reloc address. */
3952 if ((irel->r_offset > addr
3953 && irel->r_offset < toaddr))
3954 irel->r_offset -= count;
3957 /* Adjust the local symbols defined in this section. */
3958 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3959 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3960 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3962 if (isym->st_shndx == shndx
3963 && isym->st_value > addr
3964 && isym->st_value < toaddr)
3965 isym->st_value -= count;
3968 /* Now adjust the global symbols defined in this section. */
3969 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3970 - symtab_hdr->sh_info);
3971 sym_hashes = elf_sym_hashes (abfd);
3972 end_hashes = sym_hashes + symcount;
3973 for (; sym_hashes < end_hashes; sym_hashes++)
3975 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3977 if ((sym_hash->root.type == bfd_link_hash_defined
3978 || sym_hash->root.type == bfd_link_hash_defweak)
3979 && sym_hash->root.u.def.section == sec
3980 && sym_hash->root.u.def.value > addr
3981 && sym_hash->root.u.def.value < toaddr)
3983 sym_hash->root.u.def.value -= count;
3990 /* This is a version of bfd_generic_get_relocated_section_contents
3991 which uses m32r_elf_relocate_section. */
3994 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3995 data, relocatable, symbols)
3997 struct bfd_link_info *link_info;
3998 struct bfd_link_order *link_order;
4000 bfd_boolean relocatable;
4003 Elf_Internal_Shdr *symtab_hdr;
4004 asection *input_section = link_order->u.indirect.section;
4005 bfd *input_bfd = input_section->owner;
4006 asection **sections = NULL;
4007 Elf_Internal_Rela *internal_relocs = NULL;
4008 Elf_Internal_Sym *isymbuf = NULL;
4011 /* We only need to handle the case of relaxing, or of having a
4012 particular set of section contents, specially. */
4014 || elf_section_data (input_section)->this_hdr.contents == NULL)
4015 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4020 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4022 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4023 input_section->size);
4025 if ((input_section->flags & SEC_RELOC) != 0
4026 && input_section->reloc_count > 0)
4028 Elf_Internal_Sym *isymp;
4030 Elf32_External_Sym *esym, *esymend;
4032 internal_relocs = (_bfd_elf_link_read_relocs
4033 (input_bfd, input_section, (PTR) NULL,
4034 (Elf_Internal_Rela *) NULL, FALSE));
4035 if (internal_relocs == NULL)
4038 if (symtab_hdr->sh_info != 0)
4040 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4041 if (isymbuf == NULL)
4042 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4043 symtab_hdr->sh_info, 0,
4045 if (isymbuf == NULL)
4049 amt = symtab_hdr->sh_info;
4050 amt *= sizeof (asection *);
4051 sections = (asection **) bfd_malloc (amt);
4052 if (sections == NULL && symtab_hdr->sh_info > 0)
4055 isymend = isymbuf + symtab_hdr->sh_info;
4056 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4060 if (isym->st_shndx == SHN_UNDEF)
4061 isec = bfd_und_section_ptr;
4062 else if (isym->st_shndx == SHN_ABS)
4063 isec = bfd_abs_section_ptr;
4064 else if (isym->st_shndx == SHN_COMMON)
4065 isec = bfd_com_section_ptr;
4066 else if (isym->st_shndx == SHN_M32R_SCOMMON)
4067 isec = &m32r_elf_scom_section;
4069 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4074 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4075 input_section, data, internal_relocs,
4079 if (sections != NULL)
4082 && symtab_hdr->contents != (unsigned char *) isymbuf)
4084 if (elf_section_data (input_section)->relocs != internal_relocs)
4085 free (internal_relocs);
4091 if (sections != NULL)
4094 && symtab_hdr->contents != (unsigned char *) isymbuf)
4096 if (internal_relocs != NULL
4097 && elf_section_data (input_section)->relocs != internal_relocs)
4098 free (internal_relocs);
4104 /* Set the right machine number. */
4106 m32r_elf_object_p (abfd)
4109 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4112 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
4113 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
4114 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
4119 /* Store the machine number in the flags field. */
4121 m32r_elf_final_write_processing (abfd, linker)
4123 bfd_boolean linker ATTRIBUTE_UNUSED;
4127 switch (bfd_get_mach (abfd))
4130 case bfd_mach_m32r: val = E_M32R_ARCH; break;
4131 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
4132 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
4135 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4136 elf_elfheader (abfd)->e_flags |= val;
4139 /* Function to keep M32R specific file flags. */
4141 m32r_elf_set_private_flags (abfd, flags)
4145 BFD_ASSERT (!elf_flags_init (abfd)
4146 || elf_elfheader (abfd)->e_flags == flags);
4148 elf_elfheader (abfd)->e_flags = flags;
4149 elf_flags_init (abfd) = TRUE;
4153 /* Merge backend specific data from an object file to the output
4154 object file when linking. */
4156 m32r_elf_merge_private_bfd_data (ibfd, obfd)
4163 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4164 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4167 in_flags = elf_elfheader (ibfd)->e_flags;
4168 out_flags = elf_elfheader (obfd)->e_flags;
4170 if (! elf_flags_init (obfd))
4172 /* If the input is the default architecture then do not
4173 bother setting the flags for the output architecture,
4174 instead allow future merges to do this. If no future
4175 merges ever set these flags then they will retain their
4176 unitialised values, which surprise surprise, correspond
4177 to the default values. */
4178 if (bfd_get_arch_info (ibfd)->the_default)
4181 elf_flags_init (obfd) = TRUE;
4182 elf_elfheader (obfd)->e_flags = in_flags;
4184 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4185 && bfd_get_arch_info (obfd)->the_default)
4187 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4193 /* Check flag compatibility. */
4194 if (in_flags == out_flags)
4197 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4199 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4200 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4201 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
4203 (*_bfd_error_handler)
4204 (_("%s: Instruction set mismatch with previous modules"),
4205 bfd_archive_filename (ibfd));
4207 bfd_set_error (bfd_error_bad_value);
4215 /* Display the flags field */
4217 m32r_elf_print_private_bfd_data (abfd, ptr)
4221 FILE * file = (FILE *) ptr;
4223 BFD_ASSERT (abfd != NULL && ptr != NULL)
4225 _bfd_elf_print_private_bfd_data (abfd, ptr);
4227 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
4229 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4232 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
4233 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
4234 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
4243 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4245 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4246 Elf_Internal_Rela *rel;
4247 struct elf_link_hash_entry *h;
4248 Elf_Internal_Sym *sym;
4252 switch (ELF32_R_TYPE (rel->r_info))
4254 case R_M32R_GNU_VTINHERIT:
4255 case R_M32R_GNU_VTENTRY:
4256 case R_M32R_RELA_GNU_VTINHERIT:
4257 case R_M32R_RELA_GNU_VTENTRY:
4261 switch (h->root.type)
4263 case bfd_link_hash_defined:
4264 case bfd_link_hash_defweak:
4265 return h->root.u.def.section;
4267 case bfd_link_hash_common:
4268 return h->root.u.c.p->section;
4276 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4282 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
4283 bfd *abfd ATTRIBUTE_UNUSED;
4284 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4285 asection *sec ATTRIBUTE_UNUSED;
4286 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4288 /* Update the got entry reference counts for the section being removed. */
4289 Elf_Internal_Shdr *symtab_hdr;
4290 struct elf_link_hash_entry **sym_hashes;
4291 bfd_signed_vma *local_got_refcounts;
4292 const Elf_Internal_Rela *rel, *relend;
4293 unsigned long r_symndx;
4294 struct elf_link_hash_entry *h;
4296 elf_section_data (sec)->local_dynrel = NULL;
4298 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4299 sym_hashes = elf_sym_hashes (abfd);
4300 local_got_refcounts = elf_local_got_refcounts (abfd);
4302 relend = relocs + sec->reloc_count;
4303 for (rel = relocs; rel < relend; rel++)
4304 switch (ELF32_R_TYPE (rel->r_info))
4306 case R_M32R_GOT16_HI_ULO:
4307 case R_M32R_GOT16_HI_SLO:
4308 case R_M32R_GOT16_LO:
4310 case R_M32R_GOTPC_HI_ULO:
4311 case R_M32R_GOTPC_HI_SLO:
4312 case R_M32R_GOTPC_LO:
4313 case R_M32R_GOTPC24:
4314 r_symndx = ELF32_R_SYM (rel->r_info);
4315 if (r_symndx >= symtab_hdr->sh_info)
4317 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4318 if (h->got.refcount > 0)
4323 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4324 local_got_refcounts[r_symndx]--;
4328 case R_M32R_16_RELA:
4329 case R_M32R_24_RELA:
4330 case R_M32R_32_RELA:
4331 case R_M32R_HI16_ULO_RELA:
4332 case R_M32R_HI16_SLO_RELA:
4333 case R_M32R_LO16_RELA:
4334 case R_M32R_SDA16_RELA:
4335 case R_M32R_18_PCREL_RELA:
4336 case R_M32R_26_PCREL_RELA:
4337 r_symndx = ELF32_R_SYM (rel->r_info);
4338 if (r_symndx >= symtab_hdr->sh_info)
4340 struct elf_m32r_link_hash_entry *eh;
4341 struct elf_m32r_dyn_relocs **pp;
4342 struct elf_m32r_dyn_relocs *p;
4344 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4346 if (!info->shared && h->plt.refcount > 0)
4347 h->plt.refcount -= 1;
4349 eh = (struct elf_m32r_link_hash_entry *) h;
4351 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4354 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4355 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4365 case R_M32R_26_PLTREL:
4366 r_symndx = ELF32_R_SYM (rel->r_info);
4367 if (r_symndx >= symtab_hdr->sh_info)
4369 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4370 if (h->plt.refcount > 0)
4382 /* Look through the relocs for a section during the first phase.
4383 Since we don't do .gots or .plts, we just need to consider the
4384 virtual table relocs for gc. */
4387 m32r_elf_check_relocs (abfd, info, sec, relocs)
4389 struct bfd_link_info *info;
4391 const Elf_Internal_Rela *relocs;
4393 Elf_Internal_Shdr *symtab_hdr;
4394 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4395 const Elf_Internal_Rela *rel;
4396 const Elf_Internal_Rela *rel_end;
4397 struct elf_m32r_link_hash_table *htab;
4399 bfd_vma *local_got_offsets;
4400 asection *sgot, *srelgot, *sreloc;
4402 if (info->relocatable)
4405 sgot = srelgot = sreloc = NULL;
4407 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4408 sym_hashes = elf_sym_hashes (abfd);
4409 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
4410 if (!elf_bad_symtab (abfd))
4411 sym_hashes_end -= symtab_hdr->sh_info;
4413 htab = m32r_elf_hash_table (info);
4414 dynobj = htab->root.dynobj;
4415 local_got_offsets = elf_local_got_offsets (abfd);
4417 rel_end = relocs + sec->reloc_count;
4418 for (rel = relocs; rel < rel_end; rel++)
4421 struct elf_link_hash_entry *h;
4422 unsigned long r_symndx;
4424 r_symndx = ELF32_R_SYM (rel->r_info);
4425 r_type = ELF32_R_TYPE (rel->r_info);
4426 if (r_symndx < symtab_hdr->sh_info)
4429 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4431 /* Some relocs require a global offset table. */
4432 if (htab->sgot == NULL)
4436 case R_M32R_GOT16_HI_ULO:
4437 case R_M32R_GOT16_HI_SLO:
4438 case R_M32R_GOT16_LO:
4439 case R_M32R_GOTPC24:
4440 case R_M32R_GOTPC_HI_ULO:
4441 case R_M32R_GOTPC_HI_SLO:
4442 case R_M32R_GOTPC_LO:
4445 htab->root.dynobj = dynobj = abfd;
4446 if (! create_got_section (dynobj, info))
4457 case R_M32R_GOT16_HI_ULO:
4458 case R_M32R_GOT16_HI_SLO:
4459 case R_M32R_GOT16_LO:
4463 h->got.refcount += 1;
4466 bfd_signed_vma *local_got_refcounts;
4468 /* This is a global offset table entry for a local
4470 local_got_refcounts = elf_local_got_refcounts (abfd);
4471 if (local_got_refcounts == NULL)
4475 size = symtab_hdr->sh_info;
4476 size *= sizeof (bfd_signed_vma);
4477 local_got_refcounts = ((bfd_signed_vma *)
4478 bfd_zalloc (abfd, size));
4479 if (local_got_refcounts == NULL)
4481 elf_local_got_refcounts (abfd) = local_got_refcounts;
4483 local_got_refcounts[r_symndx] += 1;
4487 case R_M32R_26_PLTREL:
4488 /* This symbol requires a procedure linkage table entry. We
4489 actually build the entry in adjust_dynamic_symbol,
4490 because this might be a case of linking PIC code without
4491 linking in any dynamic objects, in which case we don't
4492 need to generate a procedure linkage table after all. */
4494 /* If this is a local symbol, we resolve it directly without
4495 creating a procedure linkage table entry. */
4499 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4502 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4503 h->plt.refcount += 1;
4506 case R_M32R_16_RELA:
4507 case R_M32R_24_RELA:
4508 case R_M32R_32_RELA:
4509 case R_M32R_HI16_ULO_RELA:
4510 case R_M32R_HI16_SLO_RELA:
4511 case R_M32R_LO16_RELA:
4512 case R_M32R_SDA16_RELA:
4513 case R_M32R_18_PCREL_RELA:
4514 case R_M32R_26_PCREL_RELA:
4516 if (h != NULL && !info->shared)
4518 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
4519 h->plt.refcount += 1;
4522 /* If we are creating a shared library, and this is a reloc
4523 against a global symbol, or a non PC relative reloc
4524 against a local symbol, then we need to copy the reloc
4525 into the shared library. However, if we are linking with
4526 -Bsymbolic, we do not need to copy a reloc against a
4527 global symbol which is defined in an object we are
4528 including in the link (i.e., DEF_REGULAR is set). At
4529 this point we have not seen all the input files, so it is
4530 possible that DEF_REGULAR is not set now but will be set
4531 later (it is never cleared). We account for that
4532 possibility below by storing information in the
4533 dyn_relocs field of the hash table entry. A similar
4534 situation occurs when creating shared libraries and symbol
4535 visibility changes render the symbol local.
4537 If on the other hand, we are creating an executable, we
4538 may need to keep relocations for symbols satisfied by a
4539 dynamic library if we manage to avoid copy relocs for the
4542 && (sec->flags & SEC_ALLOC) != 0
4543 && ((r_type != R_M32R_26_PCREL_RELA
4544 && r_type != R_M32R_18_PCREL_RELA)
4546 && (! info->symbolic
4547 || h->root.type == bfd_link_hash_defweak
4548 || (h->elf_link_hash_flags
4549 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4551 && (sec->flags & SEC_ALLOC) != 0
4553 && (h->root.type == bfd_link_hash_defweak
4554 || (h->elf_link_hash_flags
4555 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4557 struct elf_m32r_dyn_relocs *p;
4558 struct elf_m32r_dyn_relocs **head;
4561 htab->root.dynobj = dynobj = abfd;
4563 /* When creating a shared object, we must copy these
4564 relocs into the output file. We create a reloc
4565 section in dynobj and make room for the reloc. */
4570 name = (bfd_elf_string_from_elf_section
4572 elf_elfheader (abfd)->e_shstrndx,
4573 elf_section_data (sec)->rel_hdr.sh_name));
4577 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4578 && strcmp (bfd_get_section_name (abfd, sec),
4581 sreloc = bfd_get_section_by_name (dynobj, name);
4586 sreloc = bfd_make_section (dynobj, name);
4587 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4588 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4589 if ((sec->flags & SEC_ALLOC) != 0)
4590 flags |= SEC_ALLOC | SEC_LOAD;
4592 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4593 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4596 elf_section_data (sec)->sreloc = sreloc;
4599 /* If this is a global symbol, we count the number of
4600 relocations we need for this symbol. */
4602 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4607 /* Track dynamic relocs needed for local syms too. */
4608 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4613 head = ((struct elf_m32r_dyn_relocs **)
4614 &elf_section_data (s)->local_dynrel);
4618 if (p == NULL || p->sec != sec)
4620 bfd_size_type amt = sizeof (*p);
4621 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4632 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4633 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4638 /* This relocation describes the C++ object vtable hierarchy.
4639 Reconstruct it for later use during GC. */
4640 case R_M32R_RELA_GNU_VTINHERIT:
4641 case R_M32R_GNU_VTINHERIT:
4642 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4646 /* This relocation describes which C++ vtable entries are actually
4647 used. Record for later use during GC. */
4648 case R_M32R_GNU_VTENTRY:
4649 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4652 case R_M32R_RELA_GNU_VTENTRY:
4653 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4662 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4664 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4665 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4666 { NULL, 0, 0, 0, 0 }
4670 m32r_elf_fake_sections (abfd, hdr, sec)
4672 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4675 register const char *name;
4677 name = bfd_get_section_name (abfd, sec);
4679 /* The generic elf_fake_sections will set up REL_HDR using the
4680 default kind of relocations. But, we may actually need both
4681 kinds of relocations, so we set up the second header here.
4683 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4684 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4685 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4686 of the resulting empty .rela.<section> sections starts with
4687 sh_offset == object size, and ld doesn't allow that. While the check
4688 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4689 avoided by not emitting those useless sections in the first place. */
4690 if ((sec->flags & SEC_RELOC) != 0)
4692 struct bfd_elf_section_data *esd;
4693 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4695 esd = elf_section_data (sec);
4696 BFD_ASSERT (esd->rel_hdr2 == NULL);
4697 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4700 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4707 static enum elf_reloc_type_class
4708 m32r_elf_reloc_type_class (rela)
4709 const Elf_Internal_Rela *rela;
4711 switch ((int) ELF32_R_TYPE (rela->r_info))
4713 case R_M32R_RELATIVE:
4714 return reloc_class_relative;
4715 case R_M32R_JMP_SLOT:
4716 return reloc_class_plt;
4718 return reloc_class_copy;
4720 return reloc_class_normal;
4724 #define ELF_ARCH bfd_arch_m32r
4725 #define ELF_MACHINE_CODE EM_M32R
4726 #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
4727 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
4729 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
4730 #define TARGET_BIG_NAME "elf32-m32r"
4731 #define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4732 #define TARGET_LITTLE_NAME "elf32-m32rle"
4734 #define elf_info_to_howto m32r_info_to_howto
4735 #define elf_info_to_howto_rel m32r_info_to_howto_rel
4736 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4737 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4738 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4739 #define elf_backend_relocate_section m32r_elf_relocate_section
4740 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4741 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4742 #define elf_backend_check_relocs m32r_elf_check_relocs
4744 #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4745 #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4746 #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4747 #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4748 #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4749 #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4750 #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4751 #define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4753 #define elf_backend_can_gc_sections 1
4755 #define elf_backend_rela_normal 1
4757 #define elf_backend_can_refcount 1
4758 #define elf_backend_want_got_plt 1
4759 #define elf_backend_plt_readonly 1
4760 #define elf_backend_want_plt_sym 0
4761 #define elf_backend_got_header_size 12
4763 #define elf_backend_may_use_rel_p 1
4764 #ifdef USE_M32R_OLD_RELOC
4765 #define elf_backend_default_use_rela_p 0
4766 #define elf_backend_may_use_rela_p 0
4768 #define elf_backend_default_use_rela_p 1
4769 #define elf_backend_may_use_rela_p 1
4770 #define elf_backend_fake_sections m32r_elf_fake_sections
4775 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4776 #define bfd_elf32_bfd_get_relocated_section_contents \
4777 m32r_elf_get_relocated_section_contents
4780 #define elf_backend_object_p m32r_elf_object_p
4781 #define elf_backend_final_write_processing m32r_elf_final_write_processing
4782 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4783 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4784 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
4785 #define elf_backend_special_sections m32r_elf_special_sections
4787 #include "elf32-target.h"
4789 #undef ELF_MAXPAGESIZE
4790 #define ELF_MAXPAGESIZE 0x1000
4792 #undef TARGET_BIG_SYM
4793 #define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4794 #undef TARGET_BIG_NAME
4795 #define TARGET_BIG_NAME "elf32-m32r-linux"
4796 #undef TARGET_LITTLE_SYM
4797 #define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4798 #undef TARGET_LITTLE_NAME
4799 #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4801 #define elf32_bed elf32_m32r_lin_bed
4803 #include "elf32-target.h"