1 /* M32R-specific support for 32-bit ELF.
2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 static bfd_reloc_status_type m32r_elf_10_pcrel_reloc
27 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
28 static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc
29 PARAMS ((bfd
*, reloc_howto_type
*, asection
*,
30 bfd_byte
*, bfd_vma
, asection
*, bfd_vma
, bfd_vma
));
31 static bfd_reloc_status_type m32r_elf_hi16_reloc
32 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
33 static void m32r_elf_relocate_hi16
34 PARAMS ((bfd
*, int, Elf_Internal_Rela
*, Elf_Internal_Rela
*,
35 bfd_byte
*, bfd_vma
));
36 bfd_reloc_status_type m32r_elf_lo16_reloc
37 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
38 static bfd_reloc_status_type m32r_elf_sda16_reloc
39 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
40 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
41 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
42 static void m32r_info_to_howto_rel
43 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
44 boolean _bfd_m32r_elf_section_from_bfd_section
45 PARAMS ((bfd
*, Elf32_Internal_Shdr
*, asection
*, int *));
46 void _bfd_m32r_elf_symbol_processing
47 PARAMS ((bfd
*, asymbol
*));
48 static boolean m32r_elf_add_symbol_hook
49 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
50 const char **, flagword
*, asection
**, bfd_vma
*));
51 static boolean m32r_elf_relocate_section
52 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
53 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
55 static boolean m32r_elf_relax_delete_bytes
56 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
59 static bfd_reloc_status_type m32r_elf_final_sda_base
60 PARAMS ((bfd
*, struct bfd_link_info
*, const char **, bfd_vma
*));
61 static boolean m32r_elf_object_p
63 static void m32r_elf_final_write_processing
64 PARAMS ((bfd
*, boolean
));
65 static boolean m32r_elf_set_private_flags
66 PARAMS ((bfd
*, flagword
));
67 static boolean m32r_elf_copy_private_bfd_data
68 PARAMS ((bfd
*, bfd
*));
69 static boolean m32r_elf_merge_private_bfd_data
70 PARAMS ((bfd
*, bfd
*));
71 static boolean m32r_elf_print_private_bfd_data
72 PARAMS ((bfd
*, PTR
));
74 #define NOP_INSN 0x7000
75 #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
77 /* Use REL instead of RELA to save space.
78 This only saves space in libraries and object files, but perhaps
79 relocs will be put in ROM? All in all though, REL relocs are a pain
83 static reloc_howto_type m32r_elf_howto_table
[] =
85 /* This reloc does nothing. */
86 HOWTO (R_M32R_NONE
, /* type */
88 2, /* size (0 = byte, 1 = short, 2 = long) */
90 false, /* pc_relative */
92 complain_overflow_bitfield
, /* complain_on_overflow */
93 bfd_elf_generic_reloc
, /* special_function */
94 "R_M32R_NONE", /* name */
95 false, /* partial_inplace */
98 false), /* pcrel_offset */
100 /* A 16 bit absolute relocation. */
101 HOWTO (R_M32R_16
, /* type */
103 1, /* size (0 = byte, 1 = short, 2 = long) */
105 false, /* pc_relative */
107 complain_overflow_bitfield
, /* complain_on_overflow */
108 bfd_elf_generic_reloc
, /* special_function */
109 "R_M32R_16", /* name */
110 true, /* partial_inplace */
111 0xffff, /* src_mask */
112 0xffff, /* dst_mask */
113 false), /* pcrel_offset */
115 /* A 32 bit absolute relocation. */
116 HOWTO (R_M32R_32
, /* type */
118 2, /* size (0 = byte, 1 = short, 2 = long) */
120 false, /* pc_relative */
122 complain_overflow_bitfield
, /* complain_on_overflow */
123 bfd_elf_generic_reloc
, /* special_function */
124 "R_M32R_32", /* name */
125 true, /* partial_inplace */
126 0xffffffff, /* src_mask */
127 0xffffffff, /* dst_mask */
128 false), /* pcrel_offset */
130 /* A 24 bit address. */
131 HOWTO (R_M32R_24
, /* type */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
135 false, /* pc_relative */
137 complain_overflow_unsigned
, /* complain_on_overflow */
138 bfd_elf_generic_reloc
, /* special_function */
139 "R_M32R_24", /* name */
140 true, /* partial_inplace */
141 0xffffff, /* src_mask */
142 0xffffff, /* dst_mask */
143 false), /* pcrel_offset */
145 /* An PC Relative 10-bit relocation, shifted by 2.
146 This reloc is complicated because relocations are relative to pc & -4.
147 i.e. branches in the right insn slot use the address of the left insn
149 /* ??? It's not clear whether this should have partial_inplace set or not.
150 Branch relaxing in the assembler can store the addend in the insn,
151 and if bfd_install_relocation gets called the addend may get added
153 HOWTO (R_M32R_10_PCREL
, /* type */
155 1, /* size (0 = byte, 1 = short, 2 = long) */
157 true, /* pc_relative */
159 complain_overflow_signed
, /* complain_on_overflow */
160 m32r_elf_10_pcrel_reloc
, /* special_function */
161 "R_M32R_10_PCREL", /* name */
162 false, /* partial_inplace */
165 true), /* pcrel_offset */
167 /* A relative 18 bit relocation, right shifted by 2. */
168 HOWTO (R_M32R_18_PCREL
, /* type */
170 2, /* size (0 = byte, 1 = short, 2 = long) */
172 true, /* pc_relative */
174 complain_overflow_signed
, /* complain_on_overflow */
175 bfd_elf_generic_reloc
, /* special_function */
176 "R_M32R_18_PCREL", /* name */
177 false, /* partial_inplace */
178 0xffff, /* src_mask */
179 0xffff, /* dst_mask */
180 true), /* pcrel_offset */
182 /* A relative 26 bit relocation, right shifted by 2. */
183 /* ??? It's not clear whether this should have partial_inplace set or not.
184 Branch relaxing in the assembler can store the addend in the insn,
185 and if bfd_install_relocation gets called the addend may get added
187 HOWTO (R_M32R_26_PCREL
, /* type */
189 2, /* size (0 = byte, 1 = short, 2 = long) */
191 true, /* pc_relative */
193 complain_overflow_signed
, /* complain_on_overflow */
194 bfd_elf_generic_reloc
, /* special_function */
195 "R_M32R_26_PCREL", /* name */
196 false, /* partial_inplace */
197 0xffffff, /* src_mask */
198 0xffffff, /* dst_mask */
199 true), /* pcrel_offset */
201 /* High 16 bits of address when lower 16 is or'd in. */
202 HOWTO (R_M32R_HI16_ULO
, /* type */
204 2, /* size (0 = byte, 1 = short, 2 = long) */
206 false, /* pc_relative */
208 complain_overflow_dont
, /* complain_on_overflow */
209 m32r_elf_hi16_reloc
, /* special_function */
210 "R_M32R_HI16_ULO", /* name */
211 true, /* partial_inplace */
212 0x0000ffff, /* src_mask */
213 0x0000ffff, /* dst_mask */
214 false), /* pcrel_offset */
216 /* High 16 bits of address when lower 16 is added in. */
217 HOWTO (R_M32R_HI16_SLO
, /* type */
219 2, /* size (0 = byte, 1 = short, 2 = long) */
221 false, /* pc_relative */
223 complain_overflow_dont
, /* complain_on_overflow */
224 m32r_elf_hi16_reloc
, /* special_function */
225 "R_M32R_HI16_SLO", /* name */
226 true, /* partial_inplace */
227 0x0000ffff, /* src_mask */
228 0x0000ffff, /* dst_mask */
229 false), /* pcrel_offset */
231 /* Lower 16 bits of address. */
232 HOWTO (R_M32R_LO16
, /* type */
234 2, /* size (0 = byte, 1 = short, 2 = long) */
236 false, /* pc_relative */
238 complain_overflow_dont
, /* complain_on_overflow */
239 m32r_elf_lo16_reloc
, /* special_function */
240 "R_M32R_LO16", /* name */
241 true, /* partial_inplace */
242 0x0000ffff, /* src_mask */
243 0x0000ffff, /* dst_mask */
244 false), /* pcrel_offset */
246 /* Small data area 16 bits offset. */
247 HOWTO (R_M32R_SDA16
, /* type */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
251 false, /* pc_relative */
253 complain_overflow_signed
, /* complain_on_overflow */
254 m32r_elf_sda16_reloc
, /* special_function */
255 "R_M32R_SDA16", /* name */
256 true, /* partial_inplace */ /* FIXME: correct? */
257 0x0000ffff, /* src_mask */
258 0x0000ffff, /* dst_mask */
259 false), /* pcrel_offset */
261 /* GNU extension to record C++ vtable hierarchy */
262 HOWTO (R_M32R_GNU_VTINHERIT
, /* type */
264 2, /* size (0 = byte, 1 = short, 2 = long) */
266 false, /* pc_relative */
268 complain_overflow_dont
, /* complain_on_overflow */
269 NULL
, /* special_function */
270 "R_M32R_GNU_VTINHERIT", /* name */
271 false, /* partial_inplace */
274 false), /* pcrel_offset */
276 /* GNU extension to record C++ vtable member usage */
277 HOWTO (R_M32R_GNU_VTENTRY
, /* type */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
281 false, /* pc_relative */
283 complain_overflow_dont
, /* complain_on_overflow */
284 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
285 "R_M32R_GNU_VTENTRY", /* name */
286 false, /* partial_inplace */
289 false), /* pcrel_offset */
293 /* Handle the R_M32R_10_PCREL reloc. */
295 static bfd_reloc_status_type
296 m32r_elf_10_pcrel_reloc (abfd
, reloc_entry
, symbol
, data
,
297 input_section
, output_bfd
, error_message
)
299 arelent
* reloc_entry
;
302 asection
* input_section
;
304 char ** error_message ATTRIBUTE_UNUSED
;
306 /* This part is from bfd_elf_generic_reloc. */
307 if (output_bfd
!= (bfd
*) NULL
308 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
309 && (! reloc_entry
->howto
->partial_inplace
310 || reloc_entry
->addend
== 0))
312 reloc_entry
->address
+= input_section
->output_offset
;
316 if (output_bfd
!= NULL
)
318 /* FIXME: See bfd_perform_relocation. Is this right? */
319 return bfd_reloc_continue
;
322 return m32r_elf_do_10_pcrel_reloc (abfd
, reloc_entry
->howto
,
324 data
, reloc_entry
->address
,
327 + symbol
->section
->output_section
->vma
328 + symbol
->section
->output_offset
),
329 reloc_entry
->addend
);
332 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
334 static bfd_reloc_status_type
335 m32r_elf_do_10_pcrel_reloc (abfd
, howto
, input_section
, data
, offset
,
336 symbol_section
, symbol_value
, addend
)
338 reloc_howto_type
*howto
;
339 asection
*input_section
;
342 asection
*symbol_section ATTRIBUTE_UNUSED
;
343 bfd_vma symbol_value
;
346 bfd_signed_vma relocation
;
348 bfd_reloc_status_type status
;
350 /* Sanity check the address (offset in section). */
351 if (offset
> input_section
->_cooked_size
)
352 return bfd_reloc_outofrange
;
354 relocation
= symbol_value
+ addend
;
355 /* Make it pc relative. */
356 relocation
-= (input_section
->output_section
->vma
357 + input_section
->output_offset
);
358 /* These jumps mask off the lower two bits of the current address
359 before doing pcrel calculations. */
360 relocation
-= (offset
& -4L);
362 if (relocation
< -0x200 || relocation
> 0x1ff)
363 status
= bfd_reloc_overflow
;
365 status
= bfd_reloc_ok
;
367 x
= bfd_get_16 (abfd
, data
+ offset
);
368 relocation
>>= howto
->rightshift
;
369 relocation
<<= howto
->bitpos
;
370 x
= (x
& ~howto
->dst_mask
) | (((x
& howto
->src_mask
) + relocation
) & howto
->dst_mask
);
371 bfd_put_16 (abfd
, x
, data
+ offset
);
376 /* Handle the R_M32R_HI16_[SU]LO relocs.
377 HI16_SLO is for the add3 and load/store with displacement instructions.
378 HI16_ULO is for the or3 instruction.
379 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
380 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
381 we must add one to the high 16 bytes (which will get subtracted off when
382 the low 16 bits are added).
383 These relocs have to be done in combination with an R_M32R_LO16 reloc
384 because there is a carry from the LO16 to the HI16. Here we just save
385 the information we need; we do the actual relocation when we see the LO16.
386 This code is copied from the elf32-mips.c. We also support an arbitrary
387 number of HI16 relocs to be associated with a single LO16 reloc. The
388 assembler sorts the relocs to ensure each HI16 immediately precedes its
389 LO16. However if there are multiple copies, the assembler may not find
390 the real LO16 so it picks the first one it finds. */
394 struct m32r_hi16
*next
;
399 /* FIXME: This should not be a static variable. */
401 static struct m32r_hi16
*m32r_hi16_list
;
403 static bfd_reloc_status_type
404 m32r_elf_hi16_reloc (abfd
, reloc_entry
, symbol
, data
,
405 input_section
, output_bfd
, error_message
)
406 bfd
*abfd ATTRIBUTE_UNUSED
;
407 arelent
*reloc_entry
;
410 asection
*input_section
;
412 char **error_message ATTRIBUTE_UNUSED
;
414 bfd_reloc_status_type ret
;
418 /* This part is from bfd_elf_generic_reloc.
419 If we're relocating, and this an external symbol, we don't want
420 to change anything. */
421 if (output_bfd
!= (bfd
*) NULL
422 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
423 && reloc_entry
->addend
== 0)
425 reloc_entry
->address
+= input_section
->output_offset
;
429 /* Sanity check the address (offset in section). */
430 if (reloc_entry
->address
> input_section
->_cooked_size
)
431 return bfd_reloc_outofrange
;
434 if (bfd_is_und_section (symbol
->section
)
435 && output_bfd
== (bfd
*) NULL
)
436 ret
= bfd_reloc_undefined
;
438 if (bfd_is_com_section (symbol
->section
))
441 relocation
= symbol
->value
;
443 relocation
+= symbol
->section
->output_section
->vma
;
444 relocation
+= symbol
->section
->output_offset
;
445 relocation
+= reloc_entry
->addend
;
447 /* Save the information, and let LO16 do the actual relocation. */
448 n
= (struct m32r_hi16
*) bfd_malloc (sizeof *n
);
450 return bfd_reloc_outofrange
;
451 n
->addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
452 n
->addend
= relocation
;
453 n
->next
= m32r_hi16_list
;
456 if (output_bfd
!= (bfd
*) NULL
)
457 reloc_entry
->address
+= input_section
->output_offset
;
462 /* Handle an M32R ELF HI16 reloc. */
465 m32r_elf_relocate_hi16 (input_bfd
, type
, relhi
, rello
, contents
, addend
)
468 Elf_Internal_Rela
*relhi
;
469 Elf_Internal_Rela
*rello
;
476 insn
= bfd_get_32 (input_bfd
, contents
+ relhi
->r_offset
);
478 addlo
= bfd_get_32 (input_bfd
, contents
+ rello
->r_offset
);
479 if (type
== R_M32R_HI16_SLO
)
480 addlo
= ((addlo
& 0xffff) ^ 0x8000) - 0x8000;
484 addend
+= ((insn
& 0xffff) << 16) + addlo
;
486 /* Reaccount for sign extension of low part. */
487 if (type
== R_M32R_HI16_SLO
488 && (addend
& 0x8000) != 0)
491 bfd_put_32 (input_bfd
,
492 (insn
& 0xffff0000) | ((addend
>> 16) & 0xffff),
493 contents
+ relhi
->r_offset
);
496 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
497 inplace relocation; this function exists in order to do the
498 R_M32R_HI16_[SU]LO relocation described above. */
500 bfd_reloc_status_type
501 m32r_elf_lo16_reloc (input_bfd
, reloc_entry
, symbol
, data
,
502 input_section
, output_bfd
, error_message
)
504 arelent
*reloc_entry
;
507 asection
*input_section
;
509 char **error_message
;
511 bfd_reloc_status_type ret
;
515 /* This part is from bfd_elf_generic_reloc.
516 If we're relocating, and this an external symbol, we don't want
517 to change anything. */
518 if (output_bfd
!= (bfd
*) NULL
519 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
520 && reloc_entry
->addend
== 0)
522 reloc_entry
->address
+= input_section
->output_offset
;
526 if (m32r_hi16_list
!= NULL
)
536 struct m32r_hi16
*next
;
538 /* Do the HI16 relocation. Note that we actually don't need
539 to know anything about the LO16 itself, except where to
540 find the low 16 bits of the addend needed by the LO16. */
541 insn
= bfd_get_32 (input_bfd
, l
->addr
);
542 vallo
= ((bfd_get_32 (input_bfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
543 & 0xffff) ^ 0x8000) - 0x8000;
544 val
= ((insn
& 0xffff) << 16) + vallo
;
547 /* Reaccount for sign extension of low part. */
548 if ((val
& 0x8000) != 0)
551 insn
= (insn
&~ 0xffff) | ((val
>> 16) & 0xffff);
552 bfd_put_32 (input_bfd
, insn
, l
->addr
);
559 m32r_hi16_list
= NULL
;
562 /* Now do the LO16 reloc in the usual way.
563 ??? It would be nice to call bfd_elf_generic_reloc here,
564 but we have partial_inplace == TRUE. bfd_elf_generic_reloc will
565 pass the handling back to bfd_install_relocation which will install
566 a section relative addend which is wrong. */
568 /* Sanity check the address (offset in section). */
569 if (reloc_entry
->address
> input_section
->_cooked_size
)
570 return bfd_reloc_outofrange
;
573 if (bfd_is_und_section (symbol
->section
)
574 && output_bfd
== (bfd
*) NULL
)
575 ret
= bfd_reloc_undefined
;
577 if (bfd_is_com_section (symbol
->section
)
578 || output_bfd
!= (bfd
*) NULL
)
581 relocation
= symbol
->value
;
583 /* Only do this for a final link. */
584 if (output_bfd
== (bfd
*) NULL
)
586 relocation
+= symbol
->section
->output_section
->vma
;
587 relocation
+= symbol
->section
->output_offset
;
590 relocation
+= reloc_entry
->addend
;
592 insn
= bfd_get_32 (input_bfd
, data
+ reloc_entry
->address
);
593 insn
= (insn
& 0xffff0000) | (relocation
& 0xffff);
594 bfd_put_32 (input_bfd
, insn
, data
+ reloc_entry
->address
);
596 if (output_bfd
!= (bfd
*) NULL
)
597 reloc_entry
->address
+= input_section
->output_offset
;
602 /* Handle the R_M32R_SDA16 reloc.
603 This reloc is used to compute the address of objects in the small data area
604 and to perform loads and stores from that area.
605 The lower 16 bits are sign extended and added to the register specified
606 in the instruction, which is assumed to point to _SDA_BASE_. */
608 static bfd_reloc_status_type
609 m32r_elf_sda16_reloc (abfd
, reloc_entry
, symbol
, data
,
610 input_section
, output_bfd
, error_message
)
611 bfd
*abfd ATTRIBUTE_UNUSED
;
612 arelent
*reloc_entry
;
614 PTR data ATTRIBUTE_UNUSED
;
615 asection
*input_section
;
617 char **error_message ATTRIBUTE_UNUSED
;
619 /* This part is from bfd_elf_generic_reloc. */
620 if (output_bfd
!= (bfd
*) NULL
621 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
622 && (! reloc_entry
->howto
->partial_inplace
623 || reloc_entry
->addend
== 0))
625 reloc_entry
->address
+= input_section
->output_offset
;
629 if (output_bfd
!= NULL
)
631 /* FIXME: See bfd_perform_relocation. Is this right? */
632 return bfd_reloc_continue
;
635 /* FIXME: not sure what to do here yet. But then again, the linker
636 may never call us. */
640 /* Map BFD reloc types to M32R ELF reloc types. */
642 struct m32r_reloc_map
644 bfd_reloc_code_real_type bfd_reloc_val
;
645 unsigned char elf_reloc_val
;
648 static const struct m32r_reloc_map m32r_reloc_map
[] =
650 { BFD_RELOC_NONE
, R_M32R_NONE
},
651 { BFD_RELOC_16
, R_M32R_16
},
652 { BFD_RELOC_32
, R_M32R_32
},
653 { BFD_RELOC_M32R_24
, R_M32R_24
},
654 { BFD_RELOC_M32R_10_PCREL
, R_M32R_10_PCREL
},
655 { BFD_RELOC_M32R_18_PCREL
, R_M32R_18_PCREL
},
656 { BFD_RELOC_M32R_26_PCREL
, R_M32R_26_PCREL
},
657 { BFD_RELOC_M32R_HI16_ULO
, R_M32R_HI16_ULO
},
658 { BFD_RELOC_M32R_HI16_SLO
, R_M32R_HI16_SLO
},
659 { BFD_RELOC_M32R_LO16
, R_M32R_LO16
},
660 { BFD_RELOC_M32R_SDA16
, R_M32R_SDA16
},
661 { BFD_RELOC_VTABLE_INHERIT
, R_M32R_GNU_VTINHERIT
},
662 { BFD_RELOC_VTABLE_ENTRY
, R_M32R_GNU_VTENTRY
},
665 static reloc_howto_type
*
666 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
667 bfd
*abfd ATTRIBUTE_UNUSED
;
668 bfd_reloc_code_real_type code
;
673 i
< sizeof (m32r_reloc_map
) / sizeof (struct m32r_reloc_map
);
676 if (m32r_reloc_map
[i
].bfd_reloc_val
== code
)
677 return &m32r_elf_howto_table
[m32r_reloc_map
[i
].elf_reloc_val
];
683 /* Set the howto pointer for an M32R ELF reloc. */
686 m32r_info_to_howto_rel (abfd
, cache_ptr
, dst
)
687 bfd
*abfd ATTRIBUTE_UNUSED
;
689 Elf32_Internal_Rel
*dst
;
693 r_type
= ELF32_R_TYPE (dst
->r_info
);
694 BFD_ASSERT (r_type
< (unsigned int) R_M32R_max
);
695 cache_ptr
->howto
= &m32r_elf_howto_table
[r_type
];
698 /* Given a BFD section, try to locate the corresponding ELF section
702 _bfd_m32r_elf_section_from_bfd_section (abfd
, hdr
, sec
, retval
)
703 bfd
*abfd ATTRIBUTE_UNUSED
;
704 Elf32_Internal_Shdr
*hdr ATTRIBUTE_UNUSED
;
708 if (strcmp (bfd_get_section_name (abfd
, sec
), ".scommon") == 0)
710 *retval
= SHN_M32R_SCOMMON
;
716 /* M32R ELF uses two common sections. One is the usual one, and the other
717 is for small objects. All the small objects are kept together, and then
718 referenced via one register, which yields faster assembler code. It is
719 up to the compiler to emit an instruction to load the register with
720 _SDA_BASE. This is what we use for the small common section. This
721 approach is copied from elf32-mips.c. */
722 static asection m32r_elf_scom_section
;
723 static asymbol m32r_elf_scom_symbol
;
724 static asymbol
*m32r_elf_scom_symbol_ptr
;
726 /* Handle the special M32R section numbers that a symbol may use. */
729 _bfd_m32r_elf_symbol_processing (abfd
, asym
)
730 bfd
*abfd ATTRIBUTE_UNUSED
;
733 elf_symbol_type
*elfsym
;
735 elfsym
= (elf_symbol_type
*) asym
;
737 switch (elfsym
->internal_elf_sym
.st_shndx
)
739 case SHN_M32R_SCOMMON
:
740 if (m32r_elf_scom_section
.name
== NULL
)
742 /* Initialize the small common section. */
743 m32r_elf_scom_section
.name
= ".scommon";
744 m32r_elf_scom_section
.flags
= SEC_IS_COMMON
;
745 m32r_elf_scom_section
.output_section
= &m32r_elf_scom_section
;
746 m32r_elf_scom_section
.symbol
= &m32r_elf_scom_symbol
;
747 m32r_elf_scom_section
.symbol_ptr_ptr
= &m32r_elf_scom_symbol_ptr
;
748 m32r_elf_scom_symbol
.name
= ".scommon";
749 m32r_elf_scom_symbol
.flags
= BSF_SECTION_SYM
;
750 m32r_elf_scom_symbol
.section
= &m32r_elf_scom_section
;
751 m32r_elf_scom_symbol_ptr
= &m32r_elf_scom_symbol
;
753 asym
->section
= &m32r_elf_scom_section
;
754 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
759 /* Hook called by the linker routine which adds symbols from an object
760 file. We must handle the special M32R section numbers here.
761 We also keep watching for whether we need to create the sdata special
765 m32r_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
767 struct bfd_link_info
*info
;
768 const Elf_Internal_Sym
*sym
;
770 flagword
*flagsp ATTRIBUTE_UNUSED
;
774 if (! info
->relocateable
775 && (*namep
)[0] == '_' && (*namep
)[1] == 'S'
776 && strcmp (*namep
, "_SDA_BASE_") == 0)
778 /* This is simpler than using _bfd_elf_create_linker_section
779 (our needs are simpler than ppc's needs). Also
780 _bfd_elf_create_linker_section currently has a bug where if a .sdata
781 section already exists a new one is created that follows it which
782 screws of _SDA_BASE_ address calcs because output_offset != 0. */
783 struct elf_link_hash_entry
*h
;
784 asection
*s
= bfd_get_section_by_name (abfd
, ".sdata");
786 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
790 int flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
791 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
793 s
= bfd_make_section_anyway (abfd
, ".sdata");
796 bfd_set_section_flags (abfd
, s
, flags
);
797 bfd_set_section_alignment (abfd
, s
, 2);
800 h
= (struct elf_link_hash_entry
*)
801 bfd_link_hash_lookup (info
->hash
, "_SDA_BASE_", false, false, false);
803 if ((h
== NULL
|| h
->root
.type
== bfd_link_hash_undefined
)
804 && !(_bfd_generic_link_add_one_symbol (info
,
812 get_elf_backend_data (abfd
)->collect
,
813 (struct bfd_link_hash_entry
**) &h
)))
815 h
->type
= STT_OBJECT
;
818 switch (sym
->st_shndx
)
820 case SHN_M32R_SCOMMON
:
821 *secp
= bfd_make_section_old_way (abfd
, ".scommon");
822 (*secp
)->flags
|= SEC_IS_COMMON
;
823 *valp
= sym
->st_size
;
830 /* We have to figure out the SDA_BASE value, so that we can adjust the
831 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
832 BFD. If we can't find it, we're stuck. We cache it in the ELF
833 target data. We don't need to adjust the symbol value for an
834 external symbol if we are producing relocateable output. */
836 static bfd_reloc_status_type
837 m32r_elf_final_sda_base (output_bfd
, info
, error_message
, psb
)
839 struct bfd_link_info
*info
;
840 const char **error_message
;
843 if (elf_gp (output_bfd
) == 0)
845 struct bfd_link_hash_entry
*h
;
847 h
= bfd_link_hash_lookup (info
->hash
, "_SDA_BASE_", false, false, true);
848 if (h
!= (struct bfd_link_hash_entry
*) NULL
849 && h
->type
== bfd_link_hash_defined
)
850 elf_gp (output_bfd
) = (h
->u
.def
.value
851 + h
->u
.def
.section
->output_section
->vma
852 + h
->u
.def
.section
->output_offset
);
855 /* Only get the error once. */
856 *psb
= elf_gp (output_bfd
) = 4;
858 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
859 return bfd_reloc_dangerous
;
862 *psb
= elf_gp (output_bfd
);
866 /* Relocate an M32R/D ELF section.
867 There is some attempt to make this function usable for many architectures,
868 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
869 if only to serve as a learning tool.
871 The RELOCATE_SECTION function is called by the new ELF backend linker
872 to handle the relocations for a section.
874 The relocs are always passed as Rela structures; if the section
875 actually uses Rel structures, the r_addend field will always be
878 This function is responsible for adjust the section contents as
879 necessary, and (if using Rela relocs and generating a
880 relocateable output file) adjusting the reloc addend as
883 This function does not have to worry about setting the reloc
884 address or the reloc symbol index.
886 LOCAL_SYMS is a pointer to the swapped in local symbols.
888 LOCAL_SECTIONS is an array giving the section in the input file
889 corresponding to the st_shndx field of each local symbol.
891 The global hash table entry for the global symbols can be found
892 via elf_sym_hashes (input_bfd).
894 When generating relocateable output, this function must handle
895 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
896 going to be the section symbol corresponding to the output
897 section, which means that the addend must be adjusted
901 m32r_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
902 contents
, relocs
, local_syms
, local_sections
)
903 bfd
*output_bfd ATTRIBUTE_UNUSED
;
904 struct bfd_link_info
*info
;
906 asection
*input_section
;
908 Elf_Internal_Rela
*relocs
;
909 Elf_Internal_Sym
*local_syms
;
910 asection
**local_sections
;
912 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
913 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
914 Elf_Internal_Rela
*rel
, *relend
;
915 /* Assume success. */
919 relend
= relocs
+ input_section
->reloc_count
;
920 for (; rel
< relend
; rel
++)
923 reloc_howto_type
*howto
;
924 unsigned long r_symndx
;
925 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
926 ensure it's zero (we use REL relocs, not RELA). Therefore this
927 should be assigning zero to `addend', but for clarity we use
929 bfd_vma addend
= rel
->r_addend
;
930 bfd_vma offset
= rel
->r_offset
;
931 struct elf_link_hash_entry
*h
;
932 Elf_Internal_Sym
*sym
;
934 const char *sym_name
;
935 bfd_reloc_status_type r
;
936 const char *errmsg
= NULL
;
939 r_type
= ELF32_R_TYPE (rel
->r_info
);
940 if (r_type
< 0 || r_type
>= (int) R_M32R_max
)
942 (*_bfd_error_handler
) (_("%s: unknown relocation type %d"),
943 bfd_get_filename (input_bfd
),
945 bfd_set_error (bfd_error_bad_value
);
950 if (r_type
== R_M32R_GNU_VTENTRY
951 || r_type
== R_M32R_GNU_VTINHERIT
)
954 howto
= m32r_elf_howto_table
+ r_type
;
955 r_symndx
= ELF32_R_SYM (rel
->r_info
);
957 if (info
->relocateable
)
959 /* This is a relocateable link. We don't have to change
960 anything, unless the reloc is against a section symbol,
961 in which case we have to adjust according to where the
962 section symbol winds up in the output section. */
964 if (r_symndx
>= symtab_hdr
->sh_info
)
966 /* External symbol. */
971 sym
= local_syms
+ r_symndx
;
972 sym_name
= "<local symbol>";
973 /* STT_SECTION: symbol is associated with a section. */
974 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
976 /* Symbol isn't associated with a section. Nothing to do. */
980 sec
= local_sections
[r_symndx
];
981 addend
+= sec
->output_offset
+ sym
->st_value
;
983 /* This can't be done for USE_REL because it doesn't mean anything
984 and elf_link_input_bfd asserts this stays zero. */
985 rel
->r_addend
= addend
;
989 /* Addends are stored with relocs. We're done. */
992 /* If partial_inplace, we need to store any additional addend
993 back in the section. */
994 if (! howto
->partial_inplace
)
996 /* ??? Here is a nice place to call a special_function
998 if (r_type
!= R_M32R_HI16_SLO
&& r_type
!= R_M32R_HI16_ULO
)
999 r
= _bfd_relocate_contents (howto
, input_bfd
,
1000 addend
, contents
+ offset
);
1003 Elf_Internal_Rela
*lorel
;
1005 /* We allow an arbitrary number of HI16 relocs before the
1006 LO16 reloc. This permits gcc to emit the HI and LO relocs
1008 for (lorel
= rel
+ 1;
1010 && (ELF32_R_TYPE (lorel
->r_info
) == R_M32R_HI16_SLO
1011 || ELF32_R_TYPE (lorel
->r_info
) == R_M32R_HI16_ULO
));
1015 && ELF32_R_TYPE (lorel
->r_info
) == R_M32R_LO16
)
1017 m32r_elf_relocate_hi16 (input_bfd
, r_type
, rel
, lorel
,
1022 r
= _bfd_relocate_contents (howto
, input_bfd
,
1023 addend
, contents
+ offset
);
1025 #endif /* USE_REL */
1031 /* This is a final link. */
1035 if (r_symndx
< symtab_hdr
->sh_info
)
1038 sym
= local_syms
+ r_symndx
;
1039 sec
= local_sections
[r_symndx
];
1040 sym_name
= "<local symbol>";
1041 relocation
= (sec
->output_section
->vma
1042 + sec
->output_offset
1047 /* External symbol. */
1048 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1049 while (h
->root
.type
== bfd_link_hash_indirect
1050 || h
->root
.type
== bfd_link_hash_warning
)
1051 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1052 sym_name
= h
->root
.root
.string
;
1054 if (h
->root
.type
== bfd_link_hash_defined
1055 || h
->root
.type
== bfd_link_hash_defweak
)
1057 sec
= h
->root
.u
.def
.section
;
1058 if (sec
->output_section
== NULL
)
1061 relocation
= (h
->root
.u
.def
.value
1062 + sec
->output_section
->vma
1063 + sec
->output_offset
);
1065 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1069 if (! ((*info
->callbacks
->undefined_symbol
)
1070 (info
, h
->root
.root
.string
, input_bfd
,
1071 input_section
, offset
, true)))
1077 /* Sanity check the address. */
1078 if (offset
> input_section
->_raw_size
)
1080 r
= bfd_reloc_outofrange
;
1084 switch ((int) r_type
)
1086 case (int) R_M32R_10_PCREL
:
1087 r
= m32r_elf_do_10_pcrel_reloc (input_bfd
, howto
, input_section
,
1089 sec
, relocation
, addend
);
1092 case (int) R_M32R_HI16_SLO
:
1093 case (int) R_M32R_HI16_ULO
:
1095 Elf_Internal_Rela
*lorel
;
1097 /* We allow an arbitrary number of HI16 relocs before the
1098 LO16 reloc. This permits gcc to emit the HI and LO relocs
1100 for (lorel
= rel
+ 1;
1102 && (ELF32_R_TYPE (lorel
->r_info
) == R_M32R_HI16_SLO
1103 || ELF32_R_TYPE (lorel
->r_info
) == R_M32R_HI16_ULO
));
1107 && ELF32_R_TYPE (lorel
->r_info
) == R_M32R_LO16
)
1109 m32r_elf_relocate_hi16 (input_bfd
, r_type
, rel
, lorel
,
1110 contents
, relocation
+ addend
);
1114 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1116 relocation
, addend
);
1120 case (int) R_M32R_SDA16
:
1124 BFD_ASSERT (sec
!= NULL
);
1125 name
= bfd_get_section_name (abfd
, sec
);
1127 if (strcmp (name
, ".sdata") == 0
1128 || strcmp (name
, ".sbss") == 0
1129 || strcmp (name
, ".scommon") == 0)
1132 bfd
*out_bfd
= sec
->output_section
->owner
;
1134 r
= m32r_elf_final_sda_base (out_bfd
, info
,
1137 if (r
!= bfd_reloc_ok
)
1143 /* At this point `relocation' contains the object's
1145 relocation
-= sda_base
;
1146 /* Now it contains the offset from _SDA_BASE_. */
1150 (*_bfd_error_handler
) (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
1151 bfd_get_filename (input_bfd
),
1153 m32r_elf_howto_table
[(int) r_type
].name
,
1154 bfd_get_section_name (abfd
, sec
));
1155 /*bfd_set_error (bfd_error_bad_value); ??? why? */
1163 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1165 relocation
, addend
);
1172 if (r
!= bfd_reloc_ok
)
1174 /* FIXME: This should be generic enough to go in a utility. */
1178 name
= h
->root
.root
.string
;
1181 name
= (bfd_elf_string_from_elf_section
1182 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1183 if (name
== NULL
|| *name
== '\0')
1184 name
= bfd_section_name (input_bfd
, sec
);
1192 case bfd_reloc_overflow
:
1193 if (! ((*info
->callbacks
->reloc_overflow
)
1194 (info
, name
, howto
->name
, (bfd_vma
) 0,
1195 input_bfd
, input_section
, offset
)))
1199 case bfd_reloc_undefined
:
1200 if (! ((*info
->callbacks
->undefined_symbol
)
1201 (info
, name
, input_bfd
, input_section
,
1206 case bfd_reloc_outofrange
:
1207 errmsg
= _("internal error: out of range error");
1210 case bfd_reloc_notsupported
:
1211 errmsg
= _("internal error: unsupported relocation error");
1214 case bfd_reloc_dangerous
:
1215 errmsg
= _("internal error: dangerous error");
1219 errmsg
= _("internal error: unknown error");
1223 if (!((*info
->callbacks
->warning
)
1224 (info
, errmsg
, name
, input_bfd
, input_section
,
1235 #if 0 /* relaxing not supported yet */
1237 /* This function handles relaxing for the m32r.
1238 Relaxing on the m32r is tricky because of instruction alignment
1239 requirements (4 byte instructions must be aligned on 4 byte boundaries).
1241 The following relaxing opportunities are handled:
1243 seth/add3/jl -> bl24 or bl8
1246 It would be nice to handle bl24 -> bl8 but given:
1248 - 4 byte insns must be on 4 byte boundaries
1249 - branch instructions only branch to insns on 4 byte boundaries
1251 this isn't much of a win because the insn in the 2 "deleted" bytes
1252 must become a nop. With some complexity some real relaxation could be
1253 done but the frequency just wouldn't make it worth it; it's better to
1254 try to do all the code compaction one can elsewhere.
1255 When the chip supports parallel 16 bit insns, things may change.
1259 m32r_elf_relax_section (abfd
, sec
, link_info
, again
)
1262 struct bfd_link_info
*link_info
;
1265 Elf_Internal_Shdr
*symtab_hdr
;
1266 /* The Rela structures are used here because that's what
1267 _bfd_elf32_link_read_relocs uses [for convenience - it sets the addend
1269 Elf_Internal_Rela
*internal_relocs
;
1270 Elf_Internal_Rela
*free_relocs
= NULL
;
1271 Elf_Internal_Rela
*irel
, *irelend
;
1272 bfd_byte
*contents
= NULL
;
1273 bfd_byte
*free_contents
= NULL
;
1274 Elf32_External_Sym
*extsyms
= NULL
;
1275 Elf32_External_Sym
*free_extsyms
= NULL
;
1277 /* Assume nothing changes. */
1280 /* We don't have to do anything for a relocateable link, if
1281 this section does not have relocs, or if this is not a
1283 if (link_info
->relocateable
1284 || (sec
->flags
& SEC_RELOC
) == 0
1285 || sec
->reloc_count
== 0
1286 || (sec
->flags
& SEC_CODE
) == 0
1287 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
1290 /* If this is the first time we have been called for this section,
1291 initialize the cooked size. */
1292 if (sec
->_cooked_size
== 0)
1293 sec
->_cooked_size
= sec
->_raw_size
;
1295 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1297 /* Get a copy of the native relocations. */
1298 internal_relocs
= (_bfd_elf32_link_read_relocs
1299 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1300 link_info
->keep_memory
));
1301 if (internal_relocs
== NULL
)
1303 if (! link_info
->keep_memory
)
1304 free_relocs
= internal_relocs
;
1306 /* Walk through them looking for relaxing opportunities. */
1307 irelend
= internal_relocs
+ sec
->reloc_count
;
1308 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1312 /* If this isn't something that can be relaxed, then ignore
1314 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_M32R_HI16_SLO
)
1317 /* Get the section contents if we haven't done so already. */
1318 if (contents
== NULL
)
1320 /* Get cached copy if it exists. */
1321 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1322 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1325 /* Go get them off disk. */
1326 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1327 if (contents
== NULL
)
1329 free_contents
= contents
;
1331 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1332 (file_ptr
) 0, sec
->_raw_size
))
1337 /* Read this BFD's symbols if we haven't done so already. */
1338 if (extsyms
== NULL
)
1340 /* Get cached copy if it exists. */
1341 if (symtab_hdr
->contents
!= NULL
)
1342 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1345 /* Go get them off disk. */
1346 extsyms
= ((Elf32_External_Sym
*)
1347 bfd_malloc (symtab_hdr
->sh_size
));
1348 if (extsyms
== NULL
)
1350 free_extsyms
= extsyms
;
1351 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1352 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1353 != symtab_hdr
->sh_size
))
1358 /* Get the value of the symbol referred to by the reloc. */
1359 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1361 Elf_Internal_Sym isym
;
1364 /* A local symbol. */
1365 bfd_elf32_swap_symbol_in (abfd
,
1366 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1369 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
1370 symval
= (isym
.st_value
1371 + sym_sec
->output_section
->vma
1372 + sym_sec
->output_offset
);
1377 struct elf_link_hash_entry
*h
;
1379 /* An external symbol. */
1380 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1381 h
= elf_sym_hashes (abfd
)[indx
];
1382 BFD_ASSERT (h
!= NULL
);
1383 if (h
->root
.type
!= bfd_link_hash_defined
1384 && h
->root
.type
!= bfd_link_hash_defweak
)
1386 /* This appears to be a reference to an undefined
1387 symbol. Just ignore it--it will be caught by the
1388 regular reloc processing. */
1392 symval
= (h
->root
.u
.def
.value
1393 + h
->root
.u
.def
.section
->output_section
->vma
1394 + h
->root
.u
.def
.section
->output_offset
);
1397 /* For simplicity of coding, we are going to modify the section
1398 contents, the section relocs, and the BFD symbol table. We
1399 must tell the rest of the code not to free up this
1400 information. It would be possible to instead create a table
1401 of changes which have to be made, as is done in coff-mips.c;
1402 that would be more work, but would require less memory when
1403 the linker is run. */
1405 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
1406 This sequence is generated by the compiler when compiling in
1407 32 bit mode. Also look for seth/add3 -> ld24. */
1409 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_M32R_HI16_SLO
)
1411 Elf_Internal_Rela
*nrel
;
1412 bfd_vma pc
= (sec
->output_section
->vma
+ sec
->output_offset
1414 bfd_signed_vma pcrel_value
= symval
- pc
;
1415 unsigned int code
,reg
;
1416 int addend
,nop_p
,bl8_p
,to_delete
;
1418 /* The tests are ordered so that we get out as quickly as possible
1419 if this isn't something we can relax, taking into account that
1420 we are looking for two separate possibilities (jl/ld24). */
1422 /* Do nothing if no room in the section for this to be what we're
1424 if (irel
->r_offset
> sec
->_cooked_size
- 8)
1427 /* Make sure the next relocation applies to the next
1428 instruction and that it's the add3's reloc. */
1431 || irel
->r_offset
+ 4 != nrel
->r_offset
1432 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_M32R_LO16
)
1435 /* See if the instructions are seth/add3. */
1436 /* FIXME: This is where macros from cgen can come in. */
1437 code
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
+ 0);
1438 if ((code
& 0xf0ff) != 0xd0c0)
1439 continue; /* not seth rN,foo */
1440 reg
= (code
& 0x0f00) >> 8;
1441 code
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
+ 4);
1442 if (code
!= (0x80a0 | reg
| (reg
<< 8)))
1443 continue; /* not add3 rN,rN,foo */
1445 /* At this point we've confirmed we have seth/add3. Now check
1446 whether the next insn is a jl, in which case try to change this
1449 /* Ensure the branch target is in range.
1450 The bl24 instruction has a 24 bit operand which is the target
1451 address right shifted by 2, giving a signed range of 26 bits.
1452 Note that 4 bytes are added to the high value because the target
1453 will be at least 4 bytes closer if we can relax. It'll actually
1454 be 4 or 8 bytes closer, but we don't know which just yet and
1455 the difference isn't significant enough to worry about. */
1456 #ifndef USE_REL /* put in for learning purposes */
1457 pcrel_value
+= irel
->r_addend
;
1459 addend
= bfd_get_signed_16 (abfd
, contents
+ irel
->r_offset
+ 2);
1460 pcrel_value
+= addend
;
1463 if (pcrel_value
>= -(1 << 25) && pcrel_value
< (1 << 25) + 4
1464 /* Do nothing if no room in the section for this to be what we're
1466 && (irel
->r_offset
<= sec
->_cooked_size
- 12)
1467 /* Ensure the next insn is "jl rN". */
1468 && ((code
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
+ 8)),
1469 code
!= (0x1ec0 | reg
)))
1471 /* We can relax to bl24/bl8. */
1473 /* See if there's a nop following the jl.
1474 Also see if we can use a bl8 insn. */
1475 code
= bfd_get_16 (abfd
, contents
+ irel
->r_offset
+ 10);
1476 nop_p
= (code
& 0x7fff) == NOP_INSN
;
1477 bl8_p
= pcrel_value
>= -0x200 && pcrel_value
< 0x200;
1481 /* Change "seth rN,foo" to "bl8 foo || nop".
1482 We OR in CODE just in case it's not a nop (technically,
1483 CODE currently must be a nop, but for cleanness we
1484 allow it to be anything). */
1485 #ifndef USE_REL /* put in for learning purposes */
1486 code
= 0x7e000000 | MAKE_PARALLEL (code
);
1488 code
= (0x7e000000 + (((addend
>> 2) & 0xff) << 16)) | MAKE_PARALLEL (code
);
1494 /* Change the seth rN,foo to a bl24 foo. */
1495 #ifndef USE_REL /* put in for learning purposes */
1498 code
= 0xfe000000 + ((addend
>> 2) & 0xffffff);
1500 to_delete
= nop_p
? 8 : 4;
1503 bfd_put_32 (abfd
, code
, contents
+ irel
->r_offset
);
1505 /* Set the new reloc type. */
1506 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
1507 bl8_p
? R_M32R_10_PCREL
: R_M32R_26_PCREL
);
1509 /* Delete the add3 reloc by making it a null reloc. */
1510 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
1513 else if (addend
>= 0
1514 && symval
+ addend
<= 0xffffff)
1516 /* We can relax to ld24. */
1518 code
= 0xe0000000 | (reg
<< 24) | (addend
& 0xffffff);
1519 bfd_put_32 (abfd
, code
, contents
+ irel
->r_offset
);
1521 /* Tell the following code a nop filler isn't needed. */
1526 /* Can't do anything here. */
1530 /* Note that we've changed the relocs, section contents, etc. */
1531 elf_section_data (sec
)->relocs
= internal_relocs
;
1534 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1535 free_contents
= NULL
;
1537 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1538 free_extsyms
= NULL
;
1540 /* Delete TO_DELETE bytes of data. */
1541 if (!m32r_elf_relax_delete_bytes (abfd
, sec
,
1542 irel
->r_offset
+ 4, to_delete
))
1545 /* Now that the following bytes have been moved into place, see if
1546 we need to replace the jl with a nop. This happens when we had
1547 to use a bl24 insn and the insn following the jl isn't a nop.
1548 Technically, this situation can't happen (since the insn can
1549 never be executed) but to be clean we do this. When the chip
1550 supports parallel 16 bit insns things may change.
1551 We don't need to do this in the case of relaxing to ld24,
1552 and the above code sets nop_p so this isn't done. */
1553 if (! nop_p
&& to_delete
== 4)
1554 bfd_put_16 (abfd
, NOP_INSN
, contents
+ irel
->r_offset
+ 4);
1556 /* That will change things, so we should relax again.
1557 Note that this is not required, and it may be slow. */
1563 /* loop to try the next reloc */
1566 if (free_relocs
!= NULL
)
1572 if (free_contents
!= NULL
)
1574 if (! link_info
->keep_memory
)
1575 free (free_contents
);
1578 /* Cache the section contents for elf_link_input_bfd. */
1579 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1581 free_contents
= NULL
;
1584 if (free_extsyms
!= NULL
)
1586 if (! link_info
->keep_memory
)
1587 free (free_extsyms
);
1590 /* Cache the symbols for elf_link_input_bfd. */
1591 symtab_hdr
->contents
= extsyms
;
1593 free_extsyms
= NULL
;
1599 if (free_relocs
!= NULL
)
1601 if (free_contents
!= NULL
)
1602 free (free_contents
);
1603 if (free_extsyms
!= NULL
)
1604 free (free_extsyms
);
1608 /* Delete some bytes from a section while relaxing. */
1611 m32r_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1617 Elf_Internal_Shdr
*symtab_hdr
;
1618 Elf32_External_Sym
*extsyms
;
1621 Elf_Internal_Rela
*irel
, *irelend
;
1622 Elf_Internal_Rela
*irelalign
;
1624 Elf32_External_Sym
*esym
, *esymend
;
1625 struct elf_link_hash_entry
*sym_hash
;
1627 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1628 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1630 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1632 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1634 /* The deletion must stop at the next ALIGN reloc for an aligment
1635 power larger than the number of bytes we are deleting. */
1638 toaddr
= sec
->_cooked_size
;
1640 irel
= elf_section_data (sec
)->relocs
;
1641 irelend
= irel
+ sec
->reloc_count
;
1643 /* Actually delete the bytes. */
1644 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1645 sec
->_cooked_size
-= count
;
1647 /* Adjust all the relocs. */
1648 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1650 /* Get the new reloc address. */
1651 if ((irel
->r_offset
> addr
1652 && irel
->r_offset
< toaddr
))
1653 irel
->r_offset
-= count
;
1656 /* Adjust the local symbols defined in this section. */
1658 esymend
= esym
+ symtab_hdr
->sh_info
;
1659 for (; esym
< esymend
; esym
++)
1661 Elf_Internal_Sym isym
;
1663 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1665 if (isym
.st_shndx
== shndx
1666 && isym
.st_value
> addr
1667 && isym
.st_value
< toaddr
)
1669 isym
.st_value
-= count
;
1670 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1674 /* Now adjust the global symbols defined in this section. */
1675 esym
= extsyms
+ symtab_hdr
->sh_info
;
1676 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1677 for (index
= 0; esym
< esymend
; esym
++, index
++)
1679 Elf_Internal_Sym isym
;
1681 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1682 sym_hash
= elf_sym_hashes (abfd
)[index
];
1683 if (isym
.st_shndx
== shndx
1684 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1685 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1686 && (sym_hash
)->root
.u
.def
.section
== sec
1687 && (sym_hash
)->root
.u
.def
.value
> addr
1688 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1690 (sym_hash
)->root
.u
.def
.value
-= count
;
1697 /* This is a version of bfd_generic_get_relocated_section_contents
1698 which uses m32r_elf_relocate_section. */
1701 m32r_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1702 data
, relocateable
, symbols
)
1704 struct bfd_link_info
*link_info
;
1705 struct bfd_link_order
*link_order
;
1707 boolean relocateable
;
1710 Elf_Internal_Shdr
*symtab_hdr
;
1711 asection
*input_section
= link_order
->u
.indirect
.section
;
1712 bfd
*input_bfd
= input_section
->owner
;
1713 asection
**sections
= NULL
;
1714 Elf_Internal_Rela
*internal_relocs
= NULL
;
1715 Elf32_External_Sym
*external_syms
= NULL
;
1716 Elf_Internal_Sym
*internal_syms
= NULL
;
1718 /* We only need to handle the case of relaxing, or of having a
1719 particular set of section contents, specially. */
1721 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1722 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1727 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1729 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1730 input_section
->_raw_size
);
1732 if ((input_section
->flags
& SEC_RELOC
) != 0
1733 && input_section
->reloc_count
> 0)
1735 Elf_Internal_Sym
*isymp
;
1737 Elf32_External_Sym
*esym
, *esymend
;
1739 if (symtab_hdr
->contents
!= NULL
)
1740 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1743 external_syms
= ((Elf32_External_Sym
*)
1744 bfd_malloc (symtab_hdr
->sh_info
1745 * sizeof (Elf32_External_Sym
)));
1746 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1748 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1749 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
1750 symtab_hdr
->sh_info
, input_bfd
)
1751 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
1755 internal_relocs
= (_bfd_elf32_link_read_relocs
1756 (input_bfd
, input_section
, (PTR
) NULL
,
1757 (Elf_Internal_Rela
*) NULL
, false));
1758 if (internal_relocs
== NULL
)
1761 internal_syms
= ((Elf_Internal_Sym
*)
1762 bfd_malloc (symtab_hdr
->sh_info
1763 * sizeof (Elf_Internal_Sym
)));
1764 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1767 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
1768 * sizeof (asection
*));
1769 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
1772 isymp
= internal_syms
;
1774 esym
= external_syms
;
1775 esymend
= esym
+ symtab_hdr
->sh_info
;
1776 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
1780 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
1782 if (isymp
->st_shndx
== SHN_UNDEF
)
1783 isec
= bfd_und_section_ptr
;
1784 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
1785 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1786 else if (isymp
->st_shndx
== SHN_ABS
)
1787 isec
= bfd_abs_section_ptr
;
1788 else if (isymp
->st_shndx
== SHN_COMMON
)
1789 isec
= bfd_com_section_ptr
;
1790 else if (isymp
->st_shndx
== SHN_M32R_SCOMMON
)
1791 isec
= &m32r_elf_scom_section
;
1801 if (! m32r_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1802 input_section
, data
, internal_relocs
,
1803 internal_syms
, sections
))
1806 if (sections
!= NULL
)
1809 if (internal_syms
!= NULL
)
1810 free (internal_syms
);
1811 internal_syms
= NULL
;
1812 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1813 free (external_syms
);
1814 external_syms
= NULL
;
1815 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1816 free (internal_relocs
);
1817 internal_relocs
= NULL
;
1823 if (internal_relocs
!= NULL
1824 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1825 free (internal_relocs
);
1826 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1827 free (external_syms
);
1828 if (internal_syms
!= NULL
)
1829 free (internal_syms
);
1830 if (sections
!= NULL
)
1837 /* Set the right machine number. */
1839 m32r_elf_object_p (abfd
)
1842 switch (elf_elfheader (abfd
)->e_flags
& EF_M32R_ARCH
)
1845 case E_M32R_ARCH
: (void) bfd_default_set_arch_mach (abfd
, bfd_arch_m32r
, bfd_mach_m32r
); break;
1846 case E_M32RX_ARCH
: (void) bfd_default_set_arch_mach (abfd
, bfd_arch_m32r
, bfd_mach_m32rx
); break;
1851 /* Store the machine number in the flags field. */
1853 m32r_elf_final_write_processing (abfd
, linker
)
1855 boolean linker ATTRIBUTE_UNUSED
;
1859 switch (bfd_get_mach (abfd
))
1862 case bfd_mach_m32r
: val
= E_M32R_ARCH
; break;
1863 case bfd_mach_m32rx
: val
= E_M32RX_ARCH
; break;
1866 elf_elfheader (abfd
)->e_flags
&=~ EF_M32R_ARCH
;
1867 elf_elfheader (abfd
)->e_flags
|= val
;
1870 /* Function to keep M32R specific file flags. */
1872 m32r_elf_set_private_flags (abfd
, flags
)
1876 BFD_ASSERT (!elf_flags_init (abfd
)
1877 || elf_elfheader (abfd
)->e_flags
== flags
);
1879 elf_elfheader (abfd
)->e_flags
= flags
;
1880 elf_flags_init (abfd
) = true;
1884 /* Copy backend specific data from one object module to another */
1886 m32r_elf_copy_private_bfd_data (ibfd
, obfd
)
1890 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1891 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1894 BFD_ASSERT (!elf_flags_init (obfd
)
1895 || (elf_elfheader (obfd
)->e_flags
1896 == elf_elfheader (ibfd
)->e_flags
));
1898 elf_gp (obfd
) = elf_gp (ibfd
);
1899 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1900 elf_flags_init (obfd
) = true;
1904 /* Merge backend specific data from an object file to the output
1905 object file when linking. */
1907 m32r_elf_merge_private_bfd_data (ibfd
, obfd
)
1914 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1915 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1918 in_flags
= elf_elfheader (ibfd
)->e_flags
;
1919 out_flags
= elf_elfheader (obfd
)->e_flags
;
1921 if (! elf_flags_init (obfd
))
1923 /* If the input is the default architecture then do not
1924 bother setting the flags for the output architecture,
1925 instead allow future merges to do this. If no future
1926 merges ever set these flags then they will retain their
1927 unitialised values, which surprise surprise, correspond
1928 to the default values. */
1929 if (bfd_get_arch_info (ibfd
)->the_default
)
1932 elf_flags_init (obfd
) = true;
1933 elf_elfheader (obfd
)->e_flags
= in_flags
;
1935 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
1936 && bfd_get_arch_info (obfd
)->the_default
)
1938 return bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
), bfd_get_mach (ibfd
));
1944 /* Check flag compatibility. */
1945 if (in_flags
== out_flags
)
1948 if ((in_flags
& EF_M32R_ARCH
) != (out_flags
& EF_M32R_ARCH
))
1950 if ((in_flags
& EF_M32R_ARCH
) != E_M32R_ARCH
)
1952 _bfd_error_handler (_("%s: Instruction set mismatch with previous modules"),
1953 bfd_get_filename (ibfd
));
1955 bfd_set_error (bfd_error_bad_value
);
1963 /* Display the flags field */
1965 m32r_elf_print_private_bfd_data (abfd
, ptr
)
1969 FILE * file
= (FILE *) ptr
;
1971 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
)
1973 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1975 fprintf (file
, _("private flags = %lx"), elf_elfheader (abfd
)->e_flags
);
1977 switch (elf_elfheader (abfd
)->e_flags
& EF_M32R_ARCH
)
1980 case E_M32R_ARCH
: fprintf (file
, _(": m32r instructions")); break;
1981 case E_M32RX_ARCH
: fprintf (file
, _(": m32rx instructions")); break;
1990 m32r_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
1992 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1993 Elf_Internal_Rela
*rel
;
1994 struct elf_link_hash_entry
*h
;
1995 Elf_Internal_Sym
*sym
;
1999 switch (ELF32_R_TYPE (rel
->r_info
))
2001 case R_M32R_GNU_VTINHERIT
:
2002 case R_M32R_GNU_VTENTRY
:
2006 switch (h
->root
.type
)
2008 case bfd_link_hash_defined
:
2009 case bfd_link_hash_defweak
:
2010 return h
->root
.u
.def
.section
;
2012 case bfd_link_hash_common
:
2013 return h
->root
.u
.c
.p
->section
;
2022 if (!(elf_bad_symtab (abfd
)
2023 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
2024 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
2025 && sym
->st_shndx
!= SHN_COMMON
))
2027 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
2034 m32r_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
2035 bfd
*abfd ATTRIBUTE_UNUSED
;
2036 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
2037 asection
*sec ATTRIBUTE_UNUSED
;
2038 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
;
2040 /* we don't use got and plt entries for m32r */
2045 /* Look through the relocs for a section during the first phase.
2046 Since we don't do .gots or .plts, we just need to consider the
2047 virtual table relocs for gc. */
2050 m32r_elf_check_relocs (abfd
, info
, sec
, relocs
)
2052 struct bfd_link_info
*info
;
2054 const Elf_Internal_Rela
*relocs
;
2056 Elf_Internal_Shdr
*symtab_hdr
;
2057 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
2058 const Elf_Internal_Rela
*rel
;
2059 const Elf_Internal_Rela
*rel_end
;
2061 if (info
->relocateable
)
2064 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2065 sym_hashes
= elf_sym_hashes (abfd
);
2066 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof(Elf32_External_Sym
);
2067 if (!elf_bad_symtab (abfd
))
2068 sym_hashes_end
-= symtab_hdr
->sh_info
;
2070 rel_end
= relocs
+ sec
->reloc_count
;
2071 for (rel
= relocs
; rel
< rel_end
; rel
++)
2073 struct elf_link_hash_entry
*h
;
2074 unsigned long r_symndx
;
2076 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2077 if (r_symndx
< symtab_hdr
->sh_info
)
2080 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
2082 switch (ELF32_R_TYPE (rel
->r_info
))
2084 /* This relocation describes the C++ object vtable hierarchy.
2085 Reconstruct it for later use during GC. */
2086 case R_M32R_GNU_VTINHERIT
:
2087 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
2091 /* This relocation describes which C++ vtable entries are actually
2092 used. Record for later use during GC. */
2093 case R_M32R_GNU_VTENTRY
:
2094 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_offset
))
2106 #define ELF_ARCH bfd_arch_m32r
2107 #define ELF_MACHINE_CODE EM_CYGNUS_M32R
2108 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
2110 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
2111 #define TARGET_BIG_NAME "elf32-m32r"
2113 #define elf_info_to_howto 0
2114 #define elf_info_to_howto_rel m32r_info_to_howto_rel
2115 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
2116 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
2117 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
2118 #define elf_backend_relocate_section m32r_elf_relocate_section
2119 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
2120 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
2121 #define elf_backend_check_relocs m32r_elf_check_relocs
2123 #define elf_backend_can_gc_sections 1
2126 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
2127 #define bfd_elf32_bfd_get_relocated_section_contents \
2128 m32r_elf_get_relocated_section_contents
2131 #define elf_backend_object_p m32r_elf_object_p
2132 #define elf_backend_final_write_processing m32r_elf_final_write_processing
2133 #define bfd_elf32_bfd_copy_private_bfd_data m32r_elf_copy_private_bfd_data
2134 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
2135 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
2136 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
2138 #include "elf32-target.h"