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 0xffffff, /* src_mask */
689 0xffffff, /* 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 */
807 HOWTO (R_M32R_GOTOFF_HI_ULO, /* type */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
811 FALSE, /* pc_relative */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_M32R_GOTOFF_HI_ULO",/* name */
816 FALSE, /* partial_inplace */
817 0x0000ffff, /* src_mask */
818 0x0000ffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
821 HOWTO (R_M32R_GOTOFF_HI_SLO, /* type */
823 2, /* size (0 = byte, 1 = short, 2 = long) */
825 FALSE, /* pc_relative */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 "R_M32R_GOTOFF_HI_SLO",/* name */
830 FALSE, /* partial_inplace */
831 0x0000ffff, /* src_mask */
832 0x0000ffff, /* dst_mask */
833 FALSE), /* pcrel_offset */
835 HOWTO (R_M32R_GOTOFF_LO, /* type */
837 2, /* size (0 = byte, 1 = short, 2 = long) */
839 FALSE, /* pc_relative */
841 complain_overflow_dont, /* complain_on_overflow */
842 bfd_elf_generic_reloc, /* special_function */
843 "R_M32R_GOTOFF_LO", /* name */
844 FALSE, /* partial_inplace */
845 0x0000ffff, /* src_mask */
846 0x0000ffff, /* dst_mask */
847 FALSE), /* pcrel_offset */
850 /* Handle the R_M32R_10_PCREL reloc. */
852 static bfd_reloc_status_type
853 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
854 input_section, output_bfd, error_message)
856 arelent * reloc_entry;
859 asection * input_section;
861 char ** error_message ATTRIBUTE_UNUSED;
863 /* This part is from bfd_elf_generic_reloc. */
864 if (output_bfd != (bfd *) NULL
865 && (symbol->flags & BSF_SECTION_SYM) == 0
866 && (! reloc_entry->howto->partial_inplace
867 || reloc_entry->addend == 0))
869 reloc_entry->address += input_section->output_offset;
873 if (output_bfd != NULL)
875 /* FIXME: See bfd_perform_relocation. Is this right? */
876 return bfd_reloc_continue;
879 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
881 data, reloc_entry->address,
884 + symbol->section->output_section->vma
885 + symbol->section->output_offset),
886 reloc_entry->addend);
889 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
891 static bfd_reloc_status_type
892 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
893 symbol_section, symbol_value, addend)
895 reloc_howto_type *howto;
896 asection *input_section;
899 asection *symbol_section ATTRIBUTE_UNUSED;
900 bfd_vma symbol_value;
903 bfd_signed_vma relocation;
905 bfd_reloc_status_type status;
907 /* Sanity check the address (offset in section). */
908 if (offset > bfd_get_section_limit (abfd, input_section))
909 return bfd_reloc_outofrange;
911 relocation = symbol_value + addend;
912 /* Make it pc relative. */
913 relocation -= (input_section->output_section->vma
914 + input_section->output_offset);
915 /* These jumps mask off the lower two bits of the current address
916 before doing pcrel calculations. */
917 relocation -= (offset & -(bfd_vma) 4);
919 if (relocation < -0x200 || relocation > 0x1ff)
920 status = bfd_reloc_overflow;
922 status = bfd_reloc_ok;
924 x = bfd_get_16 (abfd, data + offset);
925 relocation >>= howto->rightshift;
926 relocation <<= howto->bitpos;
927 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
928 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
933 /* Handle the R_M32R_HI16_[SU]LO relocs.
934 HI16_SLO is for the add3 and load/store with displacement instructions.
935 HI16_ULO is for the or3 instruction.
936 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
937 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
938 we must add one to the high 16 bytes (which will get subtracted off when
939 the low 16 bits are added).
940 These relocs have to be done in combination with an R_M32R_LO16 reloc
941 because there is a carry from the LO16 to the HI16. Here we just save
942 the information we need; we do the actual relocation when we see the LO16.
943 This code is copied from the elf32-mips.c. We also support an arbitrary
944 number of HI16 relocs to be associated with a single LO16 reloc. The
945 assembler sorts the relocs to ensure each HI16 immediately precedes its
946 LO16. However if there are multiple copies, the assembler may not find
947 the real LO16 so it picks the first one it finds. */
951 struct m32r_hi16 *next;
956 /* FIXME: This should not be a static variable. */
958 static struct m32r_hi16 *m32r_hi16_list;
960 static bfd_reloc_status_type
961 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
962 input_section, output_bfd, error_message)
963 bfd *abfd ATTRIBUTE_UNUSED;
964 arelent *reloc_entry;
967 asection *input_section;
969 char **error_message ATTRIBUTE_UNUSED;
971 bfd_reloc_status_type ret;
975 /* This part is from bfd_elf_generic_reloc.
976 If we're relocating, and this an external symbol, we don't want
977 to change anything. */
978 if (output_bfd != (bfd *) NULL
979 && (symbol->flags & BSF_SECTION_SYM) == 0
980 && reloc_entry->addend == 0)
982 reloc_entry->address += input_section->output_offset;
986 /* Sanity check the address (offset in section). */
987 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
988 return bfd_reloc_outofrange;
991 if (bfd_is_und_section (symbol->section)
992 && output_bfd == (bfd *) NULL)
993 ret = bfd_reloc_undefined;
995 if (bfd_is_com_section (symbol->section))
998 relocation = symbol->value;
1000 relocation += symbol->section->output_section->vma;
1001 relocation += symbol->section->output_offset;
1002 relocation += reloc_entry->addend;
1004 /* Save the information, and let LO16 do the actual relocation. */
1005 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
1007 return bfd_reloc_outofrange;
1008 n->addr = (bfd_byte *) data + reloc_entry->address;
1009 n->addend = relocation;
1010 n->next = m32r_hi16_list;
1013 if (output_bfd != (bfd *) NULL)
1014 reloc_entry->address += input_section->output_offset;
1019 /* Handle an M32R ELF HI16 reloc. */
1022 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
1025 Elf_Internal_Rela *relhi;
1026 Elf_Internal_Rela *rello;
1033 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1035 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
1036 if (type == R_M32R_HI16_SLO)
1037 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
1041 addend += ((insn & 0xffff) << 16) + addlo;
1043 /* Reaccount for sign extension of low part. */
1044 if (type == R_M32R_HI16_SLO
1045 && (addend & 0x8000) != 0)
1048 bfd_put_32 (input_bfd,
1049 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1050 contents + relhi->r_offset);
1053 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1054 inplace relocation; this function exists in order to do the
1055 R_M32R_HI16_[SU]LO relocation described above. */
1057 bfd_reloc_status_type
1058 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1059 input_section, output_bfd, error_message)
1061 arelent *reloc_entry;
1064 asection *input_section;
1066 char **error_message;
1068 /* This part is from bfd_elf_generic_reloc.
1069 If we're relocating, and this an external symbol, we don't want
1070 to change anything. */
1071 if (output_bfd != (bfd *) NULL
1072 && (symbol->flags & BSF_SECTION_SYM) == 0
1073 && reloc_entry->addend == 0)
1075 reloc_entry->address += input_section->output_offset;
1076 return bfd_reloc_ok;
1079 if (m32r_hi16_list != NULL)
1081 struct m32r_hi16 *l;
1088 unsigned long vallo;
1089 struct m32r_hi16 *next;
1091 /* Do the HI16 relocation. Note that we actually don't need
1092 to know anything about the LO16 itself, except where to
1093 find the low 16 bits of the addend needed by the LO16. */
1094 insn = bfd_get_32 (input_bfd, l->addr);
1095 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
1096 & 0xffff) ^ 0x8000) - 0x8000;
1097 val = ((insn & 0xffff) << 16) + vallo;
1100 /* Reaccount for sign extension of low part. */
1101 if ((val & 0x8000) != 0)
1104 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1105 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1112 m32r_hi16_list = NULL;
1115 /* Now do the LO16 reloc in the usual way.
1116 ??? It would be nice to call bfd_elf_generic_reloc here,
1117 but we have partial_inplace set. bfd_elf_generic_reloc will
1118 pass the handling back to bfd_install_relocation which will install
1119 a section relative addend which is wrong. */
1120 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1121 input_section, output_bfd, error_message);
1124 /* Do generic partial_inplace relocation.
1125 This is a local replacement for bfd_elf_generic_reloc. */
1127 bfd_reloc_status_type
1128 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1129 input_section, output_bfd, error_message)
1131 arelent *reloc_entry;
1134 asection *input_section;
1136 char **error_message ATTRIBUTE_UNUSED;
1138 bfd_reloc_status_type ret;
1140 bfd_byte *inplace_address;
1142 /* This part is from bfd_elf_generic_reloc.
1143 If we're relocating, and this an external symbol, we don't want
1144 to change anything. */
1145 if (output_bfd != (bfd *) NULL
1146 && (symbol->flags & BSF_SECTION_SYM) == 0
1147 && reloc_entry->addend == 0)
1149 reloc_entry->address += input_section->output_offset;
1150 return bfd_reloc_ok;
1153 /* Now do the reloc in the usual way.
1154 ??? It would be nice to call bfd_elf_generic_reloc here,
1155 but we have partial_inplace set. bfd_elf_generic_reloc will
1156 pass the handling back to bfd_install_relocation which will install
1157 a section relative addend which is wrong. */
1159 /* Sanity check the address (offset in section). */
1160 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
1161 return bfd_reloc_outofrange;
1164 if (bfd_is_und_section (symbol->section)
1165 && output_bfd == (bfd *) NULL)
1166 ret = bfd_reloc_undefined;
1168 if (bfd_is_com_section (symbol->section)
1169 || output_bfd != (bfd *) NULL)
1172 relocation = symbol->value;
1174 /* Only do this for a final link. */
1175 if (output_bfd == (bfd *) NULL)
1177 relocation += symbol->section->output_section->vma;
1178 relocation += symbol->section->output_offset;
1181 relocation += reloc_entry->addend;
1182 inplace_address = (bfd_byte *) data + reloc_entry->address;
1185 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1186 (((x & reloc_entry->howto->src_mask) + relocation) & \
1187 reloc_entry->howto->dst_mask))
1189 switch (reloc_entry->howto->size)
1193 short x = bfd_get_16 (input_bfd, inplace_address);
1195 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1200 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1202 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1209 if (output_bfd != (bfd *) NULL)
1210 reloc_entry->address += input_section->output_offset;
1215 /* Handle the R_M32R_SDA16 reloc.
1216 This reloc is used to compute the address of objects in the small data area
1217 and to perform loads and stores from that area.
1218 The lower 16 bits are sign extended and added to the register specified
1219 in the instruction, which is assumed to point to _SDA_BASE_. */
1221 static bfd_reloc_status_type
1222 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1223 input_section, output_bfd, error_message)
1224 bfd *abfd ATTRIBUTE_UNUSED;
1225 arelent *reloc_entry;
1227 PTR data ATTRIBUTE_UNUSED;
1228 asection *input_section;
1230 char **error_message ATTRIBUTE_UNUSED;
1232 /* This part is from bfd_elf_generic_reloc. */
1233 if (output_bfd != (bfd *) NULL
1234 && (symbol->flags & BSF_SECTION_SYM) == 0
1235 && (! reloc_entry->howto->partial_inplace
1236 || reloc_entry->addend == 0))
1238 reloc_entry->address += input_section->output_offset;
1239 return bfd_reloc_ok;
1242 if (output_bfd != NULL)
1244 /* FIXME: See bfd_perform_relocation. Is this right? */
1245 return bfd_reloc_continue;
1248 /* FIXME: not sure what to do here yet. But then again, the linker
1249 may never call us. */
1253 /* Map BFD reloc types to M32R ELF reloc types. */
1255 struct m32r_reloc_map
1257 bfd_reloc_code_real_type bfd_reloc_val;
1258 unsigned char elf_reloc_val;
1261 static const struct m32r_reloc_map m32r_reloc_map_old[] =
1263 { BFD_RELOC_NONE, R_M32R_NONE },
1264 { BFD_RELOC_16, R_M32R_16 },
1265 { BFD_RELOC_32, R_M32R_32 },
1266 { BFD_RELOC_M32R_24, R_M32R_24 },
1267 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1268 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1269 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1270 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1271 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1272 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1273 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1274 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1275 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1278 static const struct m32r_reloc_map m32r_reloc_map[] =
1280 { BFD_RELOC_NONE, R_M32R_NONE },
1281 { BFD_RELOC_16, R_M32R_16_RELA },
1282 { BFD_RELOC_32, R_M32R_32_RELA },
1283 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1284 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1285 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1286 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1287 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1288 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1289 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1290 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1291 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1292 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
1294 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1295 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1296 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1297 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1298 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1299 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1300 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1301 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1302 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1303 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1304 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1305 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1306 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1307 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
1308 { BFD_RELOC_M32R_GOTOFF_HI_ULO, R_M32R_GOTOFF_HI_ULO },
1309 { BFD_RELOC_M32R_GOTOFF_HI_SLO, R_M32R_GOTOFF_HI_SLO },
1310 { BFD_RELOC_M32R_GOTOFF_LO, R_M32R_GOTOFF_LO },
1313 static reloc_howto_type *
1314 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1315 bfd *abfd ATTRIBUTE_UNUSED;
1316 bfd_reloc_code_real_type code;
1320 #ifdef USE_M32R_OLD_RELOC
1322 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1325 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1326 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1328 #else /* ! USE_M32R_OLD_RELOC */
1331 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1334 if (m32r_reloc_map[i].bfd_reloc_val == code)
1335 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1342 /* Set the howto pointer for an M32R ELF reloc. */
1345 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1346 bfd *abfd ATTRIBUTE_UNUSED;
1348 Elf_Internal_Rela *dst;
1350 unsigned int r_type;
1352 r_type = ELF32_R_TYPE (dst->r_info);
1353 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY)
1354 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1358 m32r_info_to_howto (abfd, cache_ptr, dst)
1359 bfd *abfd ATTRIBUTE_UNUSED;
1361 Elf_Internal_Rela *dst;
1363 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1364 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1365 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1366 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1370 /* Given a BFD section, try to locate the corresponding ELF section
1374 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1375 bfd *abfd ATTRIBUTE_UNUSED;
1379 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1381 *retval = SHN_M32R_SCOMMON;
1387 /* M32R ELF uses two common sections. One is the usual one, and the other
1388 is for small objects. All the small objects are kept together, and then
1389 referenced via one register, which yields faster assembler code. It is
1390 up to the compiler to emit an instruction to load the register with
1391 _SDA_BASE. This is what we use for the small common section. This
1392 approach is copied from elf32-mips.c. */
1393 static asection m32r_elf_scom_section;
1394 static asymbol m32r_elf_scom_symbol;
1395 static asymbol *m32r_elf_scom_symbol_ptr;
1397 /* Handle the special M32R section numbers that a symbol may use. */
1400 _bfd_m32r_elf_symbol_processing (abfd, asym)
1401 bfd *abfd ATTRIBUTE_UNUSED;
1404 elf_symbol_type *elfsym;
1406 elfsym = (elf_symbol_type *) asym;
1408 switch (elfsym->internal_elf_sym.st_shndx)
1410 case SHN_M32R_SCOMMON:
1411 if (m32r_elf_scom_section.name == NULL)
1413 /* Initialize the small common section. */
1414 m32r_elf_scom_section.name = ".scommon";
1415 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1416 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1417 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1418 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1419 m32r_elf_scom_symbol.name = ".scommon";
1420 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1421 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1422 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1424 asym->section = &m32r_elf_scom_section;
1425 asym->value = elfsym->internal_elf_sym.st_size;
1430 /* Hook called by the linker routine which adds symbols from an object
1431 file. We must handle the special M32R section numbers here.
1432 We also keep watching for whether we need to create the sdata special
1436 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1438 struct bfd_link_info *info;
1439 Elf_Internal_Sym *sym;
1441 flagword *flagsp ATTRIBUTE_UNUSED;
1445 if (! info->relocatable
1446 && (*namep)[0] == '_' && (*namep)[1] == 'S'
1447 && strcmp (*namep, "_SDA_BASE_") == 0
1448 && is_elf_hash_table (info->hash))
1450 /* This is simpler than using _bfd_elf_create_linker_section
1451 (our needs are simpler than ppc's needs). Also
1452 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1453 section already exists a new one is created that follows it which
1454 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1455 struct elf_link_hash_entry *h;
1456 struct bfd_link_hash_entry *bh;
1457 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1459 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1463 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1464 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1466 s = bfd_make_section_anyway (abfd, ".sdata");
1469 bfd_set_section_flags (abfd, s, flags);
1470 bfd_set_section_alignment (abfd, s, 2);
1473 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1474 FALSE, FALSE, FALSE);
1476 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1477 && !(_bfd_generic_link_add_one_symbol (info,
1483 (const char *) NULL,
1485 get_elf_backend_data (abfd)->collect,
1488 h = (struct elf_link_hash_entry *) bh;
1489 h->type = STT_OBJECT;
1492 switch (sym->st_shndx)
1494 case SHN_M32R_SCOMMON:
1495 *secp = bfd_make_section_old_way (abfd, ".scommon");
1496 (*secp)->flags |= SEC_IS_COMMON;
1497 *valp = sym->st_size;
1504 /* We have to figure out the SDA_BASE value, so that we can adjust the
1505 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1506 BFD. If we can't find it, we're stuck. We cache it in the ELF
1507 target data. We don't need to adjust the symbol value for an
1508 external symbol if we are producing relocatable output. */
1510 static bfd_reloc_status_type
1511 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1513 struct bfd_link_info *info;
1514 const char **error_message;
1517 if (elf_gp (output_bfd) == 0)
1519 struct bfd_link_hash_entry *h;
1521 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1522 if (h != (struct bfd_link_hash_entry *) NULL
1523 && h->type == bfd_link_hash_defined)
1524 elf_gp (output_bfd) = (h->u.def.value
1525 + h->u.def.section->output_section->vma
1526 + h->u.def.section->output_offset);
1529 /* Only get the error once. */
1530 *psb = elf_gp (output_bfd) = 4;
1532 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1533 return bfd_reloc_dangerous;
1536 *psb = elf_gp (output_bfd);
1537 return bfd_reloc_ok;
1540 /* Return size of a PLT entry. */
1541 #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1543 /* The m32r linker needs to keep track of the number of relocs that it
1544 decides to copy in check_relocs for each symbol. This is so that
1545 it can discard PC relative relocs if it doesn't need them when
1546 linking with -Bsymbolic. We store the information in a field
1547 extending the regular ELF linker hash table. */
1549 /* This structure keeps track of the number of PC relative relocs we
1550 have copied for a given symbol. */
1552 struct elf_m32r_pcrel_relocs_copied
1555 struct elf_m32r_pcrel_relocs_copied *next;
1556 /* A section in dynobj. */
1558 /* Number of relocs copied in this section. */
1559 bfd_size_type count;
1562 /* The sh linker needs to keep track of the number of relocs that it
1563 decides to copy as dynamic relocs in check_relocs for each symbol.
1564 This is so that it can later discard them if they are found to be
1565 unnecessary. We store the information in a field extending the
1566 regular ELF linker hash table. */
1568 struct elf_m32r_dyn_relocs
1570 struct elf_m32r_dyn_relocs *next;
1572 /* The input section of the reloc. */
1575 /* Total number of relocs copied for the input section. */
1576 bfd_size_type count;
1578 /* Number of pc-relative relocs copied for the input section. */
1579 bfd_size_type pc_count;
1583 /* m32r ELF linker hash entry. */
1585 struct elf_m32r_link_hash_entry
1587 struct elf_link_hash_entry root;
1589 /* Track dynamic relocs copied for this symbol. */
1590 struct elf_m32r_dyn_relocs *dyn_relocs;
1592 // bfd_signed_vma gotplt_refcount;
1594 /* Number of PC relative relocs copied for this symbol. */
1595 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1598 /* m32r ELF linker hash table. */
1600 struct elf_m32r_link_hash_table
1602 struct elf_link_hash_table root;
1604 /* Short-cuts to get to dynamic linker sections. */
1613 /* Small local sym to section mapping cache. */
1614 struct sym_sec_cache sym_sec;
1617 /* Traverse an m32r ELF linker hash table. */
1619 #define m32r_elf_link_hash_traverse(table, func, info) \
1620 (elf_link_hash_traverse \
1622 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1625 /* Get the m32r ELF linker hash table from a link_info structure. */
1628 #define m32r_elf_hash_table(p) \
1629 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1631 /* Create an entry in an m32r ELF linker hash table. */
1632 static struct bfd_hash_entry *
1633 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1636 static struct bfd_hash_entry *
1637 m32r_elf_link_hash_newfunc (entry, table, string)
1638 struct bfd_hash_entry *entry;
1639 struct bfd_hash_table *table;
1642 struct elf_m32r_link_hash_entry *ret =
1643 (struct elf_m32r_link_hash_entry *) entry;
1645 /* Allocate the structure if it has not already been allocated by a
1647 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1648 ret = ((struct elf_m32r_link_hash_entry *)
1649 bfd_hash_allocate (table,
1650 sizeof (struct elf_m32r_link_hash_entry)));
1651 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1652 return (struct bfd_hash_entry *) ret;
1654 /* Call the allocation method of the superclass. */
1655 ret = ((struct elf_m32r_link_hash_entry *)
1656 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1658 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1660 struct elf_m32r_link_hash_entry *eh;
1662 eh = (struct elf_m32r_link_hash_entry *) ret;
1663 eh->dyn_relocs = NULL;
1664 // eh->gotplt_refcount = 0;
1665 /* eh->pcrel_relocs_copied = NULL; FIXME */
1668 return (struct bfd_hash_entry *) ret;
1671 /* Create an m32r ELF linker hash table. */
1672 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1674 static struct bfd_link_hash_table *
1675 m32r_elf_link_hash_table_create (abfd)
1678 struct elf_m32r_link_hash_table *ret;
1679 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
1681 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1682 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1685 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1686 m32r_elf_link_hash_newfunc))
1693 ret->sgotplt = NULL;
1694 ret->srelgot = NULL;
1696 ret->srelplt = NULL;
1697 ret->sdynbss = NULL;
1698 ret->srelbss = NULL;
1699 ret->sym_sec.abfd = NULL;
1701 return &ret->root.root;
1704 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1705 shortcuts to them in our hash table. */
1706 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1709 create_got_section (dynobj, info)
1711 struct bfd_link_info *info;
1713 struct elf_m32r_link_hash_table *htab;
1715 if (! _bfd_elf_create_got_section (dynobj, info))
1718 htab = m32r_elf_hash_table (info);
1719 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1720 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1721 if (! htab->sgot || ! htab->sgotplt)
1724 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1725 if (htab->srelgot == NULL
1726 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1731 | SEC_LINKER_CREATED
1733 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1739 /* Create dynamic sections when linking against a dynamic object. */
1742 m32r_elf_create_dynamic_sections (abfd, info)
1744 struct bfd_link_info *info;
1746 struct elf_m32r_link_hash_table *htab;
1747 flagword flags, pltflags;
1748 register asection *s;
1749 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1750 int ptralign = 2; /* 32bit */
1752 htab = m32r_elf_hash_table (info);
1754 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1755 .rel[a].bss sections. */
1757 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1758 | SEC_LINKER_CREATED);
1761 pltflags |= SEC_CODE;
1762 if (bed->plt_not_loaded)
1763 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1764 if (bed->plt_readonly)
1765 pltflags |= SEC_READONLY;
1767 s = bfd_make_section (abfd, ".plt");
1770 || ! bfd_set_section_flags (abfd, s, pltflags)
1771 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1774 if (bed->want_plt_sym)
1776 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1778 struct bfd_link_hash_entry *bh = NULL;
1779 struct elf_link_hash_entry *h;
1780 if (! (_bfd_generic_link_add_one_symbol
1781 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1782 (bfd_vma) 0, (const char *) NULL, FALSE,
1783 get_elf_backend_data (abfd)->collect, &bh)))
1785 h = (struct elf_link_hash_entry *) bh;
1786 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1787 h->type = STT_OBJECT;
1790 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1794 s = bfd_make_section (abfd,
1795 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1798 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1799 || ! bfd_set_section_alignment (abfd, s, ptralign))
1802 if (htab->sgot == NULL
1803 && ! create_got_section (abfd, info))
1807 const char *secname;
1812 for (sec = abfd->sections; sec; sec = sec->next)
1814 secflags = bfd_get_section_flags (abfd, sec);
1815 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1816 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1818 secname = bfd_get_section_name (abfd, sec);
1819 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1820 strcpy (relname, ".rela");
1821 strcat (relname, secname);
1822 if (bfd_get_section_by_name (abfd, secname))
1824 s = bfd_make_section (abfd, relname);
1826 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1827 || ! bfd_set_section_alignment (abfd, s, ptralign))
1832 if (bed->want_dynbss)
1834 /* The .dynbss section is a place to put symbols which are defined
1835 by dynamic objects, are referenced by regular objects, and are
1836 not functions. We must allocate space for them in the process
1837 image and use a R_*_COPY reloc to tell the dynamic linker to
1838 initialize them at run time. The linker script puts the .dynbss
1839 section into the .bss section of the final image. */
1840 s = bfd_make_section (abfd, ".dynbss");
1843 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1845 /* The .rel[a].bss section holds copy relocs. This section is not
1846 normally needed. We need to create it here, though, so that the
1847 linker will map it to an output section. We can't just create it
1848 only if we need it, because we will not know whether we need it
1849 until we have seen all the input files, and the first time the
1850 main linker code calls BFD after examining all the input files
1851 (size_dynamic_sections) the input sections have already been
1852 mapped to the output sections. If the section turns out not to
1853 be needed, we can discard it later. We will never need this
1854 section when generating a shared object, since they do not use
1858 s = bfd_make_section (abfd,
1859 (bed->default_use_rela_p
1860 ? ".rela.bss" : ".rel.bss"));
1863 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1864 || ! bfd_set_section_alignment (abfd, s, ptralign))
1872 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1873 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1874 struct elf_link_hash_entry *,
1875 struct elf_link_hash_entry *);
1878 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1879 struct elf_link_hash_entry *dir,
1880 struct elf_link_hash_entry *ind)
1882 struct elf_m32r_link_hash_entry *edir, *eind;
1884 edir = (struct elf_m32r_link_hash_entry *) dir;
1885 eind = (struct elf_m32r_link_hash_entry *) ind;
1887 if (eind->dyn_relocs != NULL)
1889 if (edir->dyn_relocs != NULL)
1891 struct elf_m32r_dyn_relocs **pp;
1892 struct elf_m32r_dyn_relocs *p;
1894 if (ind->root.type == bfd_link_hash_indirect)
1897 /* Add reloc counts against the weak sym to the strong sym
1898 list. Merge any entries against the same section. */
1899 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1901 struct elf_m32r_dyn_relocs *q;
1903 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1904 if (q->sec == p->sec)
1906 q->pc_count += p->pc_count;
1907 q->count += p->count;
1914 *pp = edir->dyn_relocs;
1917 edir->dyn_relocs = eind->dyn_relocs;
1918 eind->dyn_relocs = NULL;
1921 // if (ind->root.type == bfd_link_hash_indirect
1922 // && dir->got.refcount <= 0)
1924 // edir->tls_type = eind->tls_type;
1925 // eind->tls_type = GOT_UNKNOWN;
1927 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1931 /* Adjust a symbol defined by a dynamic object and referenced by a
1932 regular object. The current definition is in some section of the
1933 dynamic object, but we're not including those sections. We have to
1934 change the definition to something the rest of the link can
1938 m32r_elf_adjust_dynamic_symbol (info, h)
1939 struct bfd_link_info *info;
1940 struct elf_link_hash_entry *h;
1942 struct elf_m32r_link_hash_table *htab;
1943 struct elf_m32r_link_hash_entry *eh;
1944 struct elf_m32r_dyn_relocs *p;
1947 unsigned int power_of_two;
1950 printf("m32r_elf_adjust_dynamic_symbol()\n");
1953 dynobj = elf_hash_table (info)->dynobj;
1955 /* Make sure we know what is going on here. */
1956 BFD_ASSERT (dynobj != NULL
1957 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1958 || h->weakdef != NULL
1959 || ((h->elf_link_hash_flags
1960 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1961 && (h->elf_link_hash_flags
1962 & ELF_LINK_HASH_REF_REGULAR) != 0
1963 && (h->elf_link_hash_flags
1964 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1967 /* If this is a function, put it in the procedure linkage table. We
1968 will fill in the contents of the procedure linkage table later,
1969 when we know the address of the .got section. */
1970 if (h->type == STT_FUNC
1971 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1974 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1975 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1976 && h->root.type != bfd_link_hash_undefweak
1977 && h->root.type != bfd_link_hash_undefined)
1979 /* This case can occur if we saw a PLT reloc in an input
1980 file, but the symbol was never referred to by a dynamic
1981 object. In such a case, we don't actually need to build
1982 a procedure linkage table, and we can just do a PCREL
1984 h->plt.offset = (bfd_vma) -1;
1985 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1991 h->plt.offset = (bfd_vma) -1;
1993 /* If this is a weak symbol, and there is a real definition, the
1994 processor independent code will have arranged for us to see the
1995 real definition first, and we can just use the same value. */
1996 if (h->weakdef != NULL)
1998 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1999 || h->weakdef->root.type == bfd_link_hash_defweak);
2000 h->root.u.def.section = h->weakdef->root.u.def.section;
2001 h->root.u.def.value = h->weakdef->root.u.def.value;
2005 /* This is a reference to a symbol defined by a dynamic object which
2006 is not a function. */
2008 /* If we are creating a shared library, we must presume that the
2009 only references to the symbol are via the global offset table.
2010 For such cases we need not do anything here; the relocations will
2011 be handled correctly by relocate_section. */
2015 /* If there are no references to this symbol that do not use the
2016 GOT, we don't need to generate a copy reloc. */
2017 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2020 /* If -z nocopyreloc was given, we won't generate them either. */
2021 if (info->nocopyreloc)
2023 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2027 eh = (struct elf_m32r_link_hash_entry *) h;
2028 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2030 s = p->sec->output_section;
2031 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2035 /* If we didn't find any dynamic relocs in sections which needs the
2036 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2040 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2044 /* We must allocate the symbol in our .dynbss section, which will
2045 become part of the .bss section of the executable. There will be
2046 an entry for this symbol in the .dynsym section. The dynamic
2047 object will contain position independent code, so all references
2048 from the dynamic object to this symbol will go through the global
2049 offset table. The dynamic linker will use the .dynsym entry to
2050 determine the address it must put in the global offset table, so
2051 both the dynamic object and the regular object will refer to the
2052 same memory location for the variable. */
2054 htab = m32r_elf_hash_table (info);
2056 BFD_ASSERT (s != NULL);
2058 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2059 to copy the initial value out of the dynamic object and into the
2060 runtime process image. We need to remember the offset into the
2061 .rela.bss section we are going to use. */
2062 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2066 srel = htab->srelbss;
2067 BFD_ASSERT (srel != NULL);
2068 srel->size += sizeof (Elf32_External_Rela);
2069 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2072 /* We need to figure out the alignment required for this symbol. I
2073 have no idea how ELF linkers handle this. */
2074 power_of_two = bfd_log2 (h->size);
2075 if (power_of_two > 3)
2078 /* Apply the required alignment. */
2079 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2080 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2082 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2086 /* Define the symbol as being at this point in the section. */
2087 h->root.u.def.section = s;
2088 h->root.u.def.value = s->size;
2090 /* Increment the section size to make room for the symbol. */
2096 /* Allocate space in .plt, .got and associated reloc sections for
2100 allocate_dynrelocs (h, inf)
2101 struct elf_link_hash_entry *h;
2104 struct bfd_link_info *info;
2105 struct elf_m32r_link_hash_table *htab;
2106 struct elf_m32r_link_hash_entry *eh;
2107 struct elf_m32r_dyn_relocs *p;
2109 if (h->root.type == bfd_link_hash_indirect)
2112 if (h->root.type == bfd_link_hash_warning)
2113 /* When warning symbols are created, they **replace** the "real"
2114 entry in the hash table, thus we never get to see the real
2115 symbol in a hash traversal. So look at it now. */
2116 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2118 info = (struct bfd_link_info *) inf;
2119 htab = m32r_elf_hash_table (info);
2121 eh = (struct elf_m32r_link_hash_entry *) h;
2122 // if ((h->got.refcount > 0
2123 // || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2124 // && eh->gotplt_refcount > 0)
2126 // /* The symbol has been forced local, or we have some direct got refs,
2127 // so treat all the gotplt refs as got refs. */
2128 // h->got.refcount += eh->gotplt_refcount;
2129 // if (h->plt.refcount >= eh->gotplt_refcount)
2130 // h->plt.refcount -= eh->gotplt_refcount;
2133 if (htab->root.dynamic_sections_created
2134 && h->plt.refcount > 0)
2136 /* Make sure this symbol is output as a dynamic symbol.
2137 Undefined weak syms won't yet be marked as dynamic. */
2138 if (h->dynindx == -1
2139 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2141 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2145 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2147 asection *s = htab->splt;
2149 /* If this is the first .plt entry, make room for the special
2152 s->size += PLT_ENTRY_SIZE;
2154 h->plt.offset = s->size;
2156 /* If this symbol is not defined in a regular file, and we are
2157 not generating a shared library, then set the symbol to this
2158 location in the .plt. This is required to make function
2159 pointers compare as equal between the normal executable and
2160 the shared library. */
2162 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2164 h->root.u.def.section = s;
2165 h->root.u.def.value = h->plt.offset;
2168 /* Make room for this entry. */
2169 s->size += PLT_ENTRY_SIZE;
2171 /* We also need to make an entry in the .got.plt section, which
2172 will be placed in the .got section by the linker script. */
2173 htab->sgotplt->size += 4;
2175 /* We also need to make an entry in the .rel.plt section. */
2176 htab->srelplt->size += sizeof (Elf32_External_Rela);
2180 h->plt.offset = (bfd_vma) -1;
2181 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2186 h->plt.offset = (bfd_vma) -1;
2187 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2190 if (h->got.refcount > 0)
2195 /* Make sure this symbol is output as a dynamic symbol.
2196 Undefined weak syms won't yet be marked as dynamic. */
2197 if (h->dynindx == -1
2198 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2200 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2206 h->got.offset = s->size;
2208 dyn = htab->root.dynamic_sections_created;
2209 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2210 htab->srelgot->size += sizeof (Elf32_External_Rela);
2213 h->got.offset = (bfd_vma) -1;
2215 if (eh->dyn_relocs == NULL)
2218 /* In the shared -Bsymbolic case, discard space allocated for
2219 dynamic pc-relative relocs against symbols which turn out to be
2220 defined in regular objects. For the normal shared case, discard
2221 space for pc-relative relocs that have become local due to symbol
2222 visibility changes. */
2226 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2227 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2230 struct elf_m32r_dyn_relocs **pp;
2231 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2233 p->count -= p->pc_count;
2244 /* For the non-shared case, discard space for relocs against
2245 symbols which turn out to need copy relocs or are not
2248 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2249 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2250 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2251 || (htab->root.dynamic_sections_created
2252 && (h->root.type == bfd_link_hash_undefweak
2253 || h->root.type == bfd_link_hash_undefined))))
2255 /* Make sure this symbol is output as a dynamic symbol.
2256 Undefined weak syms won't yet be marked as dynamic. */
2257 if (h->dynindx == -1
2258 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2260 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2264 /* If that succeeded, we know we'll be keeping all the
2266 if (h->dynindx != -1)
2270 eh->dyn_relocs = NULL;
2275 /* Finally, allocate space. */
2276 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2278 asection *sreloc = elf_section_data (p->sec)->sreloc;
2279 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2284 /* Find any dynamic relocs that apply to read-only sections. */
2287 readonly_dynrelocs (h, inf)
2288 struct elf_link_hash_entry *h;
2291 struct elf_m32r_link_hash_entry *eh;
2292 struct elf_m32r_dyn_relocs *p;
2294 if (h->root.type == bfd_link_hash_warning)
2295 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2297 eh = (struct elf_m32r_link_hash_entry *) h;
2298 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2300 asection *s = p->sec->output_section;
2302 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2304 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2306 info->flags |= DF_TEXTREL;
2308 /* Not an error, just cut short the traversal. */
2315 /* Set the sizes of the dynamic sections. */
2318 m32r_elf_size_dynamic_sections (output_bfd, info)
2319 bfd *output_bfd ATTRIBUTE_UNUSED;
2320 struct bfd_link_info *info;
2322 struct elf_m32r_link_hash_table *htab;
2329 printf("m32r_elf_size_dynamic_sections()\n");
2332 htab = m32r_elf_hash_table (info);
2333 dynobj = htab->root.dynobj;
2334 BFD_ASSERT (dynobj != NULL);
2336 if (htab->root.dynamic_sections_created)
2338 /* Set the contents of the .interp section to the interpreter. */
2341 s = bfd_get_section_by_name (dynobj, ".interp");
2342 BFD_ASSERT (s != NULL);
2343 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2344 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2348 /* Set up .got offsets for local syms, and space for local dynamic
2350 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2352 bfd_signed_vma *local_got;
2353 bfd_signed_vma *end_local_got;
2354 bfd_size_type locsymcount;
2355 Elf_Internal_Shdr *symtab_hdr;
2358 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2361 for (s = ibfd->sections; s != NULL; s = s->next)
2363 struct elf_m32r_dyn_relocs *p;
2365 for (p = ((struct elf_m32r_dyn_relocs *)
2366 elf_section_data (s)->local_dynrel);
2370 if (! bfd_is_abs_section (p->sec)
2371 && bfd_is_abs_section (p->sec->output_section))
2373 /* Input section has been discarded, either because
2374 it is a copy of a linkonce section or due to
2375 linker script /DISCARD/, so we'll be discarding
2378 else if (p->count != 0)
2380 srel = elf_section_data (p->sec)->sreloc;
2381 srel->size += p->count * sizeof (Elf32_External_Rela);
2382 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2383 info->flags |= DF_TEXTREL;
2388 local_got = elf_local_got_refcounts (ibfd);
2392 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2393 locsymcount = symtab_hdr->sh_info;
2394 end_local_got = local_got + locsymcount;
2396 srel = htab->srelgot;
2397 for (; local_got < end_local_got; ++local_got)
2401 *local_got = s->size;
2404 srel->size += sizeof (Elf32_External_Rela);
2407 *local_got = (bfd_vma) -1;
2411 /* Allocate global sym .plt and .got entries, and space for global
2412 sym dynamic relocs. */
2413 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2415 /* We now have determined the sizes of the various dynamic sections.
2416 Allocate memory for them. */
2418 for (s = dynobj->sections; s != NULL; s = s->next)
2420 if ((s->flags & SEC_LINKER_CREATED) == 0)
2425 || s == htab->sgotplt)
2427 /* Strip this section if we don't need it; see the
2430 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2432 if (s->size != 0 && s != htab->srelplt)
2435 /* We use the reloc_count field as a counter if we need
2436 to copy relocs into the output file. */
2441 /* It's not one of our sections, so don't allocate space. */
2447 /* If we don't need this section, strip it from the
2448 output file. This is mostly to handle .rela.bss and
2449 .rela.plt. We must create both sections in
2450 create_dynamic_sections, because they must be created
2451 before the linker maps input sections to output
2452 sections. The linker does that before
2453 adjust_dynamic_symbol is called, and it is that
2454 function which decides whether anything needs to go
2455 into these sections. */
2456 _bfd_strip_section_from_output (info, s);
2460 /* Allocate memory for the section contents. We use bfd_zalloc
2461 here in case unused entries are not reclaimed before the
2462 section's contents are written out. This should not happen,
2463 but this way if it does, we get a R_M32R_NONE reloc instead
2465 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2466 if (s->contents == NULL)
2470 if (htab->root.dynamic_sections_created)
2472 /* Add some entries to the .dynamic section. We fill in the
2473 values later, in m32r_elf_finish_dynamic_sections, but we
2474 must add the entries now so that we get the correct size for
2475 the .dynamic section. The DT_DEBUG entry is filled in by the
2476 dynamic linker and used by the debugger. */
2477 #define add_dynamic_entry(TAG, VAL) \
2478 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2482 if (! add_dynamic_entry (DT_DEBUG, 0))
2486 if (htab->splt->size != 0)
2488 if (! add_dynamic_entry (DT_PLTGOT, 0)
2489 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2490 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2491 || ! add_dynamic_entry (DT_JMPREL, 0))
2497 if (! add_dynamic_entry (DT_RELA, 0)
2498 || ! add_dynamic_entry (DT_RELASZ, 0)
2499 || ! add_dynamic_entry (DT_RELAENT,
2500 sizeof (Elf32_External_Rela)))
2503 /* If any dynamic relocs apply to a read-only section,
2504 then we need a DT_TEXTREL entry. */
2505 if ((info->flags & DF_TEXTREL) == 0)
2506 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2509 if ((info->flags & DF_TEXTREL) != 0)
2511 if (! add_dynamic_entry (DT_TEXTREL, 0))
2516 #undef add_dynamic_entry
2520 /* Relocate an M32R/D ELF section.
2521 There is some attempt to make this function usable for many architectures,
2522 both for RELA and REL type relocs, if only to serve as a learning tool.
2524 The RELOCATE_SECTION function is called by the new ELF backend linker
2525 to handle the relocations for a section.
2527 The relocs are always passed as Rela structures; if the section
2528 actually uses Rel structures, the r_addend field will always be
2531 This function is responsible for adjust the section contents as
2532 necessary, and (if using Rela relocs and generating a
2533 relocatable output file) adjusting the reloc addend as
2536 This function does not have to worry about setting the reloc
2537 address or the reloc symbol index.
2539 LOCAL_SYMS is a pointer to the swapped in local symbols.
2541 LOCAL_SECTIONS is an array giving the section in the input file
2542 corresponding to the st_shndx field of each local symbol.
2544 The global hash table entry for the global symbols can be found
2545 via elf_sym_hashes (input_bfd).
2547 When generating relocatable output, this function must handle
2548 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2549 going to be the section symbol corresponding to the output
2550 section, which means that the addend must be adjusted
2554 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2555 contents, relocs, local_syms, local_sections)
2556 bfd *output_bfd ATTRIBUTE_UNUSED;
2557 struct bfd_link_info *info;
2559 asection *input_section;
2561 Elf_Internal_Rela *relocs;
2562 Elf_Internal_Sym *local_syms;
2563 asection **local_sections;
2565 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2566 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2567 Elf_Internal_Rela *rel, *relend;
2568 /* Assume success. */
2569 bfd_boolean ret = TRUE;
2571 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2573 bfd_vma *local_got_offsets;
2574 asection *sgot, *splt, *sreloc;
2575 bfd_vma high_address = bfd_get_section_limit (input_bfd, input_section);
2577 dynobj = htab->root.dynobj;
2578 local_got_offsets = elf_local_got_offsets (input_bfd);
2585 relend = relocs + input_section->reloc_count;
2586 for (; rel < relend; rel++)
2589 reloc_howto_type *howto;
2590 unsigned long r_symndx;
2591 struct elf_link_hash_entry *h;
2592 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2593 ensure it's zero (we use REL relocs, not RELA). Therefore this
2594 should be assigning zero to `addend', but for clarity we use
2596 bfd_vma addend = rel->r_addend;
2597 bfd_vma offset = rel->r_offset;
2598 Elf_Internal_Sym *sym;
2600 const char *sym_name;
2601 bfd_reloc_status_type r;
2602 const char *errmsg = NULL;
2603 bfd_boolean use_rel = FALSE;
2606 r_type = ELF32_R_TYPE (rel->r_info);
2607 if (r_type < 0 || r_type >= (int) R_M32R_max)
2609 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2610 bfd_archive_filename (input_bfd),
2612 bfd_set_error (bfd_error_bad_value);
2617 if (r_type == R_M32R_GNU_VTENTRY
2618 || r_type == R_M32R_GNU_VTINHERIT
2619 || r_type == R_M32R_NONE
2620 || r_type == R_M32R_RELA_GNU_VTENTRY
2621 || r_type == R_M32R_RELA_GNU_VTINHERIT)
2624 if (r_type <= R_M32R_GNU_VTENTRY)
2627 howto = m32r_elf_howto_table + r_type;
2628 r_symndx = ELF32_R_SYM (rel->r_info);
2630 if (info->relocatable && (use_rel == TRUE))
2632 /* This is a relocatable link. We don't have to change
2633 anything, unless the reloc is against a section symbol,
2634 in which case we have to adjust according to where the
2635 section symbol winds up in the output section. */
2637 if (r_symndx >= symtab_hdr->sh_info)
2639 /* External symbol. */
2644 sym = local_syms + r_symndx;
2645 sym_name = "<local symbol>";
2646 /* STT_SECTION: symbol is associated with a section. */
2647 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2649 /* Symbol isn't associated with a section. Nothing to do. */
2653 sec = local_sections[r_symndx];
2654 addend += sec->output_offset + sym->st_value;
2656 /* If partial_inplace, we need to store any additional addend
2657 back in the section. */
2658 if (! howto->partial_inplace)
2660 /* ??? Here is a nice place to call a special_function
2662 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2663 r = _bfd_relocate_contents (howto, input_bfd,
2664 addend, contents + offset);
2667 Elf_Internal_Rela *lorel;
2669 /* We allow an arbitrary number of HI16 relocs before the
2670 LO16 reloc. This permits gcc to emit the HI and LO relocs
2672 for (lorel = rel + 1;
2674 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2675 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2679 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2681 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2686 r = _bfd_relocate_contents (howto, input_bfd,
2687 addend, contents + offset);
2694 /* This is a final link. */
2699 if (r_symndx < symtab_hdr->sh_info)
2702 sym = local_syms + r_symndx;
2703 sec = local_sections[r_symndx];
2704 sym_name = "<local symbol>";
2706 if (use_rel == FALSE)
2708 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2709 addend = rel->r_addend;
2711 if (info->relocatable)
2713 /* This is a relocatable link. We don't have to change
2714 anything, unless the reloc is against a section symbol,
2715 in which case we have to adjust according to where the
2716 section symbol winds up in the output section. */
2717 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2718 rel->r_addend += sec->output_offset + sym->st_value;
2725 relocation = (sec->output_section->vma
2726 + sec->output_offset
2732 /* External symbol. */
2733 if (info->relocatable && (use_rel == FALSE))
2736 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2737 while (h->root.type == bfd_link_hash_indirect
2738 || h->root.type == bfd_link_hash_warning)
2739 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2740 sym_name = h->root.root.string;
2742 if (h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak)
2746 sec = h->root.u.def.section;
2748 dyn = htab->root.dynamic_sections_created;
2749 sec = h->root.u.def.section;
2750 if (r_type == R_M32R_GOTPC24
2751 || (r_type == R_M32R_GOTPC_HI_ULO
2752 || r_type == R_M32R_GOTPC_HI_SLO
2753 || r_type == R_M32R_GOTPC_LO)
2754 || (r_type == R_M32R_26_PLTREL
2755 && h->plt.offset != (bfd_vma) -1)
2756 || ((r_type == R_M32R_GOT24
2757 || r_type == R_M32R_GOT16_HI_ULO
2758 || r_type == R_M32R_GOT16_HI_SLO
2759 || r_type == R_M32R_GOT16_LO)
2760 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2763 || (! info->symbolic && h->dynindx != -1)
2764 || (h->elf_link_hash_flags
2765 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2767 && ((! info->symbolic && h->dynindx != -1)
2768 || (h->elf_link_hash_flags
2769 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2770 && (((r_type == R_M32R_16_RELA
2771 || r_type == R_M32R_32_RELA
2772 || r_type == R_M32R_24_RELA
2773 || r_type == R_M32R_HI16_ULO_RELA
2774 || r_type == R_M32R_HI16_SLO_RELA
2775 || r_type == R_M32R_LO16_RELA)
2776 && (h->elf_link_hash_flags
2777 & ELF_LINK_FORCED_LOCAL) == 0)
2778 || r_type == R_M32R_10_PCREL_RELA
2779 || r_type == R_M32R_18_PCREL_RELA
2780 || r_type == R_M32R_26_PCREL_RELA)
2781 && ((input_section->flags & SEC_ALLOC) != 0
2782 /* DWARF will emit R_M32R_16(24,32) relocations
2783 in its sections against symbols defined
2784 externally in shared libraries. We can't do
2785 anything with them here. */
2786 || ((input_section->flags & SEC_DEBUGGING) != 0
2787 && (h->elf_link_hash_flags
2788 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
2790 /* In these cases, we don't need the relocation
2791 value. We check specially because in some
2792 obscure cases sec->output_section will be NULL. */
2795 else if (sec->output_section == NULL)
2797 (*_bfd_error_handler)
2798 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2799 bfd_get_filename (input_bfd), h->root.root.string,
2800 bfd_get_section_name (input_bfd, input_section));
2805 relocation = (h->root.u.def.value
2806 + sec->output_section->vma
2807 + sec->output_offset);
2809 else if (h->root.type == bfd_link_hash_undefweak)
2811 else if (info->unresolved_syms_in_objects == RM_IGNORE
2812 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2816 if (! ((*info->callbacks->undefined_symbol)
2817 (info, h->root.root.string, input_bfd,
2818 input_section, offset,
2819 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2820 || ELF_ST_VISIBILITY (h->other)))))
2826 /* Sanity check the address. */
2827 if (offset > high_address)
2829 r = bfd_reloc_outofrange;
2833 switch ((int) r_type)
2836 /* Relocation is relative to the start of the global offset
2837 table (for ld24 rx, #uimm24). eg access at label+addend
2839 ld24 rx. #label@GOTOFF + addend
2842 BFD_ASSERT (sgot != NULL);
2844 relocation = -(relocation - sgot->output_section->vma);
2845 rel->r_addend = -rel->r_addend;
2848 case R_M32R_GOTOFF_HI_ULO:
2849 case R_M32R_GOTOFF_HI_SLO:
2850 case R_M32R_GOTOFF_LO:
2851 BFD_ASSERT (sgot != NULL);
2853 relocation -= sgot->output_section->vma;
2855 if ((r_type == R_M32R_GOTOFF_HI_SLO)
2856 && ((relocation + rel->r_addend) & 0x8000))
2857 rel->r_addend += 0x10000;
2860 case R_M32R_GOTPC24:
2861 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2862 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2864 relocation = sgot->output_section->vma;
2867 case R_M32R_GOTPC_HI_ULO:
2868 case R_M32R_GOTPC_HI_SLO:
2869 case R_M32R_GOTPC_LO:
2871 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2873 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2874 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2877 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2878 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2880 relocation = sgot->output_section->vma;
2881 relocation -= (input_section->output_section->vma
2882 + input_section->output_offset
2884 if ((r_type == R_M32R_GOTPC_HI_SLO)
2885 && ((relocation + rel->r_addend) & 0x8000))
2886 rel->r_addend += 0x10000;
2890 case R_M32R_GOT16_HI_ULO:
2891 case R_M32R_GOT16_HI_SLO:
2892 case R_M32R_GOT16_LO:
2895 /* Relocation is to the entry for this symbol in the global
2897 BFD_ASSERT (sgot != NULL);
2904 off = h->got.offset;
2905 BFD_ASSERT (off != (bfd_vma) -1);
2907 dyn = htab->root.dynamic_sections_created;
2908 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2912 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2913 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2915 /* This is actually a static link, or it is a
2916 -Bsymbolic link and the symbol is defined
2917 locally, or the symbol was forced to be local
2918 because of a version file. We must initialize
2919 this entry in the global offset table. Since the
2920 offset must always be a multiple of 4, we use the
2921 least significant bit to record whether we have
2922 initialized it already.
2924 When doing a dynamic link, we create a .rela.got
2925 relocation entry to initialize the value. This
2926 is done in the finish_dynamic_symbol routine. */
2931 bfd_put_32 (output_bfd, relocation,
2932 sgot->contents + off);
2937 relocation = sgot->output_offset + off;
2944 BFD_ASSERT (local_got_offsets != NULL
2945 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2947 off = local_got_offsets[r_symndx];
2949 /* The offset must always be a multiple of 4. We use
2950 the least significant bit to record whether we have
2951 already processed this entry. */
2956 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2961 Elf_Internal_Rela outrel;
2963 /* We need to generate a R_M32R_RELATIVE reloc
2964 for the dynamic linker. */
2965 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2966 BFD_ASSERT (srelgot != NULL);
2968 outrel.r_offset = (sgot->output_section->vma
2969 + sgot->output_offset
2971 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2972 outrel.r_addend = relocation;
2973 loc = srelgot->contents;
2974 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2975 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2976 ++srelgot->reloc_count;
2979 local_got_offsets[r_symndx] |= 1;
2982 relocation = sgot->output_offset + off;
2984 if ((r_type == R_M32R_GOT16_HI_SLO)
2985 && ((relocation + rel->r_addend) & 0x8000))
2986 rel->r_addend += 0x10000;
2990 case R_M32R_26_PLTREL:
2991 /* Relocation is to the entry for this symbol in the
2992 procedure linkage table. */
2994 /* The native assembler will generate a 26_PLTREL reloc
2995 for a local symbol if you assemble a call from one
2996 section to another when using -K pic. */
3000 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3001 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3003 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
3006 if (h->plt.offset == (bfd_vma) -1)
3008 /* We didn't make a PLT entry for this symbol. This
3009 happens when statically linking PIC code, or when
3010 using -Bsymbolic. */
3014 relocation = (splt->output_section->vma
3015 + splt->output_offset
3019 case R_M32R_HI16_SLO_RELA:
3021 if ((relocation + rel->r_addend) & 0x8000)
3023 rel->r_addend += 0x10000;
3027 case R_M32R_16_RELA:
3028 case R_M32R_24_RELA:
3029 case R_M32R_32_RELA:
3030 case R_M32R_18_PCREL_RELA:
3031 case R_M32R_26_PCREL_RELA:
3032 case R_M32R_HI16_ULO_RELA:
3033 case R_M32R_LO16_RELA:
3034 case R_M32R_SDA16_RELA:
3037 && (input_section->flags & SEC_ALLOC) != 0
3038 && ((r_type != R_M32R_18_PCREL_RELA
3039 && r_type != R_M32R_26_PCREL_RELA)
3042 && (! info->symbolic
3043 || (h->elf_link_hash_flags
3044 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3046 Elf_Internal_Rela outrel;
3047 bfd_boolean skip, relocate;
3050 /* When generating a shared object, these relocations
3051 are copied into the output file to be resolved at run
3058 name = (bfd_elf_string_from_elf_section
3060 elf_elfheader (input_bfd)->e_shstrndx,
3061 elf_section_data (input_section)->rel_hdr.sh_name));
3065 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3066 && strcmp (bfd_get_section_name (input_bfd,
3070 sreloc = bfd_get_section_by_name (dynobj, name);
3071 BFD_ASSERT (sreloc != NULL);
3077 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3081 if (outrel.r_offset == (bfd_vma) -1)
3083 else if (outrel.r_offset == (bfd_vma) -2)
3084 skip = TRUE, relocate = TRUE;
3085 outrel.r_offset += (input_section->output_section->vma
3086 + input_section->output_offset);
3089 memset (&outrel, 0, sizeof outrel);
3090 else if (r_type == R_M32R_18_PCREL_RELA
3091 || r_type == R_M32R_26_PCREL_RELA)
3093 BFD_ASSERT (h != NULL && h->dynindx != -1);
3094 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3095 outrel.r_addend = rel->r_addend;
3099 /* h->dynindx may be -1 if this symbol was marked to
3102 || ((info->symbolic || h->dynindx == -1)
3103 && (h->elf_link_hash_flags
3104 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3107 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3108 outrel.r_addend = relocation + rel->r_addend;
3112 BFD_ASSERT (h->dynindx != -1);
3113 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3114 outrel.r_addend = relocation + rel->r_addend;
3118 loc = sreloc->contents;
3119 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3120 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3121 ++sreloc->reloc_count;
3123 /* If this reloc is against an external symbol, we do
3124 not want to fiddle with the addend. Otherwise, we
3125 need to include the symbol value so that it becomes
3126 an addend for the dynamic reloc. */
3132 case (int) R_M32R_10_PCREL :
3133 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3135 sec, relocation, addend);
3138 case (int) R_M32R_HI16_SLO :
3139 case (int) R_M32R_HI16_ULO :
3141 Elf_Internal_Rela *lorel;
3143 /* We allow an arbitrary number of HI16 relocs before the
3144 LO16 reloc. This permits gcc to emit the HI and LO relocs
3146 for (lorel = rel + 1;
3148 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3149 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3153 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3155 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3156 contents, relocation + addend);
3160 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3162 relocation, addend);
3167 case (int) R_M32R_SDA16 :
3171 BFD_ASSERT (sec != NULL);
3172 name = bfd_get_section_name (abfd, sec);
3174 if (strcmp (name, ".sdata") == 0
3175 || strcmp (name, ".sbss") == 0
3176 || strcmp (name, ".scommon") == 0)
3179 bfd *out_bfd = sec->output_section->owner;
3181 r = m32r_elf_final_sda_base (out_bfd, info,
3184 if (r != bfd_reloc_ok)
3190 /* At this point `relocation' contains the object's
3192 relocation -= sda_base;
3193 /* Now it contains the offset from _SDA_BASE_. */
3197 (*_bfd_error_handler)
3198 (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
3199 bfd_archive_filename (input_bfd),
3201 m32r_elf_howto_table[(int) r_type].name,
3202 bfd_get_section_name (abfd, sec));
3203 /*bfd_set_error (bfd_error_bad_value); ??? why? */
3210 default : /* OLD_M32R_RELOC */
3212 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3214 relocation, addend);
3218 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3219 contents, rel->r_offset,
3220 relocation, rel->r_addend);
3226 if (r != bfd_reloc_ok)
3228 /* FIXME: This should be generic enough to go in a utility. */
3232 name = h->root.root.string;
3235 name = (bfd_elf_string_from_elf_section
3236 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3237 if (name == NULL || *name == '\0')
3238 name = bfd_section_name (input_bfd, sec);
3246 case bfd_reloc_overflow:
3247 if (! ((*info->callbacks->reloc_overflow)
3248 (info, name, howto->name, (bfd_vma) 0,
3249 input_bfd, input_section, offset)))
3253 case bfd_reloc_undefined:
3254 if (! ((*info->callbacks->undefined_symbol)
3255 (info, name, input_bfd, input_section,
3260 case bfd_reloc_outofrange:
3261 errmsg = _("internal error: out of range error");
3264 case bfd_reloc_notsupported:
3265 errmsg = _("internal error: unsupported relocation error");
3268 case bfd_reloc_dangerous:
3269 errmsg = _("internal error: dangerous error");
3273 errmsg = _("internal error: unknown error");
3277 if (!((*info->callbacks->warning)
3278 (info, errmsg, name, input_bfd, input_section,
3289 /* Finish up dynamic symbol handling. We set the contents of various
3290 dynamic sections here. */
3292 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3294 struct bfd_link_info *info;
3295 struct elf_link_hash_entry *h;
3296 Elf_Internal_Sym *sym;
3298 struct elf_m32r_link_hash_table *htab;
3303 printf("m32r_elf_finish_dynamic_symbol()\n");
3306 htab = m32r_elf_hash_table (info);
3307 dynobj = htab->root.dynobj;
3309 if (h->plt.offset != (bfd_vma) -1)
3317 Elf_Internal_Rela rela;
3319 /* This symbol has an entry in the procedure linkage table. Set
3322 BFD_ASSERT (h->dynindx != -1);
3325 sgot = htab->sgotplt;
3326 srela = htab->srelplt;
3327 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3329 /* Get the index in the procedure linkage table which
3330 corresponds to this symbol. This is the index of this symbol
3331 in all the symbols for which we are making plt entries. The
3332 first entry in the procedure linkage table is reserved. */
3333 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3335 /* Get the offset into the .got table of the entry that
3336 corresponds to this function. Each .got entry is 4 bytes.
3337 The first three are reserved. */
3338 got_offset = (plt_index + 3) * 4;
3340 /* Fill in the entry in the procedure linkage table. */
3343 bfd_put_32 (output_bfd,
3345 + (((sgot->output_section->vma
3346 + sgot->output_offset
3347 + got_offset) >> 16) & 0xffff)),
3348 splt->contents + h->plt.offset);
3349 bfd_put_32 (output_bfd,
3351 + ((sgot->output_section->vma
3352 + sgot->output_offset
3353 + got_offset) & 0xffff)),
3354 splt->contents + h->plt.offset + 4);
3355 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3356 splt->contents + h->plt.offset + 8);
3357 bfd_put_32 (output_bfd,
3359 + plt_index * sizeof (Elf32_External_Rela)),
3360 splt->contents + h->plt.offset + 12);
3361 bfd_put_32 (output_bfd,
3363 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3364 splt->contents + h->plt.offset + 16);
3368 bfd_put_32 (output_bfd,
3369 PLT_ENTRY_WORD0 + got_offset,
3370 splt->contents + h->plt.offset);
3371 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3372 splt->contents + h->plt.offset + 4);
3373 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3374 splt->contents + h->plt.offset + 8);
3375 bfd_put_32 (output_bfd,
3377 + plt_index * sizeof (Elf32_External_Rela)),
3378 splt->contents + h->plt.offset + 12);
3379 bfd_put_32 (output_bfd,
3381 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3382 splt->contents + h->plt.offset + 16);
3385 /* Fill in the entry in the global offset table. */
3386 bfd_put_32 (output_bfd,
3387 (splt->output_section->vma
3388 + splt->output_offset
3390 + 12), /* same offset */
3391 sgot->contents + got_offset);
3393 /* Fill in the entry in the .rela.plt section. */
3394 rela.r_offset = (sgot->output_section->vma
3395 + sgot->output_offset
3397 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3399 loc = srela->contents;
3400 loc += plt_index * sizeof(Elf32_External_Rela);
3401 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3403 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3405 /* Mark the symbol as undefined, rather than as defined in
3406 the .plt section. Leave the value alone. */
3407 sym->st_shndx = SHN_UNDEF;
3411 if (h->got.offset != (bfd_vma) -1)
3415 Elf_Internal_Rela rela;
3417 /* This symbol has an entry in the global offset table. Set it
3421 srela = htab->srelgot;
3422 BFD_ASSERT (sgot != NULL && srela != NULL);
3424 rela.r_offset = (sgot->output_section->vma
3425 + sgot->output_offset
3426 + (h->got.offset &~ 1));
3428 /* If this is a -Bsymbolic link, and the symbol is defined
3429 locally, we just want to emit a RELATIVE reloc. Likewise if
3430 the symbol was forced to be local because of a version file.
3431 The entry in the global offset table will already have been
3432 initialized in the relocate_section function. */
3436 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3437 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3439 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3440 rela.r_addend = (h->root.u.def.value
3441 + h->root.u.def.section->output_section->vma
3442 + h->root.u.def.section->output_offset);
3446 BFD_ASSERT((h->got.offset & 1) == 0);
3447 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3448 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3452 loc = srela->contents;
3453 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3454 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3455 ++srela->reloc_count;
3458 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3461 Elf_Internal_Rela rela;
3463 /* This symbols needs a copy reloc. Set it up. */
3465 BFD_ASSERT (h->dynindx != -1
3466 && (h->root.type == bfd_link_hash_defined
3467 || h->root.type == bfd_link_hash_defweak));
3469 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3471 BFD_ASSERT (s != NULL);
3473 rela.r_offset = (h->root.u.def.value
3474 + h->root.u.def.section->output_section->vma
3475 + h->root.u.def.section->output_offset);
3476 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3479 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3480 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3484 /* Mark some specially defined symbols as absolute. */
3485 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3486 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3487 sym->st_shndx = SHN_ABS;
3493 /* Finish up the dynamic sections. */
3496 m32r_elf_finish_dynamic_sections (output_bfd, info)
3498 struct bfd_link_info *info;
3500 struct elf_m32r_link_hash_table *htab;
3506 printf("m32r_elf_finish_dynamic_sections()\n");
3509 htab = m32r_elf_hash_table (info);
3510 dynobj = htab->root.dynobj;
3512 sgot = htab->sgotplt;
3513 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3515 if (htab->root.dynamic_sections_created)
3518 Elf32_External_Dyn *dyncon, *dynconend;
3520 BFD_ASSERT (sgot != NULL && sdyn != NULL);
3522 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3523 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3525 for (; dyncon < dynconend; dyncon++)
3527 Elf_Internal_Dyn dyn;
3531 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3540 s = htab->sgot->output_section;
3544 s = htab->srelplt->output_section;
3546 BFD_ASSERT (s != NULL);
3547 dyn.d_un.d_ptr = s->vma;
3548 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3552 s = htab->srelplt->output_section;
3553 BFD_ASSERT (s != NULL);
3554 dyn.d_un.d_val = s->size;
3555 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3559 /* My reading of the SVR4 ABI indicates that the
3560 procedure linkage table relocs (DT_JMPREL) should be
3561 included in the overall relocs (DT_RELA). This is
3562 what Solaris does. However, UnixWare can not handle
3563 that case. Therefore, we override the DT_RELASZ entry
3564 here to make it not include the JMPREL relocs. Since
3565 the linker script arranges for .rela.plt to follow all
3566 other relocation sections, we don't have to worry
3567 about changing the DT_RELA entry. */
3568 if (htab->srelplt != NULL)
3570 s = htab->srelplt->output_section;
3571 dyn.d_un.d_val -= s->size;
3573 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3578 /* Fill in the first entry in the procedure linkage table. */
3580 if (splt && splt->size > 0)
3584 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3585 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3586 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3587 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3588 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3593 /* addr = .got + 4 */
3594 addr = sgot->output_section->vma + sgot->output_offset + 4;
3595 bfd_put_32 (output_bfd,
3596 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3598 bfd_put_32 (output_bfd,
3599 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3600 splt->contents + 4);
3601 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3602 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3603 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3606 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3611 /* Fill in the first three entries in the global offset table. */
3612 if (sgot && sgot->size > 0)
3615 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3617 bfd_put_32 (output_bfd,
3618 sdyn->output_section->vma + sdyn->output_offset,
3620 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3621 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3623 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3630 #if 0 /* relaxing not supported yet */
3632 /* This function handles relaxing for the m32r.
3633 Relaxing on the m32r is tricky because of instruction alignment
3634 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3636 The following relaxing opportunities are handled:
3638 seth/add3/jl -> bl24 or bl8
3641 It would be nice to handle bl24 -> bl8 but given:
3643 - 4 byte insns must be on 4 byte boundaries
3644 - branch instructions only branch to insns on 4 byte boundaries
3646 this isn't much of a win because the insn in the 2 "deleted" bytes
3647 must become a nop. With some complexity some real relaxation could be
3648 done but the frequency just wouldn't make it worth it; it's better to
3649 try to do all the code compaction one can elsewhere.
3650 When the chip supports parallel 16 bit insns, things may change.
3654 m32r_elf_relax_section (abfd, sec, link_info, again)
3657 struct bfd_link_info *link_info;
3660 Elf_Internal_Shdr *symtab_hdr;
3661 /* The Rela structures are used here because that's what
3662 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
3664 Elf_Internal_Rela *internal_relocs = NULL;
3665 Elf_Internal_Rela *irel, *irelend;
3666 bfd_byte *contents = NULL;
3667 Elf_Internal_Sym *isymbuf = NULL;
3669 /* Assume nothing changes. */
3672 /* We don't have to do anything for a relocatable link, if
3673 this section does not have relocs, or if this is not a
3675 if (link_info->relocatable
3676 || (sec->flags & SEC_RELOC) == 0
3677 || sec->reloc_count == 0
3678 || (sec->flags & SEC_CODE) == 0
3679 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
3682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3684 /* Get a copy of the native relocations. */
3685 internal_relocs = (_bfd_elf_link_read_relocs
3686 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3687 link_info->keep_memory));
3688 if (internal_relocs == NULL)
3691 /* Walk through them looking for relaxing opportunities. */
3692 irelend = internal_relocs + sec->reloc_count;
3693 for (irel = internal_relocs; irel < irelend; irel++)
3697 /* If this isn't something that can be relaxed, then ignore
3699 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3702 /* Get the section contents if we haven't done so already. */
3703 if (contents == NULL)
3705 /* Get cached copy if it exists. */
3706 if (elf_section_data (sec)->this_hdr.contents != NULL)
3707 contents = elf_section_data (sec)->this_hdr.contents;
3710 /* Go get them off disk. */
3711 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3716 /* Read this BFD's local symbols if we haven't done so already. */
3717 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3719 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3720 if (isymbuf == NULL)
3721 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3722 symtab_hdr->sh_info, 0,
3724 if (isymbuf == NULL)
3728 /* Get the value of the symbol referred to by the reloc. */
3729 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3731 /* A local symbol. */
3732 Elf_Internal_Sym *isym;
3735 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3736 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3737 symval = (isym->st_value
3738 + sym_sec->output_section->vma
3739 + sym_sec->output_offset);
3744 struct elf_link_hash_entry *h;
3746 /* An external symbol. */
3747 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3748 h = elf_sym_hashes (abfd)[indx];
3749 BFD_ASSERT (h != NULL);
3750 if (h->root.type != bfd_link_hash_defined
3751 && h->root.type != bfd_link_hash_defweak)
3753 /* This appears to be a reference to an undefined
3754 symbol. Just ignore it--it will be caught by the
3755 regular reloc processing. */
3759 symval = (h->root.u.def.value
3760 + h->root.u.def.section->output_section->vma
3761 + h->root.u.def.section->output_offset);
3764 /* For simplicity of coding, we are going to modify the section
3765 contents, the section relocs, and the BFD symbol table. We
3766 must tell the rest of the code not to free up this
3767 information. It would be possible to instead create a table
3768 of changes which have to be made, as is done in coff-mips.c;
3769 that would be more work, but would require less memory when
3770 the linker is run. */
3772 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3773 This sequence is generated by the compiler when compiling in
3774 32 bit mode. Also look for seth/add3 -> ld24. */
3776 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3778 Elf_Internal_Rela *nrel;
3779 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3781 bfd_signed_vma pcrel_value = symval - pc;
3782 unsigned int code,reg;
3783 int addend,nop_p,bl8_p,to_delete;
3785 /* The tests are ordered so that we get out as quickly as possible
3786 if this isn't something we can relax, taking into account that
3787 we are looking for two separate possibilities (jl/ld24). */
3789 /* Do nothing if no room in the section for this to be what we're
3791 if (irel->r_offset > sec->size - 8)
3794 /* Make sure the next relocation applies to the next
3795 instruction and that it's the add3's reloc. */
3798 || irel->r_offset + 4 != nrel->r_offset
3799 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3802 /* See if the instructions are seth/add3. */
3803 /* FIXME: This is where macros from cgen can come in. */
3804 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3805 if ((code & 0xf0ff) != 0xd0c0)
3806 continue; /* not seth rN,foo */
3807 reg = (code & 0x0f00) >> 8;
3808 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3809 if (code != (0x80a0 | reg | (reg << 8)))
3810 continue; /* not add3 rN,rN,foo */
3812 /* At this point we've confirmed we have seth/add3. Now check
3813 whether the next insn is a jl, in which case try to change this
3816 /* Ensure the branch target is in range.
3817 The bl24 instruction has a 24 bit operand which is the target
3818 address right shifted by 2, giving a signed range of 26 bits.
3819 Note that 4 bytes are added to the high value because the target
3820 will be at least 4 bytes closer if we can relax. It'll actually
3821 be 4 or 8 bytes closer, but we don't know which just yet and
3822 the difference isn't significant enough to worry about. */
3823 #if !USE_REL /* put in for learning purposes */
3824 pcrel_value += irel->r_addend;
3826 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3827 pcrel_value += addend;
3830 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3831 /* Do nothing if no room in the section for this to be what we're
3833 && (irel->r_offset <= sec->size - 12)
3834 /* Ensure the next insn is "jl rN". */
3835 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3836 code != (0x1ec0 | reg)))
3838 /* We can relax to bl24/bl8. */
3840 /* See if there's a nop following the jl.
3841 Also see if we can use a bl8 insn. */
3842 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3843 nop_p = (code & 0x7fff) == NOP_INSN;
3844 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3848 /* Change "seth rN,foo" to "bl8 foo || nop".
3849 We OR in CODE just in case it's not a nop (technically,
3850 CODE currently must be a nop, but for cleanness we
3851 allow it to be anything). */
3852 #if !USE_REL /* put in for learning purposes */
3853 code = 0x7e000000 | MAKE_PARALLEL (code);
3855 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3861 /* Change the seth rN,foo to a bl24 foo. */
3862 #if !USE_REL /* put in for learning purposes */
3865 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3867 to_delete = nop_p ? 8 : 4;
3870 bfd_put_32 (abfd, code, contents + irel->r_offset);
3872 /* Set the new reloc type. */
3873 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3874 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3876 /* Delete the add3 reloc by making it a null reloc. */
3877 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3880 else if (addend >= 0
3881 && symval + addend <= 0xffffff)
3883 /* We can relax to ld24. */
3885 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3886 bfd_put_32 (abfd, code, contents + irel->r_offset);
3888 /* Tell the following code a nop filler isn't needed. */
3893 /* Can't do anything here. */
3897 /* Note that we've changed the relocs, section contents, etc. */
3898 elf_section_data (sec)->relocs = internal_relocs;
3899 elf_section_data (sec)->this_hdr.contents = contents;
3900 symtab_hdr->contents = (unsigned char *) isymbuf;
3902 /* Delete TO_DELETE bytes of data. */
3903 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3904 irel->r_offset + 4, to_delete))
3907 /* Now that the following bytes have been moved into place, see if
3908 we need to replace the jl with a nop. This happens when we had
3909 to use a bl24 insn and the insn following the jl isn't a nop.
3910 Technically, this situation can't happen (since the insn can
3911 never be executed) but to be clean we do this. When the chip
3912 supports parallel 16 bit insns things may change.
3913 We don't need to do this in the case of relaxing to ld24,
3914 and the above code sets nop_p so this isn't done. */
3915 if (! nop_p && to_delete == 4)
3916 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3918 /* That will change things, so we should relax again.
3919 Note that this is not required, and it may be slow. */
3925 /* loop to try the next reloc */
3929 && symtab_hdr->contents != (unsigned char *) isymbuf)
3931 if (! link_info->keep_memory)
3935 /* Cache the symbols for elf_link_input_bfd. */
3936 symtab_hdr->contents = (unsigned char *) isymbuf;
3940 if (contents != NULL
3941 && elf_section_data (sec)->this_hdr.contents != contents)
3943 if (! link_info->keep_memory)
3947 /* Cache the section contents for elf_link_input_bfd. */
3948 elf_section_data (sec)->this_hdr.contents = contents;
3952 if (internal_relocs != NULL
3953 && elf_section_data (sec)->relocs != internal_relocs)
3954 free (internal_relocs);
3960 && symtab_hdr->contents != (unsigned char *) isymbuf)
3962 if (contents != NULL
3963 && elf_section_data (sec)->this_hdr.contents != contents)
3965 if (internal_relocs != NULL
3966 && elf_section_data (sec)->relocs != internal_relocs)
3967 free (internal_relocs);
3972 /* Delete some bytes from a section while relaxing. */
3975 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3981 Elf_Internal_Shdr *symtab_hdr;
3984 Elf_Internal_Rela *irel, *irelend;
3985 Elf_Internal_Rela *irelalign;
3987 Elf_Internal_Sym *isym, *isymend;
3988 struct elf_link_hash_entry **sym_hashes;
3989 struct elf_link_hash_entry **end_hashes;
3990 unsigned int symcount;
3992 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3994 contents = elf_section_data (sec)->this_hdr.contents;
3996 /* The deletion must stop at the next ALIGN reloc for an aligment
3997 power larger than the number of bytes we are deleting. */
4002 irel = elf_section_data (sec)->relocs;
4003 irelend = irel + sec->reloc_count;
4005 /* Actually delete the bytes. */
4006 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
4009 /* Adjust all the relocs. */
4010 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
4012 /* Get the new reloc address. */
4013 if ((irel->r_offset > addr
4014 && irel->r_offset < toaddr))
4015 irel->r_offset -= count;
4018 /* Adjust the local symbols defined in this section. */
4019 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4020 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
4021 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
4023 if (isym->st_shndx == shndx
4024 && isym->st_value > addr
4025 && isym->st_value < toaddr)
4026 isym->st_value -= count;
4029 /* Now adjust the global symbols defined in this section. */
4030 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
4031 - symtab_hdr->sh_info);
4032 sym_hashes = elf_sym_hashes (abfd);
4033 end_hashes = sym_hashes + symcount;
4034 for (; sym_hashes < end_hashes; sym_hashes++)
4036 struct elf_link_hash_entry *sym_hash = *sym_hashes;
4038 if ((sym_hash->root.type == bfd_link_hash_defined
4039 || sym_hash->root.type == bfd_link_hash_defweak)
4040 && sym_hash->root.u.def.section == sec
4041 && sym_hash->root.u.def.value > addr
4042 && sym_hash->root.u.def.value < toaddr)
4044 sym_hash->root.u.def.value -= count;
4051 /* This is a version of bfd_generic_get_relocated_section_contents
4052 which uses m32r_elf_relocate_section. */
4055 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
4056 data, relocatable, symbols)
4058 struct bfd_link_info *link_info;
4059 struct bfd_link_order *link_order;
4061 bfd_boolean relocatable;
4064 Elf_Internal_Shdr *symtab_hdr;
4065 asection *input_section = link_order->u.indirect.section;
4066 bfd *input_bfd = input_section->owner;
4067 asection **sections = NULL;
4068 Elf_Internal_Rela *internal_relocs = NULL;
4069 Elf_Internal_Sym *isymbuf = NULL;
4072 /* We only need to handle the case of relaxing, or of having a
4073 particular set of section contents, specially. */
4075 || elf_section_data (input_section)->this_hdr.contents == NULL)
4076 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4081 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4083 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4084 input_section->size);
4086 if ((input_section->flags & SEC_RELOC) != 0
4087 && input_section->reloc_count > 0)
4089 Elf_Internal_Sym *isymp;
4091 Elf32_External_Sym *esym, *esymend;
4093 internal_relocs = (_bfd_elf_link_read_relocs
4094 (input_bfd, input_section, (PTR) NULL,
4095 (Elf_Internal_Rela *) NULL, FALSE));
4096 if (internal_relocs == NULL)
4099 if (symtab_hdr->sh_info != 0)
4101 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4102 if (isymbuf == NULL)
4103 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4104 symtab_hdr->sh_info, 0,
4106 if (isymbuf == NULL)
4110 amt = symtab_hdr->sh_info;
4111 amt *= sizeof (asection *);
4112 sections = (asection **) bfd_malloc (amt);
4113 if (sections == NULL && symtab_hdr->sh_info > 0)
4116 isymend = isymbuf + symtab_hdr->sh_info;
4117 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4121 if (isym->st_shndx == SHN_UNDEF)
4122 isec = bfd_und_section_ptr;
4123 else if (isym->st_shndx == SHN_ABS)
4124 isec = bfd_abs_section_ptr;
4125 else if (isym->st_shndx == SHN_COMMON)
4126 isec = bfd_com_section_ptr;
4127 else if (isym->st_shndx == SHN_M32R_SCOMMON)
4128 isec = &m32r_elf_scom_section;
4130 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4135 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4136 input_section, data, internal_relocs,
4140 if (sections != NULL)
4143 && symtab_hdr->contents != (unsigned char *) isymbuf)
4145 if (elf_section_data (input_section)->relocs != internal_relocs)
4146 free (internal_relocs);
4152 if (sections != NULL)
4155 && symtab_hdr->contents != (unsigned char *) isymbuf)
4157 if (internal_relocs != NULL
4158 && elf_section_data (input_section)->relocs != internal_relocs)
4159 free (internal_relocs);
4165 /* Set the right machine number. */
4167 m32r_elf_object_p (abfd)
4170 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4173 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
4174 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
4175 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
4180 /* Store the machine number in the flags field. */
4182 m32r_elf_final_write_processing (abfd, linker)
4184 bfd_boolean linker ATTRIBUTE_UNUSED;
4188 switch (bfd_get_mach (abfd))
4191 case bfd_mach_m32r: val = E_M32R_ARCH; break;
4192 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
4193 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
4196 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4197 elf_elfheader (abfd)->e_flags |= val;
4200 /* Function to keep M32R specific file flags. */
4202 m32r_elf_set_private_flags (abfd, flags)
4206 BFD_ASSERT (!elf_flags_init (abfd)
4207 || elf_elfheader (abfd)->e_flags == flags);
4209 elf_elfheader (abfd)->e_flags = flags;
4210 elf_flags_init (abfd) = TRUE;
4214 /* Merge backend specific data from an object file to the output
4215 object file when linking. */
4217 m32r_elf_merge_private_bfd_data (ibfd, obfd)
4224 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4225 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4228 in_flags = elf_elfheader (ibfd)->e_flags;
4229 out_flags = elf_elfheader (obfd)->e_flags;
4231 if (! elf_flags_init (obfd))
4233 /* If the input is the default architecture then do not
4234 bother setting the flags for the output architecture,
4235 instead allow future merges to do this. If no future
4236 merges ever set these flags then they will retain their
4237 unitialised values, which surprise surprise, correspond
4238 to the default values. */
4239 if (bfd_get_arch_info (ibfd)->the_default)
4242 elf_flags_init (obfd) = TRUE;
4243 elf_elfheader (obfd)->e_flags = in_flags;
4245 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4246 && bfd_get_arch_info (obfd)->the_default)
4248 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4254 /* Check flag compatibility. */
4255 if (in_flags == out_flags)
4258 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4260 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4261 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4262 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
4264 (*_bfd_error_handler)
4265 (_("%s: Instruction set mismatch with previous modules"),
4266 bfd_archive_filename (ibfd));
4268 bfd_set_error (bfd_error_bad_value);
4276 /* Display the flags field */
4278 m32r_elf_print_private_bfd_data (abfd, ptr)
4282 FILE * file = (FILE *) ptr;
4284 BFD_ASSERT (abfd != NULL && ptr != NULL)
4286 _bfd_elf_print_private_bfd_data (abfd, ptr);
4288 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
4290 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4293 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
4294 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
4295 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
4304 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4306 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4307 Elf_Internal_Rela *rel;
4308 struct elf_link_hash_entry *h;
4309 Elf_Internal_Sym *sym;
4313 switch (ELF32_R_TYPE (rel->r_info))
4315 case R_M32R_GNU_VTINHERIT:
4316 case R_M32R_GNU_VTENTRY:
4317 case R_M32R_RELA_GNU_VTINHERIT:
4318 case R_M32R_RELA_GNU_VTENTRY:
4322 switch (h->root.type)
4324 case bfd_link_hash_defined:
4325 case bfd_link_hash_defweak:
4326 return h->root.u.def.section;
4328 case bfd_link_hash_common:
4329 return h->root.u.c.p->section;
4337 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4343 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
4344 bfd *abfd ATTRIBUTE_UNUSED;
4345 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4346 asection *sec ATTRIBUTE_UNUSED;
4347 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4349 /* Update the got entry reference counts for the section being removed. */
4350 Elf_Internal_Shdr *symtab_hdr;
4351 struct elf_link_hash_entry **sym_hashes;
4352 bfd_signed_vma *local_got_refcounts;
4353 const Elf_Internal_Rela *rel, *relend;
4354 unsigned long r_symndx;
4355 struct elf_link_hash_entry *h;
4357 elf_section_data (sec)->local_dynrel = NULL;
4359 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4360 sym_hashes = elf_sym_hashes (abfd);
4361 local_got_refcounts = elf_local_got_refcounts (abfd);
4363 relend = relocs + sec->reloc_count;
4364 for (rel = relocs; rel < relend; rel++)
4365 switch (ELF32_R_TYPE (rel->r_info))
4367 case R_M32R_GOT16_HI_ULO:
4368 case R_M32R_GOT16_HI_SLO:
4369 case R_M32R_GOT16_LO:
4371 case R_M32R_GOTOFF_HI_ULO:
4372 case R_M32R_GOTOFF_HI_SLO:
4373 case R_M32R_GOTOFF_LO:
4375 case R_M32R_GOTPC_HI_ULO:
4376 case R_M32R_GOTPC_HI_SLO:
4377 case R_M32R_GOTPC_LO:
4378 case R_M32R_GOTPC24:
4379 r_symndx = ELF32_R_SYM (rel->r_info);
4380 if (r_symndx >= symtab_hdr->sh_info)
4382 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4383 if (h->got.refcount > 0)
4388 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4389 local_got_refcounts[r_symndx]--;
4393 case R_M32R_16_RELA:
4394 case R_M32R_24_RELA:
4395 case R_M32R_32_RELA:
4396 case R_M32R_HI16_ULO_RELA:
4397 case R_M32R_HI16_SLO_RELA:
4398 case R_M32R_LO16_RELA:
4399 case R_M32R_SDA16_RELA:
4400 case R_M32R_18_PCREL_RELA:
4401 case R_M32R_26_PCREL_RELA:
4402 r_symndx = ELF32_R_SYM (rel->r_info);
4403 if (r_symndx >= symtab_hdr->sh_info)
4405 struct elf_m32r_link_hash_entry *eh;
4406 struct elf_m32r_dyn_relocs **pp;
4407 struct elf_m32r_dyn_relocs *p;
4409 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4411 if (!info->shared && h->plt.refcount > 0)
4412 h->plt.refcount -= 1;
4414 eh = (struct elf_m32r_link_hash_entry *) h;
4416 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4419 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4420 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4430 case R_M32R_26_PLTREL:
4431 r_symndx = ELF32_R_SYM (rel->r_info);
4432 if (r_symndx >= symtab_hdr->sh_info)
4434 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4435 if (h->plt.refcount > 0)
4447 /* Look through the relocs for a section during the first phase.
4448 Since we don't do .gots or .plts, we just need to consider the
4449 virtual table relocs for gc. */
4452 m32r_elf_check_relocs (abfd, info, sec, relocs)
4454 struct bfd_link_info *info;
4456 const Elf_Internal_Rela *relocs;
4458 Elf_Internal_Shdr *symtab_hdr;
4459 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4460 const Elf_Internal_Rela *rel;
4461 const Elf_Internal_Rela *rel_end;
4462 struct elf_m32r_link_hash_table *htab;
4464 bfd_vma *local_got_offsets;
4465 asection *sgot, *srelgot, *sreloc;
4467 if (info->relocatable)
4470 sgot = srelgot = sreloc = NULL;
4472 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4473 sym_hashes = elf_sym_hashes (abfd);
4474 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
4475 if (!elf_bad_symtab (abfd))
4476 sym_hashes_end -= symtab_hdr->sh_info;
4478 htab = m32r_elf_hash_table (info);
4479 dynobj = htab->root.dynobj;
4480 local_got_offsets = elf_local_got_offsets (abfd);
4482 rel_end = relocs + sec->reloc_count;
4483 for (rel = relocs; rel < rel_end; rel++)
4486 struct elf_link_hash_entry *h;
4487 unsigned long r_symndx;
4489 r_symndx = ELF32_R_SYM (rel->r_info);
4490 r_type = ELF32_R_TYPE (rel->r_info);
4491 if (r_symndx < symtab_hdr->sh_info)
4494 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4496 /* Some relocs require a global offset table. */
4497 if (htab->sgot == NULL)
4501 case R_M32R_GOT16_HI_ULO:
4502 case R_M32R_GOT16_HI_SLO:
4504 case R_M32R_GOTOFF_HI_ULO:
4505 case R_M32R_GOTOFF_HI_SLO:
4506 case R_M32R_GOTOFF_LO:
4507 case R_M32R_GOT16_LO:
4508 case R_M32R_GOTPC24:
4509 case R_M32R_GOTPC_HI_ULO:
4510 case R_M32R_GOTPC_HI_SLO:
4511 case R_M32R_GOTPC_LO:
4514 htab->root.dynobj = dynobj = abfd;
4515 if (! create_got_section (dynobj, info))
4526 case R_M32R_GOT16_HI_ULO:
4527 case R_M32R_GOT16_HI_SLO:
4528 case R_M32R_GOT16_LO:
4532 h->got.refcount += 1;
4535 bfd_signed_vma *local_got_refcounts;
4537 /* This is a global offset table entry for a local
4539 local_got_refcounts = elf_local_got_refcounts (abfd);
4540 if (local_got_refcounts == NULL)
4544 size = symtab_hdr->sh_info;
4545 size *= sizeof (bfd_signed_vma);
4546 local_got_refcounts = ((bfd_signed_vma *)
4547 bfd_zalloc (abfd, size));
4548 if (local_got_refcounts == NULL)
4550 elf_local_got_refcounts (abfd) = local_got_refcounts;
4552 local_got_refcounts[r_symndx] += 1;
4556 case R_M32R_26_PLTREL:
4557 /* This symbol requires a procedure linkage table entry. We
4558 actually build the entry in adjust_dynamic_symbol,
4559 because this might be a case of linking PIC code without
4560 linking in any dynamic objects, in which case we don't
4561 need to generate a procedure linkage table after all. */
4563 /* If this is a local symbol, we resolve it directly without
4564 creating a procedure linkage table entry. */
4568 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4571 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4572 h->plt.refcount += 1;
4575 case R_M32R_16_RELA:
4576 case R_M32R_24_RELA:
4577 case R_M32R_32_RELA:
4578 case R_M32R_HI16_ULO_RELA:
4579 case R_M32R_HI16_SLO_RELA:
4580 case R_M32R_LO16_RELA:
4581 case R_M32R_SDA16_RELA:
4582 case R_M32R_18_PCREL_RELA:
4583 case R_M32R_26_PCREL_RELA:
4585 if (h != NULL && !info->shared)
4587 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
4588 h->plt.refcount += 1;
4591 /* If we are creating a shared library, and this is a reloc
4592 against a global symbol, or a non PC relative reloc
4593 against a local symbol, then we need to copy the reloc
4594 into the shared library. However, if we are linking with
4595 -Bsymbolic, we do not need to copy a reloc against a
4596 global symbol which is defined in an object we are
4597 including in the link (i.e., DEF_REGULAR is set). At
4598 this point we have not seen all the input files, so it is
4599 possible that DEF_REGULAR is not set now but will be set
4600 later (it is never cleared). We account for that
4601 possibility below by storing information in the
4602 dyn_relocs field of the hash table entry. A similar
4603 situation occurs when creating shared libraries and symbol
4604 visibility changes render the symbol local.
4606 If on the other hand, we are creating an executable, we
4607 may need to keep relocations for symbols satisfied by a
4608 dynamic library if we manage to avoid copy relocs for the
4611 && (sec->flags & SEC_ALLOC) != 0
4612 && ((r_type != R_M32R_26_PCREL_RELA
4613 && r_type != R_M32R_18_PCREL_RELA)
4615 && (! info->symbolic
4616 || h->root.type == bfd_link_hash_defweak
4617 || (h->elf_link_hash_flags
4618 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4620 && (sec->flags & SEC_ALLOC) != 0
4622 && (h->root.type == bfd_link_hash_defweak
4623 || (h->elf_link_hash_flags
4624 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4626 struct elf_m32r_dyn_relocs *p;
4627 struct elf_m32r_dyn_relocs **head;
4630 htab->root.dynobj = dynobj = abfd;
4632 /* When creating a shared object, we must copy these
4633 relocs into the output file. We create a reloc
4634 section in dynobj and make room for the reloc. */
4639 name = (bfd_elf_string_from_elf_section
4641 elf_elfheader (abfd)->e_shstrndx,
4642 elf_section_data (sec)->rel_hdr.sh_name));
4646 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4647 && strcmp (bfd_get_section_name (abfd, sec),
4650 sreloc = bfd_get_section_by_name (dynobj, name);
4655 sreloc = bfd_make_section (dynobj, name);
4656 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4657 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4658 if ((sec->flags & SEC_ALLOC) != 0)
4659 flags |= SEC_ALLOC | SEC_LOAD;
4661 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4662 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4665 elf_section_data (sec)->sreloc = sreloc;
4668 /* If this is a global symbol, we count the number of
4669 relocations we need for this symbol. */
4671 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4676 /* Track dynamic relocs needed for local syms too. */
4677 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4682 head = ((struct elf_m32r_dyn_relocs **)
4683 &elf_section_data (s)->local_dynrel);
4687 if (p == NULL || p->sec != sec)
4689 bfd_size_type amt = sizeof (*p);
4690 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4701 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4702 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4707 /* This relocation describes the C++ object vtable hierarchy.
4708 Reconstruct it for later use during GC. */
4709 case R_M32R_RELA_GNU_VTINHERIT:
4710 case R_M32R_GNU_VTINHERIT:
4711 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4715 /* This relocation describes which C++ vtable entries are actually
4716 used. Record for later use during GC. */
4717 case R_M32R_GNU_VTENTRY:
4718 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4721 case R_M32R_RELA_GNU_VTENTRY:
4722 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4731 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4733 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4734 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4735 { NULL, 0, 0, 0, 0 }
4739 m32r_elf_fake_sections (abfd, hdr, sec)
4741 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4744 register const char *name;
4746 name = bfd_get_section_name (abfd, sec);
4748 /* The generic elf_fake_sections will set up REL_HDR using the
4749 default kind of relocations. But, we may actually need both
4750 kinds of relocations, so we set up the second header here.
4752 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4753 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4754 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4755 of the resulting empty .rela.<section> sections starts with
4756 sh_offset == object size, and ld doesn't allow that. While the check
4757 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4758 avoided by not emitting those useless sections in the first place. */
4759 if ((sec->flags & SEC_RELOC) != 0)
4761 struct bfd_elf_section_data *esd;
4762 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4764 esd = elf_section_data (sec);
4765 BFD_ASSERT (esd->rel_hdr2 == NULL);
4766 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4769 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4776 static enum elf_reloc_type_class
4777 m32r_elf_reloc_type_class (rela)
4778 const Elf_Internal_Rela *rela;
4780 switch ((int) ELF32_R_TYPE (rela->r_info))
4782 case R_M32R_RELATIVE:
4783 return reloc_class_relative;
4784 case R_M32R_JMP_SLOT:
4785 return reloc_class_plt;
4787 return reloc_class_copy;
4789 return reloc_class_normal;
4793 #define ELF_ARCH bfd_arch_m32r
4794 #define ELF_MACHINE_CODE EM_M32R
4795 #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
4796 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
4798 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
4799 #define TARGET_BIG_NAME "elf32-m32r"
4800 #define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4801 #define TARGET_LITTLE_NAME "elf32-m32rle"
4803 #define elf_info_to_howto m32r_info_to_howto
4804 #define elf_info_to_howto_rel m32r_info_to_howto_rel
4805 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4806 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4807 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4808 #define elf_backend_relocate_section m32r_elf_relocate_section
4809 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4810 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4811 #define elf_backend_check_relocs m32r_elf_check_relocs
4813 #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4814 #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4815 #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4816 #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4817 #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4818 #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4819 #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4820 #define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4822 #define elf_backend_can_gc_sections 1
4824 #define elf_backend_rela_normal 1
4826 #define elf_backend_can_refcount 1
4827 #define elf_backend_want_got_plt 1
4828 #define elf_backend_plt_readonly 1
4829 #define elf_backend_want_plt_sym 0
4830 #define elf_backend_got_header_size 12
4832 #define elf_backend_may_use_rel_p 1
4833 #ifdef USE_M32R_OLD_RELOC
4834 #define elf_backend_default_use_rela_p 0
4835 #define elf_backend_may_use_rela_p 0
4837 #define elf_backend_default_use_rela_p 1
4838 #define elf_backend_may_use_rela_p 1
4839 #define elf_backend_fake_sections m32r_elf_fake_sections
4844 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4845 #define bfd_elf32_bfd_get_relocated_section_contents \
4846 m32r_elf_get_relocated_section_contents
4849 #define elf_backend_object_p m32r_elf_object_p
4850 #define elf_backend_final_write_processing m32r_elf_final_write_processing
4851 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4852 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4853 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
4854 #define elf_backend_special_sections m32r_elf_special_sections
4856 #include "elf32-target.h"
4858 #undef ELF_MAXPAGESIZE
4859 #define ELF_MAXPAGESIZE 0x1000
4861 #undef TARGET_BIG_SYM
4862 #define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4863 #undef TARGET_BIG_NAME
4864 #define TARGET_BIG_NAME "elf32-m32r-linux"
4865 #undef TARGET_LITTLE_SYM
4866 #define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4867 #undef TARGET_LITTLE_NAME
4868 #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4870 #define elf32_bed elf32_m32r_lin_bed
4872 #include "elf32-target.h"