2001-02-09 Bo Thorsen <bo@suse.de>
[binutils.git] / bfd / elf32-m32r.c
blobcfdb1c3b2ce7cf4d945a380e1ee05755bbbb4a53
1 /* M32R-specific support for 32-bit ELF.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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. */
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/m32r.h"
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 bfd_reloc_status_type m32r_elf_generic_reloc
39 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
40 static bfd_reloc_status_type m32r_elf_sda16_reloc
41 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
42 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
43 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
44 static void m32r_info_to_howto_rel
45 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
46 boolean _bfd_m32r_elf_section_from_bfd_section
47 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
48 void _bfd_m32r_elf_symbol_processing
49 PARAMS ((bfd *, asymbol *));
50 static boolean m32r_elf_add_symbol_hook
51 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
52 const char **, flagword *, asection **, bfd_vma *));
53 static boolean m32r_elf_relocate_section
54 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
55 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
56 #if 0 /* not yet */
57 static boolean m32r_elf_relax_delete_bytes
58 PARAMS ((bfd *, asection *, bfd_vma, int));
59 #endif
61 static bfd_reloc_status_type m32r_elf_final_sda_base
62 PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
63 static boolean m32r_elf_object_p
64 PARAMS ((bfd *));
65 static void m32r_elf_final_write_processing
66 PARAMS ((bfd *, boolean));
67 static boolean m32r_elf_set_private_flags
68 PARAMS ((bfd *, flagword));
69 static boolean m32r_elf_copy_private_bfd_data
70 PARAMS ((bfd *, bfd *));
71 static boolean m32r_elf_merge_private_bfd_data
72 PARAMS ((bfd *, bfd *));
73 static boolean m32r_elf_print_private_bfd_data
74 PARAMS ((bfd *, PTR));
76 #define NOP_INSN 0x7000
77 #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
79 /* Use REL instead of RELA to save space.
80 This only saves space in libraries and object files, but perhaps
81 relocs will be put in ROM? All in all though, REL relocs are a pain
82 to work with. */
83 #define USE_REL
85 static reloc_howto_type m32r_elf_howto_table[] =
87 /* This reloc does nothing. */
88 HOWTO (R_M32R_NONE, /* type */
89 0, /* rightshift */
90 2, /* size (0 = byte, 1 = short, 2 = long) */
91 32, /* bitsize */
92 false, /* pc_relative */
93 0, /* bitpos */
94 complain_overflow_bitfield, /* complain_on_overflow */
95 bfd_elf_generic_reloc, /* special_function */
96 "R_M32R_NONE", /* name */
97 false, /* partial_inplace */
98 0, /* src_mask */
99 0, /* dst_mask */
100 false), /* pcrel_offset */
102 /* A 16 bit absolute relocation. */
103 HOWTO (R_M32R_16, /* type */
104 0, /* rightshift */
105 1, /* size (0 = byte, 1 = short, 2 = long) */
106 16, /* bitsize */
107 false, /* pc_relative */
108 0, /* bitpos */
109 complain_overflow_bitfield, /* complain_on_overflow */
110 m32r_elf_generic_reloc,/* special_function */
111 "R_M32R_16", /* name */
112 true, /* partial_inplace */
113 0xffff, /* src_mask */
114 0xffff, /* dst_mask */
115 false), /* pcrel_offset */
117 /* A 32 bit absolute relocation. */
118 HOWTO (R_M32R_32, /* type */
119 0, /* rightshift */
120 2, /* size (0 = byte, 1 = short, 2 = long) */
121 32, /* bitsize */
122 false, /* pc_relative */
123 0, /* bitpos */
124 complain_overflow_bitfield, /* complain_on_overflow */
125 m32r_elf_generic_reloc,/* special_function */
126 "R_M32R_32", /* name */
127 true, /* partial_inplace */
128 0xffffffff, /* src_mask */
129 0xffffffff, /* dst_mask */
130 false), /* pcrel_offset */
132 /* A 24 bit address. */
133 HOWTO (R_M32R_24, /* type */
134 0, /* rightshift */
135 2, /* size (0 = byte, 1 = short, 2 = long) */
136 24, /* bitsize */
137 false, /* pc_relative */
138 0, /* bitpos */
139 complain_overflow_unsigned, /* complain_on_overflow */
140 m32r_elf_generic_reloc,/* special_function */
141 "R_M32R_24", /* name */
142 true, /* partial_inplace */
143 0xffffff, /* src_mask */
144 0xffffff, /* dst_mask */
145 false), /* pcrel_offset */
147 /* An PC Relative 10-bit relocation, shifted by 2.
148 This reloc is complicated because relocations are relative to pc & -4.
149 i.e. branches in the right insn slot use the address of the left insn
150 slot for pc. */
151 /* ??? It's not clear whether this should have partial_inplace set or not.
152 Branch relaxing in the assembler can store the addend in the insn,
153 and if bfd_install_relocation gets called the addend may get added
154 again. */
155 HOWTO (R_M32R_10_PCREL, /* type */
156 2, /* rightshift */
157 1, /* size (0 = byte, 1 = short, 2 = long) */
158 10, /* bitsize */
159 true, /* pc_relative */
160 0, /* bitpos */
161 complain_overflow_signed, /* complain_on_overflow */
162 m32r_elf_10_pcrel_reloc, /* special_function */
163 "R_M32R_10_PCREL", /* name */
164 false, /* partial_inplace */
165 0xff, /* src_mask */
166 0xff, /* dst_mask */
167 true), /* pcrel_offset */
169 /* A relative 18 bit relocation, right shifted by 2. */
170 HOWTO (R_M32R_18_PCREL, /* type */
171 2, /* rightshift */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
173 16, /* bitsize */
174 true, /* pc_relative */
175 0, /* bitpos */
176 complain_overflow_signed, /* complain_on_overflow */
177 bfd_elf_generic_reloc, /* special_function */
178 "R_M32R_18_PCREL", /* name */
179 false, /* partial_inplace */
180 0xffff, /* src_mask */
181 0xffff, /* dst_mask */
182 true), /* pcrel_offset */
184 /* A relative 26 bit relocation, right shifted by 2. */
185 /* ??? It's not clear whether this should have partial_inplace set or not.
186 Branch relaxing in the assembler can store the addend in the insn,
187 and if bfd_install_relocation gets called the addend may get added
188 again. */
189 HOWTO (R_M32R_26_PCREL, /* type */
190 2, /* rightshift */
191 2, /* size (0 = byte, 1 = short, 2 = long) */
192 26, /* bitsize */
193 true, /* pc_relative */
194 0, /* bitpos */
195 complain_overflow_signed, /* complain_on_overflow */
196 bfd_elf_generic_reloc, /* special_function */
197 "R_M32R_26_PCREL", /* name */
198 false, /* partial_inplace */
199 0xffffff, /* src_mask */
200 0xffffff, /* dst_mask */
201 true), /* pcrel_offset */
203 /* High 16 bits of address when lower 16 is or'd in. */
204 HOWTO (R_M32R_HI16_ULO, /* type */
205 16, /* rightshift */
206 2, /* size (0 = byte, 1 = short, 2 = long) */
207 16, /* bitsize */
208 false, /* pc_relative */
209 0, /* bitpos */
210 complain_overflow_dont, /* complain_on_overflow */
211 m32r_elf_hi16_reloc, /* special_function */
212 "R_M32R_HI16_ULO", /* name */
213 true, /* partial_inplace */
214 0x0000ffff, /* src_mask */
215 0x0000ffff, /* dst_mask */
216 false), /* pcrel_offset */
218 /* High 16 bits of address when lower 16 is added in. */
219 HOWTO (R_M32R_HI16_SLO, /* type */
220 16, /* rightshift */
221 2, /* size (0 = byte, 1 = short, 2 = long) */
222 16, /* bitsize */
223 false, /* pc_relative */
224 0, /* bitpos */
225 complain_overflow_dont, /* complain_on_overflow */
226 m32r_elf_hi16_reloc, /* special_function */
227 "R_M32R_HI16_SLO", /* name */
228 true, /* partial_inplace */
229 0x0000ffff, /* src_mask */
230 0x0000ffff, /* dst_mask */
231 false), /* pcrel_offset */
233 /* Lower 16 bits of address. */
234 HOWTO (R_M32R_LO16, /* type */
235 0, /* rightshift */
236 2, /* size (0 = byte, 1 = short, 2 = long) */
237 16, /* bitsize */
238 false, /* pc_relative */
239 0, /* bitpos */
240 complain_overflow_dont, /* complain_on_overflow */
241 m32r_elf_lo16_reloc, /* special_function */
242 "R_M32R_LO16", /* name */
243 true, /* partial_inplace */
244 0x0000ffff, /* src_mask */
245 0x0000ffff, /* dst_mask */
246 false), /* pcrel_offset */
248 /* Small data area 16 bits offset. */
249 HOWTO (R_M32R_SDA16, /* type */
250 0, /* rightshift */
251 2, /* size (0 = byte, 1 = short, 2 = long) */
252 16, /* bitsize */
253 false, /* pc_relative */
254 0, /* bitpos */
255 complain_overflow_signed, /* complain_on_overflow */
256 m32r_elf_sda16_reloc, /* special_function */
257 "R_M32R_SDA16", /* name */
258 true, /* partial_inplace */ /* FIXME: correct? */
259 0x0000ffff, /* src_mask */
260 0x0000ffff, /* dst_mask */
261 false), /* pcrel_offset */
263 /* GNU extension to record C++ vtable hierarchy */
264 HOWTO (R_M32R_GNU_VTINHERIT, /* type */
265 0, /* rightshift */
266 2, /* size (0 = byte, 1 = short, 2 = long) */
267 0, /* bitsize */
268 false, /* pc_relative */
269 0, /* bitpos */
270 complain_overflow_dont, /* complain_on_overflow */
271 NULL, /* special_function */
272 "R_M32R_GNU_VTINHERIT", /* name */
273 false, /* partial_inplace */
274 0, /* src_mask */
275 0, /* dst_mask */
276 false), /* pcrel_offset */
278 /* GNU extension to record C++ vtable member usage */
279 HOWTO (R_M32R_GNU_VTENTRY, /* type */
280 0, /* rightshift */
281 2, /* size (0 = byte, 1 = short, 2 = long) */
282 0, /* bitsize */
283 false, /* pc_relative */
284 0, /* bitpos */
285 complain_overflow_dont, /* complain_on_overflow */
286 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
287 "R_M32R_GNU_VTENTRY", /* name */
288 false, /* partial_inplace */
289 0, /* src_mask */
290 0, /* dst_mask */
291 false), /* pcrel_offset */
295 /* Handle the R_M32R_10_PCREL reloc. */
297 static bfd_reloc_status_type
298 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
299 input_section, output_bfd, error_message)
300 bfd * abfd;
301 arelent * reloc_entry;
302 asymbol * symbol;
303 PTR data;
304 asection * input_section;
305 bfd * output_bfd;
306 char ** error_message ATTRIBUTE_UNUSED;
308 /* This part is from bfd_elf_generic_reloc. */
309 if (output_bfd != (bfd *) NULL
310 && (symbol->flags & BSF_SECTION_SYM) == 0
311 && (! reloc_entry->howto->partial_inplace
312 || reloc_entry->addend == 0))
314 reloc_entry->address += input_section->output_offset;
315 return bfd_reloc_ok;
318 if (output_bfd != NULL)
320 /* FIXME: See bfd_perform_relocation. Is this right? */
321 return bfd_reloc_continue;
324 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
325 input_section,
326 data, reloc_entry->address,
327 symbol->section,
328 (symbol->value
329 + symbol->section->output_section->vma
330 + symbol->section->output_offset),
331 reloc_entry->addend);
334 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
336 static bfd_reloc_status_type
337 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
338 symbol_section, symbol_value, addend)
339 bfd *abfd;
340 reloc_howto_type *howto;
341 asection *input_section;
342 bfd_byte *data;
343 bfd_vma offset;
344 asection *symbol_section ATTRIBUTE_UNUSED;
345 bfd_vma symbol_value;
346 bfd_vma addend;
348 bfd_signed_vma relocation;
349 unsigned long x;
350 bfd_reloc_status_type status;
352 /* Sanity check the address (offset in section). */
353 if (offset > input_section->_cooked_size)
354 return bfd_reloc_outofrange;
356 relocation = symbol_value + addend;
357 /* Make it pc relative. */
358 relocation -= (input_section->output_section->vma
359 + input_section->output_offset);
360 /* These jumps mask off the lower two bits of the current address
361 before doing pcrel calculations. */
362 relocation -= (offset & -4L);
364 if (relocation < -0x200 || relocation > 0x1ff)
365 status = bfd_reloc_overflow;
366 else
367 status = bfd_reloc_ok;
369 x = bfd_get_16 (abfd, data + offset);
370 relocation >>= howto->rightshift;
371 relocation <<= howto->bitpos;
372 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
373 bfd_put_16 (abfd, x, data + offset);
375 return status;
378 /* Handle the R_M32R_HI16_[SU]LO relocs.
379 HI16_SLO is for the add3 and load/store with displacement instructions.
380 HI16_ULO is for the or3 instruction.
381 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
382 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
383 we must add one to the high 16 bytes (which will get subtracted off when
384 the low 16 bits are added).
385 These relocs have to be done in combination with an R_M32R_LO16 reloc
386 because there is a carry from the LO16 to the HI16. Here we just save
387 the information we need; we do the actual relocation when we see the LO16.
388 This code is copied from the elf32-mips.c. We also support an arbitrary
389 number of HI16 relocs to be associated with a single LO16 reloc. The
390 assembler sorts the relocs to ensure each HI16 immediately precedes its
391 LO16. However if there are multiple copies, the assembler may not find
392 the real LO16 so it picks the first one it finds. */
394 struct m32r_hi16
396 struct m32r_hi16 *next;
397 bfd_byte *addr;
398 bfd_vma addend;
401 /* FIXME: This should not be a static variable. */
403 static struct m32r_hi16 *m32r_hi16_list;
405 static bfd_reloc_status_type
406 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
407 input_section, output_bfd, error_message)
408 bfd *abfd ATTRIBUTE_UNUSED;
409 arelent *reloc_entry;
410 asymbol *symbol;
411 PTR data;
412 asection *input_section;
413 bfd *output_bfd;
414 char **error_message ATTRIBUTE_UNUSED;
416 bfd_reloc_status_type ret;
417 bfd_vma relocation;
418 struct m32r_hi16 *n;
420 /* This part is from bfd_elf_generic_reloc.
421 If we're relocating, and this an external symbol, we don't want
422 to change anything. */
423 if (output_bfd != (bfd *) NULL
424 && (symbol->flags & BSF_SECTION_SYM) == 0
425 && reloc_entry->addend == 0)
427 reloc_entry->address += input_section->output_offset;
428 return bfd_reloc_ok;
431 /* Sanity check the address (offset in section). */
432 if (reloc_entry->address > input_section->_cooked_size)
433 return bfd_reloc_outofrange;
435 ret = bfd_reloc_ok;
436 if (bfd_is_und_section (symbol->section)
437 && output_bfd == (bfd *) NULL)
438 ret = bfd_reloc_undefined;
440 if (bfd_is_com_section (symbol->section))
441 relocation = 0;
442 else
443 relocation = symbol->value;
445 relocation += symbol->section->output_section->vma;
446 relocation += symbol->section->output_offset;
447 relocation += reloc_entry->addend;
449 /* Save the information, and let LO16 do the actual relocation. */
450 n = (struct m32r_hi16 *) bfd_malloc (sizeof *n);
451 if (n == NULL)
452 return bfd_reloc_outofrange;
453 n->addr = (bfd_byte *) data + reloc_entry->address;
454 n->addend = relocation;
455 n->next = m32r_hi16_list;
456 m32r_hi16_list = n;
458 if (output_bfd != (bfd *) NULL)
459 reloc_entry->address += input_section->output_offset;
461 return ret;
464 /* Handle an M32R ELF HI16 reloc. */
466 static void
467 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
468 bfd *input_bfd;
469 int type;
470 Elf_Internal_Rela *relhi;
471 Elf_Internal_Rela *rello;
472 bfd_byte *contents;
473 bfd_vma addend;
475 unsigned long insn;
476 bfd_vma addlo;
478 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
480 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
481 if (type == R_M32R_HI16_SLO)
482 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
483 else
484 addlo &= 0xffff;
486 addend += ((insn & 0xffff) << 16) + addlo;
488 /* Reaccount for sign extension of low part. */
489 if (type == R_M32R_HI16_SLO
490 && (addend & 0x8000) != 0)
491 addend += 0x10000;
493 bfd_put_32 (input_bfd,
494 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
495 contents + relhi->r_offset);
498 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
499 inplace relocation; this function exists in order to do the
500 R_M32R_HI16_[SU]LO relocation described above. */
502 bfd_reloc_status_type
503 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
504 input_section, output_bfd, error_message)
505 bfd *input_bfd;
506 arelent *reloc_entry;
507 asymbol *symbol;
508 PTR data;
509 asection *input_section;
510 bfd *output_bfd;
511 char **error_message;
513 /* This part is from bfd_elf_generic_reloc.
514 If we're relocating, and this an external symbol, we don't want
515 to change anything. */
516 if (output_bfd != (bfd *) NULL
517 && (symbol->flags & BSF_SECTION_SYM) == 0
518 && reloc_entry->addend == 0)
520 reloc_entry->address += input_section->output_offset;
521 return bfd_reloc_ok;
524 if (m32r_hi16_list != NULL)
526 struct m32r_hi16 *l;
528 l = m32r_hi16_list;
529 while (l != NULL)
531 unsigned long insn;
532 unsigned long val;
533 unsigned long vallo;
534 struct m32r_hi16 *next;
536 /* Do the HI16 relocation. Note that we actually don't need
537 to know anything about the LO16 itself, except where to
538 find the low 16 bits of the addend needed by the LO16. */
539 insn = bfd_get_32 (input_bfd, l->addr);
540 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
541 & 0xffff) ^ 0x8000) - 0x8000;
542 val = ((insn & 0xffff) << 16) + vallo;
543 val += l->addend;
545 /* Reaccount for sign extension of low part. */
546 if ((val & 0x8000) != 0)
547 val += 0x10000;
549 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
550 bfd_put_32 (input_bfd, insn, l->addr);
552 next = l->next;
553 free (l);
554 l = next;
557 m32r_hi16_list = NULL;
560 /* Now do the LO16 reloc in the usual way.
561 ??? It would be nice to call bfd_elf_generic_reloc here,
562 but we have partial_inplace == TRUE. bfd_elf_generic_reloc will
563 pass the handling back to bfd_install_relocation which will install
564 a section relative addend which is wrong. */
565 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
566 input_section, output_bfd, error_message);
569 /* Do generic partial_inplace relocation.
570 This is a local replacement for bfd_elf_generic_reloc. */
572 bfd_reloc_status_type
573 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
574 input_section, output_bfd, error_message)
575 bfd *input_bfd;
576 arelent *reloc_entry;
577 asymbol *symbol;
578 PTR data;
579 asection *input_section;
580 bfd *output_bfd;
581 char **error_message ATTRIBUTE_UNUSED;
583 bfd_reloc_status_type ret;
584 bfd_vma relocation;
585 bfd_byte *inplace_address;
587 /* This part is from bfd_elf_generic_reloc.
588 If we're relocating, and this an external symbol, we don't want
589 to change anything. */
590 if (output_bfd != (bfd *) NULL
591 && (symbol->flags & BSF_SECTION_SYM) == 0
592 && reloc_entry->addend == 0)
594 reloc_entry->address += input_section->output_offset;
595 return bfd_reloc_ok;
598 /* Now do the the reloc in the usual way.
599 ??? It would be nice to call bfd_elf_generic_reloc here,
600 but we have partial_inplace == TRUE. bfd_elf_generic_reloc will
601 pass the handling back to bfd_install_relocation which will install
602 a section relative addend which is wrong. */
604 /* Sanity check the address (offset in section). */
605 if (reloc_entry->address > input_section->_cooked_size)
606 return bfd_reloc_outofrange;
608 ret = bfd_reloc_ok;
609 if (bfd_is_und_section (symbol->section)
610 && output_bfd == (bfd *) NULL)
611 ret = bfd_reloc_undefined;
613 if (bfd_is_com_section (symbol->section)
614 || output_bfd != (bfd *) NULL)
615 relocation = 0;
616 else
617 relocation = symbol->value;
619 /* Only do this for a final link. */
620 if (output_bfd == (bfd *) NULL)
622 relocation += symbol->section->output_section->vma;
623 relocation += symbol->section->output_offset;
626 relocation += reloc_entry->addend;
627 inplace_address = (bfd_byte *) data + reloc_entry->address;
629 #define DOIT(x) \
630 x = ( (x & ~reloc_entry->howto->dst_mask) | \
631 (((x & reloc_entry->howto->src_mask) + relocation) & \
632 reloc_entry->howto->dst_mask))
634 switch (reloc_entry->howto->size)
636 case 1:
638 short x = bfd_get_16 (input_bfd, inplace_address);
639 DOIT (x);
640 bfd_put_16 (input_bfd, x, inplace_address);
642 break;
643 case 2:
645 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
646 DOIT (x);
647 bfd_put_32 (input_bfd, x, inplace_address);
649 break;
650 default:
651 BFD_ASSERT (0);
654 if (output_bfd != (bfd *) NULL)
655 reloc_entry->address += input_section->output_offset;
657 return ret;
660 /* Handle the R_M32R_SDA16 reloc.
661 This reloc is used to compute the address of objects in the small data area
662 and to perform loads and stores from that area.
663 The lower 16 bits are sign extended and added to the register specified
664 in the instruction, which is assumed to point to _SDA_BASE_. */
666 static bfd_reloc_status_type
667 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
668 input_section, output_bfd, error_message)
669 bfd *abfd ATTRIBUTE_UNUSED;
670 arelent *reloc_entry;
671 asymbol *symbol;
672 PTR data ATTRIBUTE_UNUSED;
673 asection *input_section;
674 bfd *output_bfd;
675 char **error_message ATTRIBUTE_UNUSED;
677 /* This part is from bfd_elf_generic_reloc. */
678 if (output_bfd != (bfd *) NULL
679 && (symbol->flags & BSF_SECTION_SYM) == 0
680 && (! reloc_entry->howto->partial_inplace
681 || reloc_entry->addend == 0))
683 reloc_entry->address += input_section->output_offset;
684 return bfd_reloc_ok;
687 if (output_bfd != NULL)
689 /* FIXME: See bfd_perform_relocation. Is this right? */
690 return bfd_reloc_continue;
693 /* FIXME: not sure what to do here yet. But then again, the linker
694 may never call us. */
695 abort ();
698 /* Map BFD reloc types to M32R ELF reloc types. */
700 struct m32r_reloc_map
702 bfd_reloc_code_real_type bfd_reloc_val;
703 unsigned char elf_reloc_val;
706 static const struct m32r_reloc_map m32r_reloc_map[] =
708 { BFD_RELOC_NONE, R_M32R_NONE },
709 { BFD_RELOC_16, R_M32R_16 },
710 { BFD_RELOC_32, R_M32R_32 },
711 { BFD_RELOC_M32R_24, R_M32R_24 },
712 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
713 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
714 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
715 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
716 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
717 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
718 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
719 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
720 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
723 static reloc_howto_type *
724 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
725 bfd *abfd ATTRIBUTE_UNUSED;
726 bfd_reloc_code_real_type code;
728 unsigned int i;
730 for (i = 0;
731 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
732 i++)
734 if (m32r_reloc_map[i].bfd_reloc_val == code)
735 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
738 return NULL;
741 /* Set the howto pointer for an M32R ELF reloc. */
743 static void
744 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
745 bfd *abfd ATTRIBUTE_UNUSED;
746 arelent *cache_ptr;
747 Elf32_Internal_Rel *dst;
749 unsigned int r_type;
751 r_type = ELF32_R_TYPE (dst->r_info);
752 BFD_ASSERT (r_type < (unsigned int) R_M32R_max);
753 cache_ptr->howto = &m32r_elf_howto_table[r_type];
756 /* Given a BFD section, try to locate the corresponding ELF section
757 index. */
759 boolean
760 _bfd_m32r_elf_section_from_bfd_section (abfd, hdr, sec, retval)
761 bfd *abfd ATTRIBUTE_UNUSED;
762 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
763 asection *sec;
764 int *retval;
766 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
768 *retval = SHN_M32R_SCOMMON;
769 return true;
771 return false;
774 /* M32R ELF uses two common sections. One is the usual one, and the other
775 is for small objects. All the small objects are kept together, and then
776 referenced via one register, which yields faster assembler code. It is
777 up to the compiler to emit an instruction to load the register with
778 _SDA_BASE. This is what we use for the small common section. This
779 approach is copied from elf32-mips.c. */
780 static asection m32r_elf_scom_section;
781 static asymbol m32r_elf_scom_symbol;
782 static asymbol *m32r_elf_scom_symbol_ptr;
784 /* Handle the special M32R section numbers that a symbol may use. */
786 void
787 _bfd_m32r_elf_symbol_processing (abfd, asym)
788 bfd *abfd ATTRIBUTE_UNUSED;
789 asymbol *asym;
791 elf_symbol_type *elfsym;
793 elfsym = (elf_symbol_type *) asym;
795 switch (elfsym->internal_elf_sym.st_shndx)
797 case SHN_M32R_SCOMMON:
798 if (m32r_elf_scom_section.name == NULL)
800 /* Initialize the small common section. */
801 m32r_elf_scom_section.name = ".scommon";
802 m32r_elf_scom_section.flags = SEC_IS_COMMON;
803 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
804 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
805 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
806 m32r_elf_scom_symbol.name = ".scommon";
807 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
808 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
809 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
811 asym->section = &m32r_elf_scom_section;
812 asym->value = elfsym->internal_elf_sym.st_size;
813 break;
817 /* Hook called by the linker routine which adds symbols from an object
818 file. We must handle the special M32R section numbers here.
819 We also keep watching for whether we need to create the sdata special
820 linker sections. */
822 static boolean
823 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
824 bfd *abfd;
825 struct bfd_link_info *info;
826 const Elf_Internal_Sym *sym;
827 const char **namep;
828 flagword *flagsp ATTRIBUTE_UNUSED;
829 asection **secp;
830 bfd_vma *valp;
832 if (! info->relocateable
833 && (*namep)[0] == '_' && (*namep)[1] == 'S'
834 && strcmp (*namep, "_SDA_BASE_") == 0)
836 /* This is simpler than using _bfd_elf_create_linker_section
837 (our needs are simpler than ppc's needs). Also
838 _bfd_elf_create_linker_section currently has a bug where if a .sdata
839 section already exists a new one is created that follows it which
840 screws of _SDA_BASE_ address calcs because output_offset != 0. */
841 struct elf_link_hash_entry *h;
842 asection *s = bfd_get_section_by_name (abfd, ".sdata");
844 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
846 if (s == NULL)
848 int flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
849 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
851 s = bfd_make_section_anyway (abfd, ".sdata");
852 if (s == NULL)
853 return false;
854 bfd_set_section_flags (abfd, s, flags);
855 bfd_set_section_alignment (abfd, s, 2);
858 h = (struct elf_link_hash_entry *)
859 bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, false);
861 if ((h == NULL || h->root.type == bfd_link_hash_undefined)
862 && !(_bfd_generic_link_add_one_symbol (info,
863 abfd,
864 "_SDA_BASE_",
865 BSF_GLOBAL,
867 32768,
868 (const char *) NULL,
869 false,
870 get_elf_backend_data (abfd)->collect,
871 (struct bfd_link_hash_entry **) &h)))
872 return false;
873 h->type = STT_OBJECT;
876 switch (sym->st_shndx)
878 case SHN_M32R_SCOMMON:
879 *secp = bfd_make_section_old_way (abfd, ".scommon");
880 (*secp)->flags |= SEC_IS_COMMON;
881 *valp = sym->st_size;
882 break;
885 return true;
888 /* We have to figure out the SDA_BASE value, so that we can adjust the
889 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
890 BFD. If we can't find it, we're stuck. We cache it in the ELF
891 target data. We don't need to adjust the symbol value for an
892 external symbol if we are producing relocateable output. */
894 static bfd_reloc_status_type
895 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
896 bfd *output_bfd;
897 struct bfd_link_info *info;
898 const char **error_message;
899 bfd_vma *psb;
901 if (elf_gp (output_bfd) == 0)
903 struct bfd_link_hash_entry *h;
905 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
906 if (h != (struct bfd_link_hash_entry *) NULL
907 && h->type == bfd_link_hash_defined)
908 elf_gp (output_bfd) = (h->u.def.value
909 + h->u.def.section->output_section->vma
910 + h->u.def.section->output_offset);
911 else
913 /* Only get the error once. */
914 *psb = elf_gp (output_bfd) = 4;
915 *error_message =
916 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
917 return bfd_reloc_dangerous;
920 *psb = elf_gp (output_bfd);
921 return bfd_reloc_ok;
924 /* Relocate an M32R/D ELF section.
925 There is some attempt to make this function usable for many architectures,
926 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
927 if only to serve as a learning tool.
929 The RELOCATE_SECTION function is called by the new ELF backend linker
930 to handle the relocations for a section.
932 The relocs are always passed as Rela structures; if the section
933 actually uses Rel structures, the r_addend field will always be
934 zero.
936 This function is responsible for adjust the section contents as
937 necessary, and (if using Rela relocs and generating a
938 relocateable output file) adjusting the reloc addend as
939 necessary.
941 This function does not have to worry about setting the reloc
942 address or the reloc symbol index.
944 LOCAL_SYMS is a pointer to the swapped in local symbols.
946 LOCAL_SECTIONS is an array giving the section in the input file
947 corresponding to the st_shndx field of each local symbol.
949 The global hash table entry for the global symbols can be found
950 via elf_sym_hashes (input_bfd).
952 When generating relocateable output, this function must handle
953 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
954 going to be the section symbol corresponding to the output
955 section, which means that the addend must be adjusted
956 accordingly. */
958 static boolean
959 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
960 contents, relocs, local_syms, local_sections)
961 bfd *output_bfd ATTRIBUTE_UNUSED;
962 struct bfd_link_info *info;
963 bfd *input_bfd;
964 asection *input_section;
965 bfd_byte *contents;
966 Elf_Internal_Rela *relocs;
967 Elf_Internal_Sym *local_syms;
968 asection **local_sections;
970 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
971 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
972 Elf_Internal_Rela *rel, *relend;
973 /* Assume success. */
974 boolean ret = true;
976 rel = relocs;
977 relend = relocs + input_section->reloc_count;
978 for (; rel < relend; rel++)
980 int r_type;
981 reloc_howto_type *howto;
982 unsigned long r_symndx;
983 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
984 ensure it's zero (we use REL relocs, not RELA). Therefore this
985 should be assigning zero to `addend', but for clarity we use
986 `r_addend'. */
987 bfd_vma addend = rel->r_addend;
988 bfd_vma offset = rel->r_offset;
989 struct elf_link_hash_entry *h;
990 Elf_Internal_Sym *sym;
991 asection *sec;
992 const char *sym_name;
993 bfd_reloc_status_type r;
994 const char *errmsg = NULL;
996 h = NULL;
997 r_type = ELF32_R_TYPE (rel->r_info);
998 if (r_type < 0 || r_type >= (int) R_M32R_max)
1000 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
1001 bfd_get_filename (input_bfd),
1002 (int) r_type);
1003 bfd_set_error (bfd_error_bad_value);
1004 ret = false;
1005 continue;
1008 if (r_type == R_M32R_GNU_VTENTRY
1009 || r_type == R_M32R_GNU_VTINHERIT)
1010 continue;
1012 howto = m32r_elf_howto_table + r_type;
1013 r_symndx = ELF32_R_SYM (rel->r_info);
1015 if (info->relocateable)
1017 /* This is a relocateable link. We don't have to change
1018 anything, unless the reloc is against a section symbol,
1019 in which case we have to adjust according to where the
1020 section symbol winds up in the output section. */
1021 sec = NULL;
1022 if (r_symndx >= symtab_hdr->sh_info)
1024 /* External symbol. */
1025 continue;
1028 /* Local symbol. */
1029 sym = local_syms + r_symndx;
1030 sym_name = "<local symbol>";
1031 /* STT_SECTION: symbol is associated with a section. */
1032 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
1034 /* Symbol isn't associated with a section. Nothing to do. */
1035 continue;
1038 sec = local_sections[r_symndx];
1039 addend += sec->output_offset + sym->st_value;
1040 #ifndef USE_REL
1041 /* This can't be done for USE_REL because it doesn't mean anything
1042 and elf_link_input_bfd asserts this stays zero. */
1043 rel->r_addend = addend;
1044 #endif
1046 #ifndef USE_REL
1047 /* Addends are stored with relocs. We're done. */
1048 continue;
1049 #else /* USE_REL */
1050 /* If partial_inplace, we need to store any additional addend
1051 back in the section. */
1052 if (! howto->partial_inplace)
1053 continue;
1054 /* ??? Here is a nice place to call a special_function
1055 like handler. */
1056 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
1057 r = _bfd_relocate_contents (howto, input_bfd,
1058 addend, contents + offset);
1059 else
1061 Elf_Internal_Rela *lorel;
1063 /* We allow an arbitrary number of HI16 relocs before the
1064 LO16 reloc. This permits gcc to emit the HI and LO relocs
1065 itself. */
1066 for (lorel = rel + 1;
1067 (lorel < relend
1068 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
1069 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
1070 lorel++)
1071 continue;
1072 if (lorel < relend
1073 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
1075 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
1076 contents, addend);
1077 r = bfd_reloc_ok;
1079 else
1080 r = _bfd_relocate_contents (howto, input_bfd,
1081 addend, contents + offset);
1083 #endif /* USE_REL */
1085 else
1087 bfd_vma relocation;
1089 /* This is a final link. */
1090 sym = NULL;
1091 sec = NULL;
1093 if (r_symndx < symtab_hdr->sh_info)
1095 /* Local symbol. */
1096 sym = local_syms + r_symndx;
1097 sec = local_sections[r_symndx];
1098 sym_name = "<local symbol>";
1099 relocation = (sec->output_section->vma
1100 + sec->output_offset
1101 + sym->st_value);
1103 else
1105 /* External symbol. */
1106 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1107 while (h->root.type == bfd_link_hash_indirect
1108 || h->root.type == bfd_link_hash_warning)
1109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1110 sym_name = h->root.root.string;
1112 if (h->root.type == bfd_link_hash_defined
1113 || h->root.type == bfd_link_hash_defweak)
1115 sec = h->root.u.def.section;
1116 if (sec->output_section == NULL)
1117 relocation = 0;
1118 else
1119 relocation = (h->root.u.def.value
1120 + sec->output_section->vma
1121 + sec->output_offset);
1123 else if (h->root.type == bfd_link_hash_undefweak)
1124 relocation = 0;
1125 else
1127 if (! ((*info->callbacks->undefined_symbol)
1128 (info, h->root.root.string, input_bfd,
1129 input_section, offset, true)))
1130 return false;
1131 relocation = 0;
1135 /* Sanity check the address. */
1136 if (offset > input_section->_raw_size)
1138 r = bfd_reloc_outofrange;
1139 goto check_reloc;
1142 switch ((int) r_type)
1144 case (int) R_M32R_10_PCREL :
1145 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
1146 contents, offset,
1147 sec, relocation, addend);
1148 break;
1150 case (int) R_M32R_HI16_SLO :
1151 case (int) R_M32R_HI16_ULO :
1153 Elf_Internal_Rela *lorel;
1155 /* We allow an arbitrary number of HI16 relocs before the
1156 LO16 reloc. This permits gcc to emit the HI and LO relocs
1157 itself. */
1158 for (lorel = rel + 1;
1159 (lorel < relend
1160 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
1161 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
1162 lorel++)
1163 continue;
1164 if (lorel < relend
1165 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
1167 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
1168 contents, relocation + addend);
1169 r = bfd_reloc_ok;
1171 else
1172 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1173 contents, offset,
1174 relocation, addend);
1176 break;
1178 case (int) R_M32R_SDA16 :
1180 const char *name;
1182 BFD_ASSERT (sec != NULL);
1183 name = bfd_get_section_name (abfd, sec);
1185 if (strcmp (name, ".sdata") == 0
1186 || strcmp (name, ".sbss") == 0
1187 || strcmp (name, ".scommon") == 0)
1189 bfd_vma sda_base;
1190 bfd *out_bfd = sec->output_section->owner;
1192 r = m32r_elf_final_sda_base (out_bfd, info,
1193 &errmsg,
1194 &sda_base);
1195 if (r != bfd_reloc_ok)
1197 ret = false;
1198 goto check_reloc;
1201 /* At this point `relocation' contains the object's
1202 address. */
1203 relocation -= sda_base;
1204 /* Now it contains the offset from _SDA_BASE_. */
1206 else
1208 (*_bfd_error_handler) (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
1209 bfd_get_filename (input_bfd),
1210 sym_name,
1211 m32r_elf_howto_table[(int) r_type].name,
1212 bfd_get_section_name (abfd, sec));
1213 /*bfd_set_error (bfd_error_bad_value); ??? why? */
1214 ret = false;
1215 continue;
1218 /* fall through */
1220 default :
1221 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1222 contents, offset,
1223 relocation, addend);
1224 break;
1228 check_reloc:
1230 if (r != bfd_reloc_ok)
1232 /* FIXME: This should be generic enough to go in a utility. */
1233 const char *name;
1235 if (h != NULL)
1236 name = h->root.root.string;
1237 else
1239 name = (bfd_elf_string_from_elf_section
1240 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1241 if (name == NULL || *name == '\0')
1242 name = bfd_section_name (input_bfd, sec);
1245 if (errmsg != NULL)
1246 goto common_error;
1248 switch (r)
1250 case bfd_reloc_overflow:
1251 if (! ((*info->callbacks->reloc_overflow)
1252 (info, name, howto->name, (bfd_vma) 0,
1253 input_bfd, input_section, offset)))
1254 return false;
1255 break;
1257 case bfd_reloc_undefined:
1258 if (! ((*info->callbacks->undefined_symbol)
1259 (info, name, input_bfd, input_section,
1260 offset, true)))
1261 return false;
1262 break;
1264 case bfd_reloc_outofrange:
1265 errmsg = _("internal error: out of range error");
1266 goto common_error;
1268 case bfd_reloc_notsupported:
1269 errmsg = _("internal error: unsupported relocation error");
1270 goto common_error;
1272 case bfd_reloc_dangerous:
1273 errmsg = _("internal error: dangerous error");
1274 goto common_error;
1276 default:
1277 errmsg = _("internal error: unknown error");
1278 /* fall through */
1280 common_error:
1281 if (!((*info->callbacks->warning)
1282 (info, errmsg, name, input_bfd, input_section,
1283 offset)))
1284 return false;
1285 break;
1290 return ret;
1293 #if 0 /* relaxing not supported yet */
1295 /* This function handles relaxing for the m32r.
1296 Relaxing on the m32r is tricky because of instruction alignment
1297 requirements (4 byte instructions must be aligned on 4 byte boundaries).
1299 The following relaxing opportunities are handled:
1301 seth/add3/jl -> bl24 or bl8
1302 seth/add3 -> ld24
1304 It would be nice to handle bl24 -> bl8 but given:
1306 - 4 byte insns must be on 4 byte boundaries
1307 - branch instructions only branch to insns on 4 byte boundaries
1309 this isn't much of a win because the insn in the 2 "deleted" bytes
1310 must become a nop. With some complexity some real relaxation could be
1311 done but the frequency just wouldn't make it worth it; it's better to
1312 try to do all the code compaction one can elsewhere.
1313 When the chip supports parallel 16 bit insns, things may change.
1316 static boolean
1317 m32r_elf_relax_section (abfd, sec, link_info, again)
1318 bfd *abfd;
1319 asection *sec;
1320 struct bfd_link_info *link_info;
1321 boolean *again;
1323 Elf_Internal_Shdr *symtab_hdr;
1324 /* The Rela structures are used here because that's what
1325 _bfd_elf32_link_read_relocs uses [for convenience - it sets the addend
1326 field to 0]. */
1327 Elf_Internal_Rela *internal_relocs;
1328 Elf_Internal_Rela *free_relocs = NULL;
1329 Elf_Internal_Rela *irel, *irelend;
1330 bfd_byte *contents = NULL;
1331 bfd_byte *free_contents = NULL;
1332 Elf32_External_Sym *extsyms = NULL;
1333 Elf32_External_Sym *free_extsyms = NULL;
1335 /* Assume nothing changes. */
1336 *again = false;
1338 /* We don't have to do anything for a relocateable link, if
1339 this section does not have relocs, or if this is not a
1340 code section. */
1341 if (link_info->relocateable
1342 || (sec->flags & SEC_RELOC) == 0
1343 || sec->reloc_count == 0
1344 || (sec->flags & SEC_CODE) == 0
1345 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
1346 return true;
1348 /* If this is the first time we have been called for this section,
1349 initialize the cooked size. */
1350 if (sec->_cooked_size == 0)
1351 sec->_cooked_size = sec->_raw_size;
1353 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1355 /* Get a copy of the native relocations. */
1356 internal_relocs = (_bfd_elf32_link_read_relocs
1357 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1358 link_info->keep_memory));
1359 if (internal_relocs == NULL)
1360 goto error_return;
1361 if (! link_info->keep_memory)
1362 free_relocs = internal_relocs;
1364 /* Walk through them looking for relaxing opportunities. */
1365 irelend = internal_relocs + sec->reloc_count;
1366 for (irel = internal_relocs; irel < irelend; irel++)
1368 bfd_vma symval;
1370 /* If this isn't something that can be relaxed, then ignore
1371 this reloc. */
1372 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
1373 continue;
1375 /* Get the section contents if we haven't done so already. */
1376 if (contents == NULL)
1378 /* Get cached copy if it exists. */
1379 if (elf_section_data (sec)->this_hdr.contents != NULL)
1380 contents = elf_section_data (sec)->this_hdr.contents;
1381 else
1383 /* Go get them off disk. */
1384 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1385 if (contents == NULL)
1386 goto error_return;
1387 free_contents = contents;
1389 if (! bfd_get_section_contents (abfd, sec, contents,
1390 (file_ptr) 0, sec->_raw_size))
1391 goto error_return;
1395 /* Read this BFD's symbols if we haven't done so already. */
1396 if (extsyms == NULL)
1398 /* Get cached copy if it exists. */
1399 if (symtab_hdr->contents != NULL)
1400 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1401 else
1403 /* Go get them off disk. */
1404 extsyms = ((Elf32_External_Sym *)
1405 bfd_malloc (symtab_hdr->sh_size));
1406 if (extsyms == NULL)
1407 goto error_return;
1408 free_extsyms = extsyms;
1409 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1410 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1411 != symtab_hdr->sh_size))
1412 goto error_return;
1416 /* Get the value of the symbol referred to by the reloc. */
1417 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1419 Elf_Internal_Sym isym;
1420 asection *sym_sec;
1422 /* A local symbol. */
1423 bfd_elf32_swap_symbol_in (abfd,
1424 extsyms + ELF32_R_SYM (irel->r_info),
1425 &isym);
1427 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1428 symval = (isym.st_value
1429 + sym_sec->output_section->vma
1430 + sym_sec->output_offset);
1432 else
1434 unsigned long indx;
1435 struct elf_link_hash_entry *h;
1437 /* An external symbol. */
1438 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1439 h = elf_sym_hashes (abfd)[indx];
1440 BFD_ASSERT (h != NULL);
1441 if (h->root.type != bfd_link_hash_defined
1442 && h->root.type != bfd_link_hash_defweak)
1444 /* This appears to be a reference to an undefined
1445 symbol. Just ignore it--it will be caught by the
1446 regular reloc processing. */
1447 continue;
1450 symval = (h->root.u.def.value
1451 + h->root.u.def.section->output_section->vma
1452 + h->root.u.def.section->output_offset);
1455 /* For simplicity of coding, we are going to modify the section
1456 contents, the section relocs, and the BFD symbol table. We
1457 must tell the rest of the code not to free up this
1458 information. It would be possible to instead create a table
1459 of changes which have to be made, as is done in coff-mips.c;
1460 that would be more work, but would require less memory when
1461 the linker is run. */
1463 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
1464 This sequence is generated by the compiler when compiling in
1465 32 bit mode. Also look for seth/add3 -> ld24. */
1467 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
1469 Elf_Internal_Rela *nrel;
1470 bfd_vma pc = (sec->output_section->vma + sec->output_offset
1471 + irel->r_offset);
1472 bfd_signed_vma pcrel_value = symval - pc;
1473 unsigned int code,reg;
1474 int addend,nop_p,bl8_p,to_delete;
1476 /* The tests are ordered so that we get out as quickly as possible
1477 if this isn't something we can relax, taking into account that
1478 we are looking for two separate possibilities (jl/ld24). */
1480 /* Do nothing if no room in the section for this to be what we're
1481 looking for. */
1482 if (irel->r_offset > sec->_cooked_size - 8)
1483 continue;
1485 /* Make sure the next relocation applies to the next
1486 instruction and that it's the add3's reloc. */
1487 nrel = irel + 1;
1488 if (nrel == irelend
1489 || irel->r_offset + 4 != nrel->r_offset
1490 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
1491 continue;
1493 /* See if the instructions are seth/add3. */
1494 /* FIXME: This is where macros from cgen can come in. */
1495 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
1496 if ((code & 0xf0ff) != 0xd0c0)
1497 continue; /* not seth rN,foo */
1498 reg = (code & 0x0f00) >> 8;
1499 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
1500 if (code != (0x80a0 | reg | (reg << 8)))
1501 continue; /* not add3 rN,rN,foo */
1503 /* At this point we've confirmed we have seth/add3. Now check
1504 whether the next insn is a jl, in which case try to change this
1505 to bl24 or bl8. */
1507 /* Ensure the branch target is in range.
1508 The bl24 instruction has a 24 bit operand which is the target
1509 address right shifted by 2, giving a signed range of 26 bits.
1510 Note that 4 bytes are added to the high value because the target
1511 will be at least 4 bytes closer if we can relax. It'll actually
1512 be 4 or 8 bytes closer, but we don't know which just yet and
1513 the difference isn't significant enough to worry about. */
1514 #ifndef USE_REL /* put in for learning purposes */
1515 pcrel_value += irel->r_addend;
1516 #else
1517 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
1518 pcrel_value += addend;
1519 #endif
1521 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
1522 /* Do nothing if no room in the section for this to be what we're
1523 looking for. */
1524 && (irel->r_offset <= sec->_cooked_size - 12)
1525 /* Ensure the next insn is "jl rN". */
1526 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
1527 code != (0x1ec0 | reg)))
1529 /* We can relax to bl24/bl8. */
1531 /* See if there's a nop following the jl.
1532 Also see if we can use a bl8 insn. */
1533 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
1534 nop_p = (code & 0x7fff) == NOP_INSN;
1535 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
1537 if (bl8_p)
1539 /* Change "seth rN,foo" to "bl8 foo || nop".
1540 We OR in CODE just in case it's not a nop (technically,
1541 CODE currently must be a nop, but for cleanness we
1542 allow it to be anything). */
1543 #ifndef USE_REL /* put in for learning purposes */
1544 code = 0x7e000000 | MAKE_PARALLEL (code);
1545 #else
1546 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
1547 #endif
1548 to_delete = 8;
1550 else
1552 /* Change the seth rN,foo to a bl24 foo. */
1553 #ifndef USE_REL /* put in for learning purposes */
1554 code = 0xfe000000;
1555 #else
1556 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
1557 #endif
1558 to_delete = nop_p ? 8 : 4;
1561 bfd_put_32 (abfd, code, contents + irel->r_offset);
1563 /* Set the new reloc type. */
1564 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1565 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
1567 /* Delete the add3 reloc by making it a null reloc. */
1568 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1569 R_M32R_NONE);
1571 else if (addend >= 0
1572 && symval + addend <= 0xffffff)
1574 /* We can relax to ld24. */
1576 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
1577 bfd_put_32 (abfd, code, contents + irel->r_offset);
1578 to_delete = 4;
1579 /* Tell the following code a nop filler isn't needed. */
1580 nop_p = 1;
1582 else
1584 /* Can't do anything here. */
1585 continue;
1588 /* Note that we've changed the relocs, section contents, etc. */
1589 elf_section_data (sec)->relocs = internal_relocs;
1590 free_relocs = NULL;
1592 elf_section_data (sec)->this_hdr.contents = contents;
1593 free_contents = NULL;
1595 symtab_hdr->contents = (bfd_byte *) extsyms;
1596 free_extsyms = NULL;
1598 /* Delete TO_DELETE bytes of data. */
1599 if (!m32r_elf_relax_delete_bytes (abfd, sec,
1600 irel->r_offset + 4, to_delete))
1601 goto error_return;
1603 /* Now that the following bytes have been moved into place, see if
1604 we need to replace the jl with a nop. This happens when we had
1605 to use a bl24 insn and the insn following the jl isn't a nop.
1606 Technically, this situation can't happen (since the insn can
1607 never be executed) but to be clean we do this. When the chip
1608 supports parallel 16 bit insns things may change.
1609 We don't need to do this in the case of relaxing to ld24,
1610 and the above code sets nop_p so this isn't done. */
1611 if (! nop_p && to_delete == 4)
1612 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
1614 /* That will change things, so we should relax again.
1615 Note that this is not required, and it may be slow. */
1616 *again = true;
1618 continue;
1621 /* loop to try the next reloc */
1624 if (free_relocs != NULL)
1626 free (free_relocs);
1627 free_relocs = NULL;
1630 if (free_contents != NULL)
1632 if (! link_info->keep_memory)
1633 free (free_contents);
1634 else
1636 /* Cache the section contents for elf_link_input_bfd. */
1637 elf_section_data (sec)->this_hdr.contents = contents;
1639 free_contents = NULL;
1642 if (free_extsyms != NULL)
1644 if (! link_info->keep_memory)
1645 free (free_extsyms);
1646 else
1648 /* Cache the symbols for elf_link_input_bfd. */
1649 symtab_hdr->contents = extsyms;
1651 free_extsyms = NULL;
1654 return true;
1656 error_return:
1657 if (free_relocs != NULL)
1658 free (free_relocs);
1659 if (free_contents != NULL)
1660 free (free_contents);
1661 if (free_extsyms != NULL)
1662 free (free_extsyms);
1663 return false;
1666 /* Delete some bytes from a section while relaxing. */
1668 static boolean
1669 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
1670 bfd *abfd;
1671 asection *sec;
1672 bfd_vma addr;
1673 int count;
1675 Elf_Internal_Shdr *symtab_hdr;
1676 Elf32_External_Sym *extsyms;
1677 int shndx, index;
1678 bfd_byte *contents;
1679 Elf_Internal_Rela *irel, *irelend;
1680 Elf_Internal_Rela *irelalign;
1681 bfd_vma toaddr;
1682 Elf32_External_Sym *esym, *esymend;
1683 struct elf_link_hash_entry *sym_hash;
1685 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1686 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1688 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1690 contents = elf_section_data (sec)->this_hdr.contents;
1692 /* The deletion must stop at the next ALIGN reloc for an aligment
1693 power larger than the number of bytes we are deleting. */
1695 irelalign = NULL;
1696 toaddr = sec->_cooked_size;
1698 irel = elf_section_data (sec)->relocs;
1699 irelend = irel + sec->reloc_count;
1701 /* Actually delete the bytes. */
1702 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1703 sec->_cooked_size -= count;
1705 /* Adjust all the relocs. */
1706 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1708 /* Get the new reloc address. */
1709 if ((irel->r_offset > addr
1710 && irel->r_offset < toaddr))
1711 irel->r_offset -= count;
1714 /* Adjust the local symbols defined in this section. */
1715 esym = extsyms;
1716 esymend = esym + symtab_hdr->sh_info;
1717 for (; esym < esymend; esym++)
1719 Elf_Internal_Sym isym;
1721 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1723 if (isym.st_shndx == shndx
1724 && isym.st_value > addr
1725 && isym.st_value < toaddr)
1727 isym.st_value -= count;
1728 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1732 /* Now adjust the global symbols defined in this section. */
1733 esym = extsyms + symtab_hdr->sh_info;
1734 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1735 for (index = 0; esym < esymend; esym++, index++)
1737 Elf_Internal_Sym isym;
1739 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1740 sym_hash = elf_sym_hashes (abfd)[index];
1741 if (isym.st_shndx == shndx
1742 && ((sym_hash)->root.type == bfd_link_hash_defined
1743 || (sym_hash)->root.type == bfd_link_hash_defweak)
1744 && (sym_hash)->root.u.def.section == sec
1745 && (sym_hash)->root.u.def.value > addr
1746 && (sym_hash)->root.u.def.value < toaddr)
1748 (sym_hash)->root.u.def.value -= count;
1752 return true;
1755 /* This is a version of bfd_generic_get_relocated_section_contents
1756 which uses m32r_elf_relocate_section. */
1758 static bfd_byte *
1759 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1760 data, relocateable, symbols)
1761 bfd *output_bfd;
1762 struct bfd_link_info *link_info;
1763 struct bfd_link_order *link_order;
1764 bfd_byte *data;
1765 boolean relocateable;
1766 asymbol **symbols;
1768 Elf_Internal_Shdr *symtab_hdr;
1769 asection *input_section = link_order->u.indirect.section;
1770 bfd *input_bfd = input_section->owner;
1771 asection **sections = NULL;
1772 Elf_Internal_Rela *internal_relocs = NULL;
1773 Elf32_External_Sym *external_syms = NULL;
1774 Elf_Internal_Sym *internal_syms = NULL;
1776 /* We only need to handle the case of relaxing, or of having a
1777 particular set of section contents, specially. */
1778 if (relocateable
1779 || elf_section_data (input_section)->this_hdr.contents == NULL)
1780 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1781 link_order, data,
1782 relocateable,
1783 symbols);
1785 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1787 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1788 input_section->_raw_size);
1790 if ((input_section->flags & SEC_RELOC) != 0
1791 && input_section->reloc_count > 0)
1793 Elf_Internal_Sym *isymp;
1794 asection **secpp;
1795 Elf32_External_Sym *esym, *esymend;
1797 if (symtab_hdr->contents != NULL)
1798 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1799 else
1801 external_syms = ((Elf32_External_Sym *)
1802 bfd_malloc (symtab_hdr->sh_info
1803 * sizeof (Elf32_External_Sym)));
1804 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1805 goto error_return;
1806 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1807 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1808 symtab_hdr->sh_info, input_bfd)
1809 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1810 goto error_return;
1813 internal_relocs = (_bfd_elf32_link_read_relocs
1814 (input_bfd, input_section, (PTR) NULL,
1815 (Elf_Internal_Rela *) NULL, false));
1816 if (internal_relocs == NULL)
1817 goto error_return;
1819 internal_syms = ((Elf_Internal_Sym *)
1820 bfd_malloc (symtab_hdr->sh_info
1821 * sizeof (Elf_Internal_Sym)));
1822 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1823 goto error_return;
1825 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1826 * sizeof (asection *));
1827 if (sections == NULL && symtab_hdr->sh_info > 0)
1828 goto error_return;
1830 isymp = internal_syms;
1831 secpp = sections;
1832 esym = external_syms;
1833 esymend = esym + symtab_hdr->sh_info;
1834 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1836 asection *isec;
1838 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1840 if (isymp->st_shndx == SHN_UNDEF)
1841 isec = bfd_und_section_ptr;
1842 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1843 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1844 else if (isymp->st_shndx == SHN_ABS)
1845 isec = bfd_abs_section_ptr;
1846 else if (isymp->st_shndx == SHN_COMMON)
1847 isec = bfd_com_section_ptr;
1848 else if (isymp->st_shndx == SHN_M32R_SCOMMON)
1849 isec = &m32r_elf_scom_section;
1850 else
1852 /* Who knows? */
1853 isec = NULL;
1856 *secpp = isec;
1859 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
1860 input_section, data, internal_relocs,
1861 internal_syms, sections))
1862 goto error_return;
1864 if (sections != NULL)
1865 free (sections);
1866 sections = NULL;
1867 if (internal_syms != NULL)
1868 free (internal_syms);
1869 internal_syms = NULL;
1870 if (external_syms != NULL && symtab_hdr->contents == NULL)
1871 free (external_syms);
1872 external_syms = NULL;
1873 if (internal_relocs != elf_section_data (input_section)->relocs)
1874 free (internal_relocs);
1875 internal_relocs = NULL;
1878 return data;
1880 error_return:
1881 if (internal_relocs != NULL
1882 && internal_relocs != elf_section_data (input_section)->relocs)
1883 free (internal_relocs);
1884 if (external_syms != NULL && symtab_hdr->contents == NULL)
1885 free (external_syms);
1886 if (internal_syms != NULL)
1887 free (internal_syms);
1888 if (sections != NULL)
1889 free (sections);
1890 return NULL;
1893 #endif /* #if 0 */
1895 /* Set the right machine number. */
1896 static boolean
1897 m32r_elf_object_p (abfd)
1898 bfd *abfd;
1900 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
1902 default:
1903 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
1904 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
1906 return true;
1909 /* Store the machine number in the flags field. */
1910 static void
1911 m32r_elf_final_write_processing (abfd, linker)
1912 bfd * abfd;
1913 boolean linker ATTRIBUTE_UNUSED;
1915 unsigned long val;
1917 switch (bfd_get_mach (abfd))
1919 default:
1920 case bfd_mach_m32r: val = E_M32R_ARCH; break;
1921 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
1924 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
1925 elf_elfheader (abfd)->e_flags |= val;
1928 /* Function to keep M32R specific file flags. */
1929 static boolean
1930 m32r_elf_set_private_flags (abfd, flags)
1931 bfd * abfd;
1932 flagword flags;
1934 BFD_ASSERT (!elf_flags_init (abfd)
1935 || elf_elfheader (abfd)->e_flags == flags);
1937 elf_elfheader (abfd)->e_flags = flags;
1938 elf_flags_init (abfd) = true;
1939 return true;
1942 /* Copy backend specific data from one object module to another */
1943 static boolean
1944 m32r_elf_copy_private_bfd_data (ibfd, obfd)
1945 bfd * ibfd;
1946 bfd * obfd;
1948 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1949 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1950 return true;
1952 BFD_ASSERT (!elf_flags_init (obfd)
1953 || (elf_elfheader (obfd)->e_flags
1954 == elf_elfheader (ibfd)->e_flags));
1956 elf_gp (obfd) = elf_gp (ibfd);
1957 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1958 elf_flags_init (obfd) = true;
1959 return true;
1962 /* Merge backend specific data from an object file to the output
1963 object file when linking. */
1964 static boolean
1965 m32r_elf_merge_private_bfd_data (ibfd, obfd)
1966 bfd * ibfd;
1967 bfd * obfd;
1969 flagword out_flags;
1970 flagword in_flags;
1972 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1973 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1974 return true;
1976 in_flags = elf_elfheader (ibfd)->e_flags;
1977 out_flags = elf_elfheader (obfd)->e_flags;
1979 if (! elf_flags_init (obfd))
1981 /* If the input is the default architecture then do not
1982 bother setting the flags for the output architecture,
1983 instead allow future merges to do this. If no future
1984 merges ever set these flags then they will retain their
1985 unitialised values, which surprise surprise, correspond
1986 to the default values. */
1987 if (bfd_get_arch_info (ibfd)->the_default)
1988 return true;
1990 elf_flags_init (obfd) = true;
1991 elf_elfheader (obfd)->e_flags = in_flags;
1993 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1994 && bfd_get_arch_info (obfd)->the_default)
1996 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1999 return true;
2002 /* Check flag compatibility. */
2003 if (in_flags == out_flags)
2004 return true;
2006 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
2008 if ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
2010 _bfd_error_handler (_("%s: Instruction set mismatch with previous modules"),
2011 bfd_get_filename (ibfd));
2013 bfd_set_error (bfd_error_bad_value);
2014 return false;
2018 return true;
2021 /* Display the flags field */
2022 static boolean
2023 m32r_elf_print_private_bfd_data (abfd, ptr)
2024 bfd * abfd;
2025 PTR ptr;
2027 FILE * file = (FILE *) ptr;
2029 BFD_ASSERT (abfd != NULL && ptr != NULL)
2031 _bfd_elf_print_private_bfd_data (abfd, ptr);
2033 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
2035 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
2037 default:
2038 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
2039 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
2042 fputc ('\n', file);
2044 return true;
2047 asection *
2048 m32r_elf_gc_mark_hook (abfd, info, rel, h, sym)
2049 bfd *abfd;
2050 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2051 Elf_Internal_Rela *rel;
2052 struct elf_link_hash_entry *h;
2053 Elf_Internal_Sym *sym;
2055 if (h != NULL)
2057 switch (ELF32_R_TYPE (rel->r_info))
2059 case R_M32R_GNU_VTINHERIT:
2060 case R_M32R_GNU_VTENTRY:
2061 break;
2063 default:
2064 switch (h->root.type)
2066 case bfd_link_hash_defined:
2067 case bfd_link_hash_defweak:
2068 return h->root.u.def.section;
2070 case bfd_link_hash_common:
2071 return h->root.u.c.p->section;
2073 default:
2074 break;
2078 else
2080 if (!(elf_bad_symtab (abfd)
2081 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2082 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2083 && sym->st_shndx != SHN_COMMON))
2085 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2088 return NULL;
2091 static boolean
2092 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
2093 bfd *abfd ATTRIBUTE_UNUSED;
2094 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2095 asection *sec ATTRIBUTE_UNUSED;
2096 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2098 /* we don't use got and plt entries for m32r */
2099 return true;
2102 /* Look through the relocs for a section during the first phase.
2103 Since we don't do .gots or .plts, we just need to consider the
2104 virtual table relocs for gc. */
2106 static boolean
2107 m32r_elf_check_relocs (abfd, info, sec, relocs)
2108 bfd *abfd;
2109 struct bfd_link_info *info;
2110 asection *sec;
2111 const Elf_Internal_Rela *relocs;
2113 Elf_Internal_Shdr *symtab_hdr;
2114 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2115 const Elf_Internal_Rela *rel;
2116 const Elf_Internal_Rela *rel_end;
2118 if (info->relocateable)
2119 return true;
2121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2122 sym_hashes = elf_sym_hashes (abfd);
2123 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2124 if (!elf_bad_symtab (abfd))
2125 sym_hashes_end -= symtab_hdr->sh_info;
2127 rel_end = relocs + sec->reloc_count;
2128 for (rel = relocs; rel < rel_end; rel++)
2130 struct elf_link_hash_entry *h;
2131 unsigned long r_symndx;
2133 r_symndx = ELF32_R_SYM (rel->r_info);
2134 if (r_symndx < symtab_hdr->sh_info)
2135 h = NULL;
2136 else
2137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2139 switch (ELF32_R_TYPE (rel->r_info))
2141 /* This relocation describes the C++ object vtable hierarchy.
2142 Reconstruct it for later use during GC. */
2143 case R_M32R_GNU_VTINHERIT:
2144 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2145 return false;
2146 break;
2148 /* This relocation describes which C++ vtable entries are actually
2149 used. Record for later use during GC. */
2150 case R_M32R_GNU_VTENTRY:
2151 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2152 return false;
2153 break;
2157 return true;
2160 #define ELF_ARCH bfd_arch_m32r
2161 #define ELF_MACHINE_CODE EM_CYGNUS_M32R
2162 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
2164 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
2165 #define TARGET_BIG_NAME "elf32-m32r"
2167 #define elf_info_to_howto 0
2168 #define elf_info_to_howto_rel m32r_info_to_howto_rel
2169 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
2170 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
2171 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
2172 #define elf_backend_relocate_section m32r_elf_relocate_section
2173 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
2174 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
2175 #define elf_backend_check_relocs m32r_elf_check_relocs
2177 #define elf_backend_can_gc_sections 1
2178 #if 0 /* not yet */
2179 /* relax support */
2180 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
2181 #define bfd_elf32_bfd_get_relocated_section_contents \
2182 m32r_elf_get_relocated_section_contents
2183 #endif
2185 #define elf_backend_object_p m32r_elf_object_p
2186 #define elf_backend_final_write_processing m32r_elf_final_write_processing
2187 #define bfd_elf32_bfd_copy_private_bfd_data m32r_elf_copy_private_bfd_data
2188 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
2189 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
2190 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
2192 #include "elf32-target.h"