2007-02-13 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elf32-m32c.c
blob492c5bc8e822bfb7c6e388807eac84df17af492b
1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m32c.h"
26 #include "libiberty.h"
28 /* Forward declarations. */
29 static reloc_howto_type * m32c_reloc_type_lookup
30 (bfd *, bfd_reloc_code_real_type);
31 static void m32c_info_to_howto_rela
32 (bfd *, arelent *, Elf_Internal_Rela *);
33 static bfd_boolean m32c_elf_relocate_section
34 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35 static bfd_boolean m32c_elf_check_relocs
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
38 #ifdef DEBUG
39 char * m32c_get_reloc (long reloc);
40 void dump_symtab (bfd *, void *, void *);
41 #endif
42 static bfd_boolean m32c_elf_relax_section
43 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
46 static reloc_howto_type m32c_elf_howto_table [] =
48 /* This reloc does nothing. */
49 HOWTO (R_M32C_NONE, /* type */
50 0, /* rightshift */
51 0, /* size (0 = byte, 1 = short, 2 = long) */
52 32, /* bitsize */
53 FALSE, /* pc_relative */
54 0, /* bitpos */
55 complain_overflow_bitfield, /* complain_on_overflow */
56 bfd_elf_generic_reloc, /* special_function */
57 "R_M32C_NONE", /* name */
58 FALSE, /* partial_inplace */
59 0, /* src_mask */
60 0, /* dst_mask */
61 FALSE), /* pcrel_offset */
63 /* GCC intentionally overflows these next two in order to work
64 around limitations in the addressing modes, so don't complain
65 about overflow. */
66 HOWTO (R_M32C_16, /* type */
67 0, /* rightshift */
68 1, /* size (0 = byte, 1 = short, 2 = long) */
69 16, /* bitsize */
70 FALSE, /* pc_relative */
71 0, /* bitpos */
72 complain_overflow_dont, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_M32C_16", /* name */
75 FALSE, /* partial_inplace */
76 0, /* src_mask */
77 0xffff, /* dst_mask */
78 FALSE), /* pcrel_offset */
80 HOWTO (R_M32C_24, /* type */
81 0, /* rightshift */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
83 24, /* bitsize */
84 FALSE, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_dont, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_M32C_24", /* name */
89 FALSE, /* partial_inplace */
90 0, /* src_mask */
91 0xffffff, /* dst_mask */
92 FALSE), /* pcrel_offset */
94 HOWTO (R_M32C_32, /* type */
95 0, /* rightshift */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
97 32, /* bitsize */
98 FALSE, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_bitfield, /* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
102 "R_M32C_32", /* name */
103 FALSE, /* partial_inplace */
104 0, /* src_mask */
105 0xffffffff, /* dst_mask */
106 FALSE), /* pcrel_offset */
108 HOWTO (R_M32C_8_PCREL, /* type */
109 0, /* rightshift */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
111 8, /* bitsize */
112 TRUE, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_signed, /* complain_on_overflow */
115 bfd_elf_generic_reloc, /* special_function */
116 "R_M32C_8_PCREL", /* name */
117 FALSE, /* partial_inplace */
118 0, /* src_mask */
119 0xff, /* dst_mask */
120 TRUE), /* pcrel_offset */
122 HOWTO (R_M32C_16_PCREL, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
126 TRUE, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_signed, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_M32C_16_PCREL", /* name */
131 FALSE, /* partial_inplace */
132 0, /* src_mask */
133 0xffff, /* dst_mask */
134 TRUE), /* pcrel_offset */
136 HOWTO (R_M32C_8, /* type */
137 0, /* rightshift */
138 0, /* size (0 = byte, 1 = short, 2 = long) */
139 8, /* bitsize */
140 FALSE, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_unsigned, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_M32C_8", /* name */
145 FALSE, /* partial_inplace */
146 0, /* src_mask */
147 0xff, /* dst_mask */
148 FALSE), /* pcrel_offset */
150 HOWTO (R_M32C_LO16, /* type */
151 0, /* rightshift */
152 1, /* size (0 = byte, 1 = short, 2 = long) */
153 16, /* bitsize */
154 FALSE, /* pc_relative */
155 0, /* bitpos */
156 complain_overflow_dont, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_M32C_LO16", /* name */
159 FALSE, /* partial_inplace */
160 0, /* src_mask */
161 0xffff, /* dst_mask */
162 FALSE), /* pcrel_offset */
164 HOWTO (R_M32C_HI8, /* type */
165 0, /* rightshift */
166 0, /* size (0 = byte, 1 = short, 2 = long) */
167 8, /* bitsize */
168 FALSE, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_dont, /* complain_on_overflow */
171 bfd_elf_generic_reloc, /* special_function */
172 "R_M32C_HI8", /* name */
173 FALSE, /* partial_inplace */
174 0, /* src_mask */
175 0xff, /* dst_mask */
176 FALSE), /* pcrel_offset */
178 HOWTO (R_M32C_HI16, /* type */
179 0, /* rightshift */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
181 16, /* bitsize */
182 FALSE, /* pc_relative */
183 0, /* bitpos */
184 complain_overflow_dont, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_M32C_HI16", /* name */
187 FALSE, /* partial_inplace */
188 0, /* src_mask */
189 0xffff, /* dst_mask */
190 FALSE), /* pcrel_offset */
192 HOWTO (R_M32C_RL_JUMP, /* type */
193 0, /* rightshift */
194 0, /* size (0 = byte, 1 = short, 2 = long) */
195 0, /* bitsize */
196 FALSE, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_signed, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_M32C_RL_JUMP", /* name */
201 FALSE, /* partial_inplace */
202 0, /* src_mask */
203 0, /* dst_mask */
204 FALSE), /* pcrel_offset */
206 HOWTO (R_M32C_RL_1ADDR, /* type */
207 0, /* rightshift */
208 0, /* size (0 = byte, 1 = short, 2 = long) */
209 0, /* bitsize */
210 FALSE, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_signed, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_M32C_RL_1ADDR", /* name */
215 FALSE, /* partial_inplace */
216 0, /* src_mask */
217 0, /* dst_mask */
218 FALSE), /* pcrel_offset */
220 HOWTO (R_M32C_RL_2ADDR, /* type */
221 0, /* rightshift */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
223 0, /* bitsize */
224 FALSE, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_signed, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_M32C_RL_2ADDR", /* name */
229 FALSE, /* partial_inplace */
230 0, /* src_mask */
231 0, /* dst_mask */
232 FALSE), /* pcrel_offset */
236 /* Map BFD reloc types to M32C ELF reloc types. */
238 struct m32c_reloc_map
240 bfd_reloc_code_real_type bfd_reloc_val;
241 unsigned int m32c_reloc_val;
244 static const struct m32c_reloc_map m32c_reloc_map [] =
246 { BFD_RELOC_NONE, R_M32C_NONE },
247 { BFD_RELOC_16, R_M32C_16 },
248 { BFD_RELOC_24, R_M32C_24 },
249 { BFD_RELOC_32, R_M32C_32 },
250 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
251 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
252 { BFD_RELOC_8, R_M32C_8 },
253 { BFD_RELOC_LO16, R_M32C_LO16 },
254 { BFD_RELOC_HI16, R_M32C_HI16 },
255 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
256 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
257 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
258 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
261 static reloc_howto_type *
262 m32c_reloc_type_lookup
263 (bfd * abfd ATTRIBUTE_UNUSED,
264 bfd_reloc_code_real_type code)
266 unsigned int i;
268 for (i = ARRAY_SIZE (m32c_reloc_map); --i;)
269 if (m32c_reloc_map [i].bfd_reloc_val == code)
270 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
272 return NULL;
275 /* Set the howto pointer for an M32C ELF reloc. */
277 static void
278 m32c_info_to_howto_rela
279 (bfd * abfd ATTRIBUTE_UNUSED,
280 arelent * cache_ptr,
281 Elf_Internal_Rela * dst)
283 unsigned int r_type;
285 r_type = ELF32_R_TYPE (dst->r_info);
286 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
287 cache_ptr->howto = & m32c_elf_howto_table [r_type];
292 /* Relocate an M32C ELF section.
293 There is some attempt to make this function usable for many architectures,
294 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
295 if only to serve as a learning tool.
297 The RELOCATE_SECTION function is called by the new ELF backend linker
298 to handle the relocations for a section.
300 The relocs are always passed as Rela structures; if the section
301 actually uses Rel structures, the r_addend field will always be
302 zero.
304 This function is responsible for adjusting the section contents as
305 necessary, and (if using Rela relocs and generating a relocatable
306 output file) adjusting the reloc addend as necessary.
308 This function does not have to worry about setting the reloc
309 address or the reloc symbol index.
311 LOCAL_SYMS is a pointer to the swapped in local symbols.
313 LOCAL_SECTIONS is an array giving the section in the input file
314 corresponding to the st_shndx field of each local symbol.
316 The global hash table entry for the global symbols can be found
317 via elf_sym_hashes (input_bfd).
319 When generating relocatable output, this function must handle
320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
321 going to be the section symbol corresponding to the output
322 section, which means that the addend must be adjusted
323 accordingly. */
325 static bfd_boolean
326 m32c_elf_relocate_section
327 (bfd * output_bfd ATTRIBUTE_UNUSED,
328 struct bfd_link_info * info,
329 bfd * input_bfd,
330 asection * input_section,
331 bfd_byte * contents,
332 Elf_Internal_Rela * relocs,
333 Elf_Internal_Sym * local_syms,
334 asection ** local_sections)
336 Elf_Internal_Shdr * symtab_hdr;
337 struct elf_link_hash_entry ** sym_hashes;
338 Elf_Internal_Rela * rel;
339 Elf_Internal_Rela * relend;
340 bfd *dynobj;
341 asection *splt;
343 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
344 sym_hashes = elf_sym_hashes (input_bfd);
345 relend = relocs + input_section->reloc_count;
347 dynobj = elf_hash_table (info)->dynobj;
348 splt = NULL;
349 if (dynobj != NULL)
350 splt = bfd_get_section_by_name (dynobj, ".plt");
352 for (rel = relocs; rel < relend; rel ++)
354 reloc_howto_type * howto;
355 unsigned long r_symndx;
356 Elf_Internal_Sym * sym;
357 asection * sec;
358 struct elf_link_hash_entry * h;
359 bfd_vma relocation;
360 bfd_reloc_status_type r;
361 const char * name = NULL;
362 int r_type;
364 r_type = ELF32_R_TYPE (rel->r_info);
366 /* These are only used for relaxing; we don't actually relocate
367 anything with them, so skip them. */
368 if (r_type == R_M32C_RL_JUMP
369 || r_type == R_M32C_RL_1ADDR
370 || r_type == R_M32C_RL_2ADDR)
371 continue;
373 r_symndx = ELF32_R_SYM (rel->r_info);
375 if (info->relocatable)
377 /* This is a relocatable link. We don't have to change
378 anything, unless the reloc is against a section symbol,
379 in which case we have to adjust according to where the
380 section symbol winds up in the output section. */
381 if (r_symndx < symtab_hdr->sh_info)
383 sym = local_syms + r_symndx;
385 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
387 sec = local_sections [r_symndx];
388 rel->r_addend += sec->output_offset + sym->st_value;
392 continue;
395 /* This is a final link. */
396 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
397 h = NULL;
398 sym = NULL;
399 sec = NULL;
401 if (r_symndx < symtab_hdr->sh_info)
403 sym = local_syms + r_symndx;
404 sec = local_sections [r_symndx];
405 relocation = (sec->output_section->vma
406 + sec->output_offset
407 + sym->st_value);
409 name = bfd_elf_string_from_elf_section
410 (input_bfd, symtab_hdr->sh_link, sym->st_name);
411 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
413 else
415 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
417 while (h->root.type == bfd_link_hash_indirect
418 || h->root.type == bfd_link_hash_warning)
419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
421 name = h->root.root.string;
423 if (h->root.type == bfd_link_hash_defined
424 || h->root.type == bfd_link_hash_defweak)
426 sec = h->root.u.def.section;
427 relocation = (h->root.u.def.value
428 + sec->output_section->vma
429 + sec->output_offset);
431 else if (h->root.type == bfd_link_hash_undefweak)
433 relocation = 0;
435 else
437 if (! ((*info->callbacks->undefined_symbol)
438 (info, h->root.root.string, input_bfd,
439 input_section, rel->r_offset, TRUE)))
440 return FALSE;
441 relocation = 0;
445 switch (ELF32_R_TYPE (rel->r_info))
447 case R_M32C_16:
449 bfd_vma *plt_offset;
451 if (h != NULL)
452 plt_offset = &h->plt.offset;
453 else
454 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
456 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
457 relocation, *plt_offset);*/
458 if (relocation <= 0xffff)
460 /* If the symbol is in range for a 16-bit address, we should
461 have deallocated the plt entry in relax_section. */
462 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
464 else
466 /* If the symbol is out of range for a 16-bit address,
467 we must have allocated a plt entry. */
468 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
470 /* If this is the first time we've processed this symbol,
471 fill in the plt entry with the correct symbol address. */
472 if ((*plt_offset & 1) == 0)
474 unsigned int x;
476 x = 0x000000fc; /* jmpf */
477 x |= (relocation << 8) & 0xffffff00;
478 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
479 *plt_offset |= 1;
482 relocation = (splt->output_section->vma
483 + splt->output_offset
484 + (*plt_offset & -2));
485 if (name)
487 char *newname = bfd_malloc (strlen(name)+5);
488 strcpy (newname, name);
489 strcat(newname, ".plt");
490 _bfd_generic_link_add_one_symbol (info,
491 input_bfd,
492 newname,
493 BSF_FUNCTION | BSF_WEAK,
494 splt,
495 (*plt_offset & -2),
503 break;
505 case R_M32C_HI8:
506 case R_M32C_HI16:
507 relocation >>= 16;
508 break;
511 #if 0
512 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
513 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
514 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
515 relocation, rel->r_addend);
517 int i;
518 for (i=0; i<4; i++)
519 printf (" %02x", contents[rel->r_offset+i]);
520 printf ("\n");
522 #endif
523 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
524 contents, rel->r_offset, relocation,
525 rel->r_addend);
527 if (r != bfd_reloc_ok)
529 const char * msg = (const char *) NULL;
531 switch (r)
533 case bfd_reloc_overflow:
534 r = info->callbacks->reloc_overflow
535 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
536 input_bfd, input_section, rel->r_offset);
537 break;
539 case bfd_reloc_undefined:
540 r = info->callbacks->undefined_symbol
541 (info, name, input_bfd, input_section, rel->r_offset,
542 TRUE);
543 break;
545 case bfd_reloc_outofrange:
546 msg = _("internal error: out of range error");
547 break;
549 case bfd_reloc_notsupported:
550 msg = _("internal error: unsupported relocation error");
551 break;
553 case bfd_reloc_dangerous:
554 msg = _("internal error: dangerous relocation");
555 break;
557 default:
558 msg = _("internal error: unknown error");
559 break;
562 if (msg)
563 r = info->callbacks->warning
564 (info, msg, name, input_bfd, input_section, rel->r_offset);
566 if (! r)
567 return FALSE;
571 return TRUE;
574 /* We support 16-bit pointers to code above 64k by generating a thunk
575 below 64k containing a JMP instruction to the final address. */
577 static bfd_boolean
578 m32c_elf_check_relocs
579 (bfd * abfd,
580 struct bfd_link_info * info,
581 asection * sec,
582 const Elf_Internal_Rela * relocs)
584 Elf_Internal_Shdr * symtab_hdr;
585 struct elf_link_hash_entry ** sym_hashes;
586 struct elf_link_hash_entry ** sym_hashes_end;
587 const Elf_Internal_Rela * rel;
588 const Elf_Internal_Rela * rel_end;
589 bfd_vma *local_plt_offsets;
590 asection *splt;
591 bfd *dynobj;
593 if (info->relocatable)
594 return TRUE;
596 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
597 sym_hashes = elf_sym_hashes (abfd);
598 local_plt_offsets = elf_local_got_offsets (abfd);
599 splt = NULL;
600 dynobj = elf_hash_table(info)->dynobj;
602 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
603 if (!elf_bad_symtab (abfd))
604 sym_hashes_end -= symtab_hdr->sh_info;
606 rel_end = relocs + sec->reloc_count;
607 for (rel = relocs; rel < rel_end; rel++)
609 struct elf_link_hash_entry *h;
610 unsigned long r_symndx;
611 bfd_vma *offset;
613 r_symndx = ELF32_R_SYM (rel->r_info);
614 if (r_symndx < symtab_hdr->sh_info)
615 h = NULL;
616 else
618 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
619 while (h->root.type == bfd_link_hash_indirect
620 || h->root.type == bfd_link_hash_warning)
621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
624 switch (ELF32_R_TYPE (rel->r_info))
626 /* This relocation describes a 16-bit pointer to a function.
627 We may need to allocate a thunk in low memory; reserve memory
628 for it now. */
629 case R_M32C_16:
630 if (dynobj == NULL)
631 elf_hash_table (info)->dynobj = dynobj = abfd;
632 if (splt == NULL)
634 splt = bfd_get_section_by_name (dynobj, ".plt");
635 if (splt == NULL)
637 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
638 | SEC_IN_MEMORY | SEC_LINKER_CREATED
639 | SEC_READONLY | SEC_CODE);
640 splt = bfd_make_section_with_flags (dynobj, ".plt", flags);
641 if (splt == NULL
642 || ! bfd_set_section_alignment (dynobj, splt, 1))
643 return FALSE;
647 if (h != NULL)
648 offset = &h->plt.offset;
649 else
651 if (local_plt_offsets == NULL)
653 size_t size;
654 unsigned int i;
656 size = symtab_hdr->sh_info * sizeof (bfd_vma);
657 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
658 if (local_plt_offsets == NULL)
659 return FALSE;
660 elf_local_got_offsets (abfd) = local_plt_offsets;
662 for (i = 0; i < symtab_hdr->sh_info; i++)
663 local_plt_offsets[i] = (bfd_vma) -1;
665 offset = &local_plt_offsets[r_symndx];
668 if (*offset == (bfd_vma) -1)
670 *offset = splt->size;
671 splt->size += 4;
673 break;
677 return TRUE;
680 /* This must exist if dynobj is ever set. */
682 static bfd_boolean
683 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
684 struct bfd_link_info *info)
686 bfd *dynobj;
687 asection *splt;
689 /* As an extra sanity check, verify that all plt entries have
690 been filled in. */
692 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
693 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
695 bfd_byte *contents = splt->contents;
696 unsigned int i, size = splt->size;
697 for (i = 0; i < size; i += 4)
699 unsigned int x = bfd_get_32 (dynobj, contents + i);
700 BFD_ASSERT (x != 0);
704 return TRUE;
707 static bfd_boolean
708 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
709 struct bfd_link_info *info)
711 bfd *dynobj;
712 asection *splt;
714 if (info->relocatable)
715 return TRUE;
717 dynobj = elf_hash_table (info)->dynobj;
718 if (dynobj == NULL)
719 return TRUE;
721 splt = bfd_get_section_by_name (dynobj, ".plt");
722 BFD_ASSERT (splt != NULL);
724 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
725 if (splt->contents == NULL)
726 return FALSE;
728 return TRUE;
731 /* Function to set the ELF flag bits. */
733 static bfd_boolean
734 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
736 elf_elfheader (abfd)->e_flags = flags;
737 elf_flags_init (abfd) = TRUE;
738 return TRUE;
741 /* Merge backend specific data from an object file to the output
742 object file when linking. */
744 static bfd_boolean
745 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
747 flagword old_flags, old_partial;
748 flagword new_flags, new_partial;
749 bfd_boolean error = FALSE;
750 char new_opt[80];
751 char old_opt[80];
753 new_opt[0] = old_opt[0] = '\0';
754 new_flags = elf_elfheader (ibfd)->e_flags;
755 old_flags = elf_elfheader (obfd)->e_flags;
757 #ifdef DEBUG
758 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
759 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
760 bfd_get_filename (ibfd));
761 #endif
763 if (!elf_flags_init (obfd))
765 /* First call, no flags set. */
766 elf_flags_init (obfd) = TRUE;
767 elf_elfheader (obfd)->e_flags = new_flags;
770 else if (new_flags == old_flags)
771 /* Compatible flags are ok. */
774 else /* Possibly incompatible flags. */
776 /* Warn if different cpu is used (allow a specific cpu to override
777 the generic cpu). */
778 new_partial = (new_flags & EF_M32C_CPU_MASK);
779 old_partial = (old_flags & EF_M32C_CPU_MASK);
780 if (new_partial == old_partial)
783 else
785 switch (new_partial)
787 default: strcat (new_opt, " -m16c"); break;
788 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
789 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
792 switch (old_partial)
794 default: strcat (old_opt, " -m16c"); break;
795 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
796 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
800 /* Print out any mismatches from above. */
801 if (new_opt[0])
803 error = TRUE;
804 (*_bfd_error_handler)
805 (_("%s: compiled with %s and linked with modules compiled with %s"),
806 bfd_get_filename (ibfd), new_opt, old_opt);
809 new_flags &= ~ EF_M32C_ALL_FLAGS;
810 old_flags &= ~ EF_M32C_ALL_FLAGS;
812 /* Warn about any other mismatches. */
813 if (new_flags != old_flags)
815 error = TRUE;
816 (*_bfd_error_handler)
817 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
818 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
822 if (error)
823 bfd_set_error (bfd_error_bad_value);
825 return !error;
829 static bfd_boolean
830 m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
832 FILE *file = (FILE *) ptr;
833 flagword flags;
835 BFD_ASSERT (abfd != NULL && ptr != NULL);
837 /* Print normal ELF private data. */
838 _bfd_elf_print_private_bfd_data (abfd, ptr);
840 flags = elf_elfheader (abfd)->e_flags;
841 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
843 switch (flags & EF_M32C_CPU_MASK)
845 default: break;
846 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
847 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
850 fputc ('\n', file);
851 return TRUE;
854 /* Return the MACH for an e_flags value. */
856 static int
857 elf32_m32c_machine (bfd *abfd)
859 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
861 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
862 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
865 return bfd_mach_m16c;
868 static bfd_boolean
869 m32c_elf_object_p (bfd *abfd)
871 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
872 elf32_m32c_machine (abfd));
873 return TRUE;
877 #ifdef DEBUG
878 void
879 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
881 size_t locsymcount;
882 Elf_Internal_Sym *isymbuf;
883 Elf_Internal_Sym *isymend;
884 Elf_Internal_Sym *isym;
885 Elf_Internal_Shdr *symtab_hdr;
886 bfd_boolean free_internal = 0, free_external = 0;
887 char * st_info_str;
888 char * st_info_stb_str;
889 char * st_other_str;
890 char * st_shndx_str;
892 if (! internal_syms)
894 internal_syms = bfd_malloc (1000);
895 free_internal = 1;
897 if (! external_syms)
899 external_syms = bfd_malloc (1000);
900 free_external = 1;
903 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
904 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
905 if (free_internal)
906 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
907 symtab_hdr->sh_info, 0,
908 internal_syms, external_syms, NULL);
909 else
910 isymbuf = internal_syms;
911 isymend = isymbuf + locsymcount;
913 for (isym = isymbuf ; isym < isymend ; isym++)
915 switch (ELF_ST_TYPE (isym->st_info))
917 case STT_FUNC: st_info_str = "STT_FUNC";
918 case STT_SECTION: st_info_str = "STT_SECTION";
919 case STT_FILE: st_info_str = "STT_FILE";
920 case STT_OBJECT: st_info_str = "STT_OBJECT";
921 case STT_TLS: st_info_str = "STT_TLS";
922 default: st_info_str = "";
924 switch (ELF_ST_BIND (isym->st_info))
926 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
927 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
928 default: st_info_stb_str = "";
930 switch (ELF_ST_VISIBILITY (isym->st_other))
932 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
933 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
934 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
935 default: st_other_str = "";
937 switch (isym->st_shndx)
939 case SHN_ABS: st_shndx_str = "SHN_ABS";
940 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
941 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
942 default: st_shndx_str = "";
945 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
946 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
947 isym,
948 (unsigned long) isym->st_value,
949 (unsigned long) isym->st_size,
950 isym->st_name,
951 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
952 isym->st_name),
953 isym->st_info, st_info_str, st_info_stb_str,
954 isym->st_other, st_other_str,
955 isym->st_shndx, st_shndx_str);
957 if (free_internal)
958 free (internal_syms);
959 if (free_external)
960 free (external_syms);
963 char *
964 m32c_get_reloc (long reloc)
966 if (0 <= reloc && reloc < R_M32C_max)
967 return m32c_elf_howto_table[reloc].name;
968 else
969 return "";
971 #endif /* DEBUG */
973 /* Handle relaxing. */
975 /* A subroutine of m32c_elf_relax_section. If the global symbol H
976 is within the low 64k, remove any entry for it in the plt. */
978 struct relax_plt_data
980 asection *splt;
981 bfd_boolean *again;
984 static bfd_boolean
985 m32c_relax_plt_check (struct elf_link_hash_entry *h,
986 PTR xdata)
988 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
990 if (h->root.type == bfd_link_hash_warning)
991 h = (struct elf_link_hash_entry *) h->root.u.i.link;
993 if (h->plt.offset != (bfd_vma) -1)
995 bfd_vma address;
997 if (h->root.type == bfd_link_hash_undefined
998 || h->root.type == bfd_link_hash_undefweak)
999 address = 0;
1000 else
1001 address = (h->root.u.def.section->output_section->vma
1002 + h->root.u.def.section->output_offset
1003 + h->root.u.def.value);
1005 if (address <= 0xffff)
1007 h->plt.offset = -1;
1008 data->splt->size -= 4;
1009 *data->again = TRUE;
1013 return TRUE;
1016 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1017 previously had a plt entry, give it a new entry offset. */
1019 static bfd_boolean
1020 m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1021 PTR xdata)
1023 bfd_vma *entry = (bfd_vma *) xdata;
1025 if (h->root.type == bfd_link_hash_warning)
1026 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1028 if (h->plt.offset != (bfd_vma) -1)
1030 h->plt.offset = *entry;
1031 *entry += 4;
1034 return TRUE;
1037 static bfd_boolean
1038 m32c_elf_relax_plt_section (bfd *dynobj,
1039 asection *splt,
1040 struct bfd_link_info *info,
1041 bfd_boolean *again)
1043 struct relax_plt_data relax_plt_data;
1044 bfd *ibfd;
1046 /* Assume nothing changes. */
1047 *again = FALSE;
1049 if (info->relocatable)
1050 return TRUE;
1052 /* We only relax the .plt section at the moment. */
1053 if (dynobj != elf_hash_table (info)->dynobj
1054 || strcmp (splt->name, ".plt") != 0)
1055 return TRUE;
1057 /* Quick check for an empty plt. */
1058 if (splt->size == 0)
1059 return TRUE;
1061 /* Map across all global symbols; see which ones happen to
1062 fall in the low 64k. */
1063 relax_plt_data.splt = splt;
1064 relax_plt_data.again = again;
1065 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1066 &relax_plt_data);
1068 /* Likewise for local symbols, though that's somewhat less convenient
1069 as we have to walk the list of input bfds and swap in symbol data. */
1070 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1072 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1073 Elf_Internal_Shdr *symtab_hdr;
1074 Elf_Internal_Sym *isymbuf = NULL;
1075 unsigned int idx;
1077 if (! local_plt_offsets)
1078 continue;
1080 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1081 if (symtab_hdr->sh_info != 0)
1083 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1084 if (isymbuf == NULL)
1085 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1086 symtab_hdr->sh_info, 0,
1087 NULL, NULL, NULL);
1088 if (isymbuf == NULL)
1089 return FALSE;
1092 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1094 Elf_Internal_Sym *isym;
1095 asection *tsec;
1096 bfd_vma address;
1098 if (local_plt_offsets[idx] == (bfd_vma) -1)
1099 continue;
1101 isym = &isymbuf[idx];
1102 if (isym->st_shndx == SHN_UNDEF)
1103 continue;
1104 else if (isym->st_shndx == SHN_ABS)
1105 tsec = bfd_abs_section_ptr;
1106 else if (isym->st_shndx == SHN_COMMON)
1107 tsec = bfd_com_section_ptr;
1108 else
1109 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1111 address = (tsec->output_section->vma
1112 + tsec->output_offset
1113 + isym->st_value);
1114 if (address <= 0xffff)
1116 local_plt_offsets[idx] = -1;
1117 splt->size -= 4;
1118 *again = TRUE;
1122 if (isymbuf != NULL
1123 && symtab_hdr->contents != (unsigned char *) isymbuf)
1125 if (! info->keep_memory)
1126 free (isymbuf);
1127 else
1129 /* Cache the symbols for elf_link_input_bfd. */
1130 symtab_hdr->contents = (unsigned char *) isymbuf;
1135 /* If we changed anything, walk the symbols again to reallocate
1136 .plt entry addresses. */
1137 if (*again && splt->size > 0)
1139 bfd_vma entry = 0;
1141 elf_link_hash_traverse (elf_hash_table (info),
1142 m32c_relax_plt_realloc, &entry);
1144 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1146 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1147 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1148 unsigned int idx;
1150 if (! local_plt_offsets)
1151 continue;
1153 for (idx = 0; idx < nlocals; ++idx)
1154 if (local_plt_offsets[idx] != (bfd_vma) -1)
1156 local_plt_offsets[idx] = entry;
1157 entry += 4;
1162 return TRUE;
1165 static int
1166 compare_reloc (const void *e1, const void *e2)
1168 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1169 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1171 if (i1->r_offset == i2->r_offset)
1172 return 0;
1173 else
1174 return i1->r_offset < i2->r_offset ? -1 : 1;
1177 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1178 static bfd_vma
1179 m32c_offset_for_reloc (bfd *abfd,
1180 Elf_Internal_Rela *rel,
1181 Elf_Internal_Shdr *symtab_hdr,
1182 Elf_External_Sym_Shndx *shndx_buf,
1183 Elf_Internal_Sym *intsyms)
1185 bfd_vma symval;
1187 /* Get the value of the symbol referred to by the reloc. */
1188 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1190 /* A local symbol. */
1191 Elf_Internal_Sym *isym;
1192 Elf_External_Sym_Shndx *shndx;
1193 asection *ssec;
1196 isym = intsyms + ELF32_R_SYM (rel->r_info);
1197 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1198 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0);
1200 symval = isym->st_value;
1201 if (ssec)
1202 symval += ssec->output_section->vma
1203 + ssec->output_offset;
1205 else
1207 unsigned long indx;
1208 struct elf_link_hash_entry *h;
1210 /* An external symbol. */
1211 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1212 h = elf_sym_hashes (abfd)[indx];
1213 BFD_ASSERT (h != NULL);
1215 if (h->root.type != bfd_link_hash_defined
1216 && h->root.type != bfd_link_hash_defweak)
1217 /* This appears to be a reference to an undefined
1218 symbol. Just ignore it--it will be caught by the
1219 regular reloc processing. */
1220 return 0;
1222 symval = (h->root.u.def.value
1223 + h->root.u.def.section->output_section->vma
1224 + h->root.u.def.section->output_offset);
1226 return symval;
1229 static int bytes_saved = 0;
1231 static int bytes_to_reloc[] = {
1232 R_M32C_NONE,
1233 R_M32C_8,
1234 R_M32C_16,
1235 R_M32C_24,
1236 R_M32C_32
1239 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1241 /* Mask for the number of relocs associated with this insn. */
1242 #define RLA_RELOCS 0x0000000f
1243 /* Number of bytes gas emitted (before gas's relaxing) */
1244 #define RLA_NBYTES 0x00000ff0
1246 /* If the displacement is within the given range and the new encoding
1247 differs from the old encoding (the index), then the insn can be
1248 relaxed to the new encoding. */
1249 typedef struct {
1250 int bytes;
1251 unsigned int max_disp;
1252 unsigned char new_encoding;
1253 } EncodingTable;
1255 static EncodingTable m16c_addr_encodings[] = {
1256 { 0, 0, 0 }, /* R0 */
1257 { 0, 0, 1 }, /* R1 */
1258 { 0, 0, 2 }, /* R2 */
1259 { 0, 0, 3 }, /* R3 */
1260 { 0, 0, 4 }, /* A0 */
1261 { 0, 0, 5 }, /* A1 */
1262 { 0, 0, 6 }, /* [A0] */
1263 { 0, 0, 7 }, /* [A1] */
1264 { 1, 0, 6 }, /* udsp:8[A0] */
1265 { 1, 0, 7 }, /* udsp:8[A1] */
1266 { 1, 0, 10 }, /* udsp:8[SB] */
1267 { 1, 0, 11 }, /* sdsp:8[FB] */
1268 { 2, 255, 8 }, /* udsp:16[A0] */
1269 { 2, 255, 9 }, /* udsp:16[A1] */
1270 { 2, 255, 10 }, /* udsp:16[SB] */
1271 { 2, 0, 15 }, /* abs:16 */
1274 static EncodingTable m16c_jmpaddr_encodings[] = {
1275 { 0, 0, 0 }, /* R0 */
1276 { 0, 0, 1 }, /* R1 */
1277 { 0, 0, 2 }, /* R2 */
1278 { 0, 0, 3 }, /* R3 */
1279 { 0, 0, 4 }, /* A0 */
1280 { 0, 0, 5 }, /* A1 */
1281 { 0, 0, 6 }, /* [A0] */
1282 { 0, 0, 7 }, /* [A1] */
1283 { 1, 0, 6 }, /* udsp:8[A0] */
1284 { 1, 0, 7 }, /* udsp:8[A1] */
1285 { 1, 0, 10 }, /* udsp:8[SB] */
1286 { 1, 0, 11 }, /* sdsp:8[FB] */
1287 { 3, 255, 8 }, /* udsp:20[A0] */
1288 { 3, 255, 9 }, /* udsp:20[A1] */
1289 { 2, 255, 10 }, /* udsp:16[SB] */
1290 { 2, 0, 15 }, /* abs:16 */
1293 static EncodingTable m32c_addr_encodings[] = {
1294 { 0, 0, 0 }, /* [A0] */
1295 { 0, 0, 1 }, /* [A1] */
1296 { 0, 0, 2 }, /* A0 */
1297 { 0, 0, 3 }, /* A1 */
1298 { 1, 0, 0 }, /* udsp:8[A0] */
1299 { 1, 0, 1 }, /* udsp:8[A1] */
1300 { 1, 0, 6 }, /* udsp:8[SB] */
1301 { 1, 0, 7 }, /* sdsp:8[FB] */
1302 { 2, 255, 4 }, /* udsp:16[A0] */
1303 { 2, 255, 5 }, /* udsp:16[A1] */
1304 { 2, 255, 6 }, /* udsp:16[SB] */
1305 { 2, 127, 7 }, /* sdsp:16[FB] */
1306 { 3, 65535, 8 }, /* udsp:24[A0] */
1307 { 3, 65535, 9 }, /* udsp:24[A1] */
1308 { 3, 65535, 15 }, /* abs24 */
1309 { 2, 0, 15 }, /* abs16 */
1310 { 0, 0, 16 }, /* R2 */
1311 { 0, 0, 17 }, /* R3 */
1312 { 0, 0, 18 }, /* R0 */
1313 { 0, 0, 19 }, /* R1 */
1314 { 0, 0, 20 }, /* */
1315 { 0, 0, 21 }, /* */
1316 { 0, 0, 22 }, /* */
1317 { 0, 0, 23 }, /* */
1318 { 0, 0, 24 }, /* */
1319 { 0, 0, 25 }, /* */
1320 { 0, 0, 26 }, /* */
1321 { 0, 0, 27 }, /* */
1322 { 0, 0, 28 }, /* */
1323 { 0, 0, 29 }, /* */
1324 { 0, 0, 30 }, /* */
1325 { 0, 0, 31 }, /* */
1328 static bfd_boolean
1329 m32c_elf_relax_section
1330 (bfd * abfd,
1331 asection * sec,
1332 struct bfd_link_info * link_info,
1333 bfd_boolean * again)
1335 Elf_Internal_Shdr *symtab_hdr;
1336 Elf_Internal_Shdr *shndx_hdr;
1337 Elf_Internal_Rela *internal_relocs;
1338 Elf_Internal_Rela *free_relocs = NULL;
1339 Elf_Internal_Rela *irel, *irelend, *srel;
1340 bfd_byte * contents = NULL;
1341 bfd_byte * free_contents = NULL;
1342 Elf_Internal_Sym *intsyms = NULL;
1343 Elf_Internal_Sym *free_intsyms = NULL;
1344 Elf_External_Sym_Shndx *shndx_buf = NULL;
1345 int machine;
1347 if (abfd == elf_hash_table (link_info)->dynobj
1348 && strcmp (sec->name, ".plt") == 0)
1349 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1351 /* Assume nothing changes. */
1352 *again = FALSE;
1354 machine = elf32_m32c_machine (abfd);
1356 /* We don't have to do anything for a relocatable link, if
1357 this section does not have relocs, or if this is not a
1358 code section. */
1359 if (link_info->relocatable
1360 || (sec->flags & SEC_RELOC) == 0
1361 || sec->reloc_count == 0
1362 || (sec->flags & SEC_CODE) == 0)
1363 return TRUE;
1365 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1366 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1368 /* Get the section contents. */
1369 if (elf_section_data (sec)->this_hdr.contents != NULL)
1370 contents = elf_section_data (sec)->this_hdr.contents;
1371 /* Go get them off disk. */
1372 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1373 goto error_return;
1375 /* Read this BFD's symbols. */
1376 /* Get cached copy if it exists. */
1377 if (symtab_hdr->contents != NULL)
1379 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1381 else
1383 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1384 symtab_hdr->contents = (bfd_byte *) intsyms;
1387 if (shndx_hdr->sh_size != 0)
1389 bfd_size_type amt;
1391 amt = symtab_hdr->sh_info;
1392 amt *= sizeof (Elf_External_Sym_Shndx);
1393 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1394 if (shndx_buf == NULL)
1395 goto error_return;
1396 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1397 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1398 goto error_return;
1399 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1402 /* Get a copy of the native relocations. */
1403 internal_relocs = (_bfd_elf_link_read_relocs
1404 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1405 link_info->keep_memory));
1406 if (internal_relocs == NULL)
1407 goto error_return;
1408 if (! link_info->keep_memory)
1409 free_relocs = internal_relocs;
1411 /* The RL_ relocs must be just before the operand relocs they go
1412 with, so we must sort them to guarantee this. */
1413 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1414 compare_reloc);
1416 /* Walk through them looking for relaxing opportunities. */
1417 irelend = internal_relocs + sec->reloc_count;
1419 for (irel = internal_relocs; irel < irelend; irel++)
1421 bfd_vma symval;
1422 unsigned char *insn, *gap, *einsn;
1423 bfd_vma pc;
1424 bfd_signed_vma pcrel;
1425 int relax_relocs;
1426 int gap_size;
1427 int new_type;
1428 int posn;
1429 int enc;
1430 EncodingTable *enctbl;
1431 EncodingTable *e;
1433 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1434 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1435 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1436 continue;
1438 srel = irel;
1440 /* There will always be room for the relaxed insn, since it is smaller
1441 than the one it would replace. */
1442 BFD_ASSERT (irel->r_offset < sec->size);
1444 insn = contents + irel->r_offset;
1445 relax_relocs = irel->r_addend % 16;
1447 /* Ok, we only have three relocs we care about, and they're all
1448 fake. The lower four bits of the addend is always the number
1449 of following relocs (hence the qsort above) that are assigned
1450 to this opcode. The next 8 bits of the addend indicates the
1451 number of bytes in the insn. We use the rest of them
1452 ourselves as flags for the more expensive operations (defines
1453 above). The three relocs are:
1455 RL_JUMP: This marks all direct jump insns. We check the
1456 displacement and replace them with shorter jumps if
1457 they're in range. We also use this to find JMP.S
1458 insns and manually shorten them when we delete bytes.
1459 We have to decode these insns to figure out what to
1462 RL_1ADDR: This is a :G or :Q insn, which has a single
1463 "standard" operand. We have to extract the type
1464 field, see if it's a wide displacement, then figure
1465 out if we can replace it with a narrow displacement.
1466 We don't have to decode these insns.
1468 RL_2ADDR: Similarly, but two "standard" operands. Note that
1469 r_addend may still be 1, as standard operands don't
1470 always have displacements. Gas shouldn't give us one
1471 with zero operands, but since we don't know which one
1472 has the displacement, we check them both anyway.
1474 These all point to the beginning of the insn itself, not the
1475 operands.
1477 Note that we only relax one step at a time, relying on the
1478 linker to call us repeatedly. Thus, there is no code for
1479 JMP.A->JMP.B although that will happen in two steps.
1480 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1483 /* Get the value of the symbol referred to by the reloc. Just
1484 in case this is the last reloc in the list, use the RL's
1485 addend to choose between this reloc (no addend) or the next
1486 (yes addend, which means at least one following reloc). */
1487 srel = irel + (relax_relocs ? 1 : 0);
1488 symval = OFFSET_FOR_RELOC (srel);
1490 /* Setting gap_size nonzero is the flag which means "something
1491 shrunk". */
1492 gap_size = 0;
1493 gap = NULL;
1494 new_type = ELF32_R_TYPE(srel->r_info);
1496 pc = sec->output_section->vma + sec->output_offset
1497 + srel->r_offset;
1498 pcrel = symval - pc + srel->r_addend;
1500 if (machine == bfd_mach_m16c)
1502 /* R8C / M16C */
1504 switch (ELF32_R_TYPE(irel->r_info))
1507 case R_M32C_RL_JUMP:
1508 switch (insn[0])
1510 case 0xfe: /* jmp.b */
1511 if (pcrel >= 2 && pcrel <= 9)
1513 /* Relax JMP.B -> JMP.S. We need to get rid of
1514 the following reloc though. */
1515 insn[0] = 0x60 | (pcrel - 2);
1516 new_type = R_M32C_NONE;
1517 irel->r_addend = 0x10;
1518 gap_size = 1;
1519 gap = insn + 1;
1521 break;
1523 case 0xf4: /* jmp.w */
1524 /* 128 is allowed because it will be one byte closer
1525 after relaxing. Likewise for all other pc-rel
1526 jumps. */
1527 if (pcrel <= 128 && pcrel >= -128)
1529 /* Relax JMP.W -> JMP.B */
1530 insn[0] = 0xfe;
1531 insn[1] = 0;
1532 new_type = R_M32C_8_PCREL;
1533 gap_size = 1;
1534 gap = insn + 2;
1536 break;
1538 case 0xfc: /* jmp.a */
1539 if (pcrel <= 32768 && pcrel >= -32768)
1541 /* Relax JMP.A -> JMP.W */
1542 insn[0] = 0xf4;
1543 insn[1] = 0;
1544 insn[2] = 0;
1545 new_type = R_M32C_16_PCREL;
1546 gap_size = 1;
1547 gap = insn + 3;
1549 break;
1551 case 0xfd: /* jsr.a */
1552 if (pcrel <= 32768 && pcrel >= -32768)
1554 /* Relax JSR.A -> JSR.W */
1555 insn[0] = 0xf5;
1556 insn[1] = 0;
1557 insn[2] = 0;
1558 new_type = R_M32C_16_PCREL;
1559 gap_size = 1;
1560 gap = insn + 3;
1562 break;
1564 break;
1566 case R_M32C_RL_2ADDR:
1567 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1569 enctbl = m16c_addr_encodings;
1570 posn = 2;
1571 enc = (insn[1] >> 4) & 0x0f;
1572 e = & enctbl[enc];
1574 if (srel->r_offset == irel->r_offset + posn
1575 && e->new_encoding != enc
1576 && symval <= e->max_disp)
1578 insn[1] &= 0x0f;
1579 insn[1] |= e->new_encoding << 4;
1580 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1581 gap = insn + posn + enctbl[e->new_encoding].bytes;
1582 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1583 break;
1585 if (relax_relocs == 2)
1586 srel ++;
1587 posn += e->bytes;
1589 goto try_1addr_16;
1591 case R_M32C_RL_1ADDR:
1592 /* xxxx xxxx xxxx dest [disp] */
1594 enctbl = m16c_addr_encodings;
1595 posn = 2;
1597 /* Check the opcode for jumps. We know it's safe to
1598 do this because all 2ADDR insns are at least two
1599 bytes long. */
1600 enc = insn[0] * 256 + insn[1];
1601 enc &= 0xfff0;
1602 if (enc == 0x7d20
1603 || enc == 0x7d00
1604 || enc == 0x7d30
1605 || enc == 0x7d10)
1607 enctbl = m16c_jmpaddr_encodings;
1610 try_1addr_16:
1611 /* srel, posn, and enc must be set here. */
1613 symval = OFFSET_FOR_RELOC (srel);
1614 enc = insn[1] & 0x0f;
1615 e = & enctbl[enc];
1617 if (srel->r_offset == irel->r_offset + posn
1618 && e->new_encoding != enc
1619 && symval <= e->max_disp)
1621 insn[1] &= 0xf0;
1622 insn[1] |= e->new_encoding;
1623 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1624 gap = insn + posn + enctbl[e->new_encoding].bytes;
1625 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1626 break;
1629 break;
1631 } /* Ends switch (reloc type) for m16c. */
1633 else /* machine == bfd_mach_m32c */
1635 /* M32CM / M32C */
1637 switch (ELF32_R_TYPE(irel->r_info))
1640 case R_M32C_RL_JUMP:
1641 switch (insn[0])
1643 case 0xbb: /* jmp.b */
1644 if (pcrel >= 2 && pcrel <= 9)
1646 int p = pcrel - 2;
1647 /* Relax JMP.B -> JMP.S. We need to get rid of
1648 the following reloc though. */
1649 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1650 new_type = R_M32C_NONE;
1651 irel->r_addend = 0x10;
1652 gap_size = 1;
1653 gap = insn + 1;
1655 break;
1657 case 0xce: /* jmp.w */
1658 if (pcrel <= 128 && pcrel >= -128)
1660 /* Relax JMP.W -> JMP.B */
1661 insn[0] = 0xbb;
1662 insn[1] = 0;
1663 new_type = R_M32C_8_PCREL;
1664 gap_size = 1;
1665 gap = insn + 2;
1667 break;
1669 case 0xcc: /* jmp.a */
1670 if (pcrel <= 32768 && pcrel >= -32768)
1672 /* Relax JMP.A -> JMP.W */
1673 insn[0] = 0xce;
1674 insn[1] = 0;
1675 insn[2] = 0;
1676 new_type = R_M32C_16_PCREL;
1677 gap_size = 1;
1678 gap = insn + 3;
1680 break;
1682 case 0xcd: /* jsr.a */
1683 if (pcrel <= 32768 && pcrel >= -32768)
1685 /* Relax JSR.A -> JSR.W */
1686 insn[0] = 0xcf;
1687 insn[1] = 0;
1688 insn[2] = 0;
1689 new_type = R_M32C_16_PCREL;
1690 gap_size = 1;
1691 gap = insn + 3;
1693 break;
1695 break;
1697 case R_M32C_RL_2ADDR:
1698 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1700 einsn = insn;
1701 posn = 2;
1702 if (einsn[0] == 1)
1704 /* prefix; remove it as far as the RL reloc is concerned. */
1705 einsn ++;
1706 posn ++;
1709 enctbl = m32c_addr_encodings;
1710 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1711 e = & enctbl[enc];
1713 if (srel->r_offset == irel->r_offset + posn
1714 && e->new_encoding != enc
1715 && symval <= e->max_disp)
1717 einsn[0] &= 0x8f;
1718 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1719 einsn[1] &= 0xcf;
1720 einsn[1] |= (e->new_encoding & 0x03) << 4;
1721 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1722 gap = insn + posn + enctbl[e->new_encoding].bytes;
1723 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1724 break;
1726 if (relax_relocs == 2)
1727 srel ++;
1728 posn += e->bytes;
1730 goto try_1addr_32;
1732 case R_M32C_RL_1ADDR:
1733 /* xxxx DDDx DDxx xxxx [disp] */
1735 einsn = insn;
1736 posn = 2;
1737 if (einsn[0] == 1)
1739 /* prefix; remove it as far as the RL reloc is concerned. */
1740 einsn ++;
1741 posn ++;
1744 enctbl = m32c_addr_encodings;
1746 try_1addr_32:
1747 /* srel, posn, and enc must be set here. */
1749 symval = OFFSET_FOR_RELOC (srel);
1750 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1751 e = & enctbl[enc];
1753 if (srel->r_offset == irel->r_offset + posn
1754 && e->new_encoding != enc
1755 && symval <= e->max_disp)
1757 einsn[0] &= 0xf1;
1758 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1759 einsn[1] &= 0x3f;
1760 einsn[1] |= (e->new_encoding & 0x03) << 6;
1761 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1762 gap = insn + posn + enctbl[e->new_encoding].bytes;
1763 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1764 break;
1767 break;
1769 } /* Ends switch (reloc type) for m32c. */
1772 if (gap_size == 0)
1773 continue;
1775 *again = TRUE;
1777 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1779 /* Note that we've changed the relocs, section contents, etc. */
1780 elf_section_data (sec)->relocs = internal_relocs;
1781 free_relocs = NULL;
1783 elf_section_data (sec)->this_hdr.contents = contents;
1784 free_contents = NULL;
1786 symtab_hdr->contents = (bfd_byte *) intsyms;
1787 free_intsyms = NULL;
1789 bytes_saved += gap_size;
1791 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1792 goto error_return;
1794 } /* next relocation */
1796 if (free_relocs != NULL)
1798 free (free_relocs);
1799 free_relocs = NULL;
1802 if (free_contents != NULL)
1804 if (! link_info->keep_memory)
1805 free (free_contents);
1806 /* Cache the section contents for elf_link_input_bfd. */
1807 else
1808 elf_section_data (sec)->this_hdr.contents = contents;
1810 free_contents = NULL;
1813 if (shndx_buf != NULL)
1815 shndx_hdr->contents = NULL;
1816 free (shndx_buf);
1819 if (free_intsyms != NULL)
1821 if (! link_info->keep_memory)
1822 free (free_intsyms);
1823 /* Cache the symbols for elf_link_input_bfd. */
1824 else
1826 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1829 free_intsyms = NULL;
1832 return TRUE;
1834 error_return:
1835 if (free_relocs != NULL)
1836 free (free_relocs);
1837 if (free_contents != NULL)
1838 free (free_contents);
1839 if (shndx_buf != NULL)
1841 shndx_hdr->contents = NULL;
1842 free (shndx_buf);
1844 if (free_intsyms != NULL)
1845 free (free_intsyms);
1846 return FALSE;
1849 /* Delete some bytes from a section while relaxing. */
1851 static bfd_boolean
1852 m32c_elf_relax_delete_bytes
1853 (bfd * abfd,
1854 asection * sec,
1855 bfd_vma addr,
1856 int count)
1858 Elf_Internal_Shdr *symtab_hdr;
1859 Elf_Internal_Shdr *shndx_hdr;
1860 int sec_shndx;
1861 bfd_byte *contents;
1862 Elf_Internal_Rela *irel;
1863 Elf_Internal_Rela *irelend;
1864 Elf_Internal_Rela *irelalign;
1865 bfd_vma toaddr;
1866 Elf_Internal_Sym *isym;
1867 Elf_Internal_Sym *isymend;
1868 Elf_Internal_Sym *intsyms;
1869 Elf_External_Sym_Shndx *shndx_buf;
1870 Elf_External_Sym_Shndx *shndx;
1871 struct elf_link_hash_entry ** sym_hashes;
1872 struct elf_link_hash_entry ** end_hashes;
1873 unsigned int symcount;
1875 contents = elf_section_data (sec)->this_hdr.contents;
1877 /* The deletion must stop at the next ALIGN reloc for an aligment
1878 power larger than the number of bytes we are deleting. */
1879 irelalign = NULL;
1880 toaddr = sec->size;
1882 irel = elf_section_data (sec)->relocs;
1883 irelend = irel + sec->reloc_count;
1885 /* Actually delete the bytes. */
1886 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1887 sec->size -= count;
1889 /* Adjust all the relocs. */
1890 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1892 /* Get the new reloc address. */
1893 if (irel->r_offset > addr && irel->r_offset < toaddr)
1894 irel->r_offset -= count;
1896 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1897 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1898 && irel->r_offset + 1 < addr
1899 && irel->r_offset + 7 > addr)
1901 bfd_vma disp;
1902 unsigned char *insn = &contents[irel->r_offset];
1903 disp = *insn;
1904 /* This is a JMP.S, which we have to manually update. */
1905 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1907 if ((*insn & 0xf8) != 0x60)
1908 continue;
1909 disp = (disp & 7);
1911 else
1913 if ((*insn & 0xce) != 0x4a)
1914 continue;
1915 disp = ((disp & 0x30) >> 3) | (disp & 1);
1917 if (irel->r_offset + disp + 2 >= addr+count)
1919 disp -= count;
1920 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1922 *insn = (*insn & 0xf8) | disp;
1924 else
1926 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1932 /* Adjust the local symbols defined in this section. */
1933 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1934 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1935 isym = intsyms;
1936 isymend = isym + symtab_hdr->sh_info;
1938 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1939 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1940 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1941 shndx = shndx_buf;
1943 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1946 if ((int) isym->st_shndx == sec_shndx
1947 && isym->st_value > addr
1948 && isym->st_value < toaddr)
1950 isym->st_value -= count;
1954 /* Now adjust the global symbols defined in this section. */
1955 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1956 - symtab_hdr->sh_info);
1957 sym_hashes = elf_sym_hashes (abfd);
1958 // sym_hashes += symtab_hdr->sh_info;
1959 end_hashes = sym_hashes + symcount;
1961 for (; sym_hashes < end_hashes; sym_hashes ++)
1963 struct elf_link_hash_entry * sym_hash = * sym_hashes;
1965 if (sym_hash &&
1966 ( sym_hash->root.type == bfd_link_hash_defined
1967 || sym_hash->root.type == bfd_link_hash_defweak)
1968 && sym_hash->root.u.def.section == sec
1969 && sym_hash->root.u.def.value > addr
1970 && sym_hash->root.u.def.value < toaddr)
1972 sym_hash->root.u.def.value -= count;
1976 return TRUE;
1980 #define ELF_ARCH bfd_arch_m32c
1981 #define ELF_MACHINE_CODE EM_M32C
1982 #define ELF_MAXPAGESIZE 0x1000
1984 #if 0
1985 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
1986 #define TARGET_BIG_NAME "elf32-m32c"
1987 #else
1988 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
1989 #define TARGET_LITTLE_NAME "elf32-m32c"
1990 #endif
1992 #define elf_info_to_howto_rel NULL
1993 #define elf_info_to_howto m32c_info_to_howto_rela
1994 #define elf_backend_object_p m32c_elf_object_p
1995 #define elf_backend_relocate_section m32c_elf_relocate_section
1996 #define elf_backend_check_relocs m32c_elf_check_relocs
1997 #define elf_backend_object_p m32c_elf_object_p
1998 #define elf_symbol_leading_char ('_')
1999 #define elf_backend_always_size_sections \
2000 m32c_elf_always_size_sections
2001 #define elf_backend_finish_dynamic_sections \
2002 m32c_elf_finish_dynamic_sections
2004 #define elf_backend_can_gc_sections 1
2006 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2007 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2008 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2009 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2010 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2012 #include "elf32-target.h"