ld/testsuite/
[binutils.git] / bfd / elf32-m32c.c
blob50538c9e08ca69d0439a114a53ca8cc3d91872dd
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 HOWTO (R_M32C_16, /* type */
64 0, /* rightshift */
65 1, /* size (0 = byte, 1 = short, 2 = long) */
66 16, /* bitsize */
67 FALSE, /* pc_relative */
68 0, /* bitpos */
69 complain_overflow_bitfield, /* complain_on_overflow */
70 bfd_elf_generic_reloc, /* special_function */
71 "R_M32C_16", /* name */
72 FALSE, /* partial_inplace */
73 0, /* src_mask */
74 0xffff, /* dst_mask */
75 FALSE), /* pcrel_offset */
77 HOWTO (R_M32C_24, /* type */
78 0, /* rightshift */
79 2, /* size (0 = byte, 1 = short, 2 = long) */
80 24, /* bitsize */
81 FALSE, /* pc_relative */
82 0, /* bitpos */
83 complain_overflow_bitfield, /* complain_on_overflow */
84 bfd_elf_generic_reloc, /* special_function */
85 "R_M32C_24", /* name */
86 FALSE, /* partial_inplace */
87 0, /* src_mask */
88 0xffffff, /* dst_mask */
89 FALSE), /* pcrel_offset */
91 HOWTO (R_M32C_32, /* type */
92 0, /* rightshift */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
94 32, /* bitsize */
95 FALSE, /* pc_relative */
96 0, /* bitpos */
97 complain_overflow_bitfield, /* complain_on_overflow */
98 bfd_elf_generic_reloc, /* special_function */
99 "R_M32C_32", /* name */
100 FALSE, /* partial_inplace */
101 0, /* src_mask */
102 0xffffffff, /* dst_mask */
103 FALSE), /* pcrel_offset */
105 HOWTO (R_M32C_8_PCREL, /* type */
106 0, /* rightshift */
107 0, /* size (0 = byte, 1 = short, 2 = long) */
108 8, /* bitsize */
109 TRUE, /* pc_relative */
110 0, /* bitpos */
111 complain_overflow_signed, /* complain_on_overflow */
112 bfd_elf_generic_reloc, /* special_function */
113 "R_M32C_8_PCREL", /* name */
114 FALSE, /* partial_inplace */
115 0, /* src_mask */
116 0xff, /* dst_mask */
117 TRUE), /* pcrel_offset */
119 HOWTO (R_M32C_16_PCREL, /* type */
120 0, /* rightshift */
121 1, /* size (0 = byte, 1 = short, 2 = long) */
122 16, /* bitsize */
123 TRUE, /* pc_relative */
124 0, /* bitpos */
125 complain_overflow_signed, /* complain_on_overflow */
126 bfd_elf_generic_reloc, /* special_function */
127 "R_M32C_16_PCREL", /* name */
128 FALSE, /* partial_inplace */
129 0, /* src_mask */
130 0xffff, /* dst_mask */
131 TRUE), /* pcrel_offset */
133 HOWTO (R_M32C_8, /* type */
134 0, /* rightshift */
135 0, /* size (0 = byte, 1 = short, 2 = long) */
136 8, /* bitsize */
137 FALSE, /* pc_relative */
138 0, /* bitpos */
139 complain_overflow_unsigned, /* complain_on_overflow */
140 bfd_elf_generic_reloc, /* special_function */
141 "R_M32C_8", /* name */
142 FALSE, /* partial_inplace */
143 0, /* src_mask */
144 0xff, /* dst_mask */
145 FALSE), /* pcrel_offset */
147 HOWTO (R_M32C_LO16, /* type */
148 0, /* rightshift */
149 1, /* size (0 = byte, 1 = short, 2 = long) */
150 16, /* bitsize */
151 FALSE, /* pc_relative */
152 0, /* bitpos */
153 complain_overflow_dont, /* complain_on_overflow */
154 bfd_elf_generic_reloc, /* special_function */
155 "R_M32C_LO16", /* name */
156 FALSE, /* partial_inplace */
157 0, /* src_mask */
158 0xffff, /* dst_mask */
159 FALSE), /* pcrel_offset */
161 HOWTO (R_M32C_HI8, /* type */
162 0, /* rightshift */
163 0, /* size (0 = byte, 1 = short, 2 = long) */
164 8, /* bitsize */
165 FALSE, /* pc_relative */
166 0, /* bitpos */
167 complain_overflow_dont, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_M32C_HI8", /* name */
170 FALSE, /* partial_inplace */
171 0, /* src_mask */
172 0xff, /* dst_mask */
173 FALSE), /* pcrel_offset */
175 HOWTO (R_M32C_HI16, /* type */
176 0, /* rightshift */
177 1, /* size (0 = byte, 1 = short, 2 = long) */
178 16, /* bitsize */
179 FALSE, /* pc_relative */
180 0, /* bitpos */
181 complain_overflow_dont, /* complain_on_overflow */
182 bfd_elf_generic_reloc, /* special_function */
183 "R_M32C_HI16", /* name */
184 FALSE, /* partial_inplace */
185 0, /* src_mask */
186 0xffff, /* dst_mask */
187 FALSE), /* pcrel_offset */
189 HOWTO (R_M32C_RL_JUMP, /* type */
190 0, /* rightshift */
191 0, /* size (0 = byte, 1 = short, 2 = long) */
192 0, /* bitsize */
193 FALSE, /* pc_relative */
194 0, /* bitpos */
195 complain_overflow_signed, /* complain_on_overflow */
196 bfd_elf_generic_reloc, /* special_function */
197 "R_M32C_RL_JUMP", /* name */
198 FALSE, /* partial_inplace */
199 0, /* src_mask */
200 0, /* dst_mask */
201 FALSE), /* pcrel_offset */
203 HOWTO (R_M32C_RL_1ADDR, /* type */
204 0, /* rightshift */
205 0, /* size (0 = byte, 1 = short, 2 = long) */
206 0, /* bitsize */
207 FALSE, /* pc_relative */
208 0, /* bitpos */
209 complain_overflow_signed, /* complain_on_overflow */
210 bfd_elf_generic_reloc, /* special_function */
211 "R_M32C_RL_1ADDR", /* name */
212 FALSE, /* partial_inplace */
213 0, /* src_mask */
214 0, /* dst_mask */
215 FALSE), /* pcrel_offset */
217 HOWTO (R_M32C_RL_2ADDR, /* type */
218 0, /* rightshift */
219 0, /* size (0 = byte, 1 = short, 2 = long) */
220 0, /* bitsize */
221 FALSE, /* pc_relative */
222 0, /* bitpos */
223 complain_overflow_signed, /* complain_on_overflow */
224 bfd_elf_generic_reloc, /* special_function */
225 "R_M32C_RL_2ADDR", /* name */
226 FALSE, /* partial_inplace */
227 0, /* src_mask */
228 0, /* dst_mask */
229 FALSE), /* pcrel_offset */
233 /* Map BFD reloc types to M32C ELF reloc types. */
235 struct m32c_reloc_map
237 bfd_reloc_code_real_type bfd_reloc_val;
238 unsigned int m32c_reloc_val;
241 static const struct m32c_reloc_map m32c_reloc_map [] =
243 { BFD_RELOC_NONE, R_M32C_NONE },
244 { BFD_RELOC_16, R_M32C_16 },
245 { BFD_RELOC_24, R_M32C_24 },
246 { BFD_RELOC_32, R_M32C_32 },
247 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
248 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
249 { BFD_RELOC_8, R_M32C_8 },
250 { BFD_RELOC_LO16, R_M32C_LO16 },
251 { BFD_RELOC_HI16, R_M32C_HI16 },
252 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
253 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
254 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
255 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
258 static reloc_howto_type *
259 m32c_reloc_type_lookup
260 (bfd * abfd ATTRIBUTE_UNUSED,
261 bfd_reloc_code_real_type code)
263 unsigned int i;
265 for (i = ARRAY_SIZE (m32c_reloc_map); --i;)
266 if (m32c_reloc_map [i].bfd_reloc_val == code)
267 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
269 return NULL;
272 /* Set the howto pointer for an M32C ELF reloc. */
274 static void
275 m32c_info_to_howto_rela
276 (bfd * abfd ATTRIBUTE_UNUSED,
277 arelent * cache_ptr,
278 Elf_Internal_Rela * dst)
280 unsigned int r_type;
282 r_type = ELF32_R_TYPE (dst->r_info);
283 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
284 cache_ptr->howto = & m32c_elf_howto_table [r_type];
289 /* Relocate an M32C ELF section.
290 There is some attempt to make this function usable for many architectures,
291 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
292 if only to serve as a learning tool.
294 The RELOCATE_SECTION function is called by the new ELF backend linker
295 to handle the relocations for a section.
297 The relocs are always passed as Rela structures; if the section
298 actually uses Rel structures, the r_addend field will always be
299 zero.
301 This function is responsible for adjusting the section contents as
302 necessary, and (if using Rela relocs and generating a relocatable
303 output file) adjusting the reloc addend as necessary.
305 This function does not have to worry about setting the reloc
306 address or the reloc symbol index.
308 LOCAL_SYMS is a pointer to the swapped in local symbols.
310 LOCAL_SECTIONS is an array giving the section in the input file
311 corresponding to the st_shndx field of each local symbol.
313 The global hash table entry for the global symbols can be found
314 via elf_sym_hashes (input_bfd).
316 When generating relocatable output, this function must handle
317 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
318 going to be the section symbol corresponding to the output
319 section, which means that the addend must be adjusted
320 accordingly. */
322 static bfd_boolean
323 m32c_elf_relocate_section
324 (bfd * output_bfd ATTRIBUTE_UNUSED,
325 struct bfd_link_info * info,
326 bfd * input_bfd,
327 asection * input_section,
328 bfd_byte * contents,
329 Elf_Internal_Rela * relocs,
330 Elf_Internal_Sym * local_syms,
331 asection ** local_sections)
333 Elf_Internal_Shdr * symtab_hdr;
334 struct elf_link_hash_entry ** sym_hashes;
335 Elf_Internal_Rela * rel;
336 Elf_Internal_Rela * relend;
337 bfd *dynobj;
338 asection *splt;
340 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
341 sym_hashes = elf_sym_hashes (input_bfd);
342 relend = relocs + input_section->reloc_count;
344 dynobj = elf_hash_table (info)->dynobj;
345 splt = NULL;
346 if (dynobj != NULL)
347 splt = bfd_get_section_by_name (dynobj, ".plt");
349 for (rel = relocs; rel < relend; rel ++)
351 reloc_howto_type * howto;
352 unsigned long r_symndx;
353 Elf_Internal_Sym * sym;
354 asection * sec;
355 struct elf_link_hash_entry * h;
356 bfd_vma relocation;
357 bfd_reloc_status_type r;
358 const char * name = NULL;
359 int r_type;
361 r_type = ELF32_R_TYPE (rel->r_info);
363 /* These are only used for relaxing; we don't actually relocate
364 anything with them, so skip them. */
365 if (r_type == R_M32C_RL_JUMP
366 || r_type == R_M32C_RL_1ADDR
367 || r_type == R_M32C_RL_2ADDR)
368 continue;
370 r_symndx = ELF32_R_SYM (rel->r_info);
372 if (info->relocatable)
374 /* This is a relocatable link. We don't have to change
375 anything, unless the reloc is against a section symbol,
376 in which case we have to adjust according to where the
377 section symbol winds up in the output section. */
378 if (r_symndx < symtab_hdr->sh_info)
380 sym = local_syms + r_symndx;
382 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
384 sec = local_sections [r_symndx];
385 rel->r_addend += sec->output_offset + sym->st_value;
389 continue;
392 /* This is a final link. */
393 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
394 h = NULL;
395 sym = NULL;
396 sec = NULL;
398 if (r_symndx < symtab_hdr->sh_info)
400 sym = local_syms + r_symndx;
401 sec = local_sections [r_symndx];
402 relocation = (sec->output_section->vma
403 + sec->output_offset
404 + sym->st_value);
406 name = bfd_elf_string_from_elf_section
407 (input_bfd, symtab_hdr->sh_link, sym->st_name);
408 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
410 else
412 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
414 while (h->root.type == bfd_link_hash_indirect
415 || h->root.type == bfd_link_hash_warning)
416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
418 name = h->root.root.string;
420 if (h->root.type == bfd_link_hash_defined
421 || h->root.type == bfd_link_hash_defweak)
423 sec = h->root.u.def.section;
424 relocation = (h->root.u.def.value
425 + sec->output_section->vma
426 + sec->output_offset);
428 else if (h->root.type == bfd_link_hash_undefweak)
430 relocation = 0;
432 else
434 if (! ((*info->callbacks->undefined_symbol)
435 (info, h->root.root.string, input_bfd,
436 input_section, rel->r_offset, TRUE)))
437 return FALSE;
438 relocation = 0;
442 switch (ELF32_R_TYPE (rel->r_info))
444 case R_M32C_16:
446 bfd_vma *plt_offset;
448 if (h != NULL)
449 plt_offset = &h->plt.offset;
450 else
451 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
453 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
454 relocation, *plt_offset);*/
455 if (relocation <= 0xffff)
457 /* If the symbol is in range for a 16-bit address, we should
458 have deallocated the plt entry in relax_section. */
459 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
461 else
463 /* If the symbol is out of range for a 16-bit address,
464 we must have allocated a plt entry. */
465 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
467 /* If this is the first time we've processed this symbol,
468 fill in the plt entry with the correct symbol address. */
469 if ((*plt_offset & 1) == 0)
471 unsigned int x;
473 x = 0x000000fc; /* jmpf */
474 x |= (relocation << 8) & 0xffffff00;
475 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
476 *plt_offset |= 1;
479 relocation = (splt->output_section->vma
480 + splt->output_offset
481 + (*plt_offset & -2));
482 if (name)
484 char *newname = bfd_malloc (strlen(name)+5);
485 strcpy (newname, name);
486 strcat(newname, ".plt");
487 _bfd_generic_link_add_one_symbol (info,
488 input_bfd,
489 newname,
490 BSF_FUNCTION | BSF_WEAK,
491 splt,
492 (*plt_offset & -2),
500 break;
502 case R_M32C_HI8:
503 case R_M32C_HI16:
504 relocation >>= 16;
505 break;
508 #if 0
509 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
510 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
511 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
512 relocation, rel->r_addend);
514 int i;
515 for (i=0; i<4; i++)
516 printf (" %02x", contents[rel->r_offset+i]);
517 printf ("\n");
519 #endif
520 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
521 contents, rel->r_offset, relocation,
522 rel->r_addend);
524 if (r != bfd_reloc_ok)
526 const char * msg = (const char *) NULL;
528 switch (r)
530 case bfd_reloc_overflow:
531 r = info->callbacks->reloc_overflow
532 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
533 input_bfd, input_section, rel->r_offset);
534 break;
536 case bfd_reloc_undefined:
537 r = info->callbacks->undefined_symbol
538 (info, name, input_bfd, input_section, rel->r_offset,
539 TRUE);
540 break;
542 case bfd_reloc_outofrange:
543 msg = _("internal error: out of range error");
544 break;
546 case bfd_reloc_notsupported:
547 msg = _("internal error: unsupported relocation error");
548 break;
550 case bfd_reloc_dangerous:
551 msg = _("internal error: dangerous relocation");
552 break;
554 default:
555 msg = _("internal error: unknown error");
556 break;
559 if (msg)
560 r = info->callbacks->warning
561 (info, msg, name, input_bfd, input_section, rel->r_offset);
563 if (! r)
564 return FALSE;
568 return TRUE;
571 /* We support 16-bit pointers to code above 64k by generating a thunk
572 below 64k containing a JMP instruction to the final address. */
574 static bfd_boolean
575 m32c_elf_check_relocs
576 (bfd * abfd,
577 struct bfd_link_info * info,
578 asection * sec,
579 const Elf_Internal_Rela * relocs)
581 Elf_Internal_Shdr * symtab_hdr;
582 struct elf_link_hash_entry ** sym_hashes;
583 struct elf_link_hash_entry ** sym_hashes_end;
584 const Elf_Internal_Rela * rel;
585 const Elf_Internal_Rela * rel_end;
586 bfd_vma *local_plt_offsets;
587 asection *splt;
588 bfd *dynobj;
590 if (info->relocatable)
591 return TRUE;
593 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
594 sym_hashes = elf_sym_hashes (abfd);
595 local_plt_offsets = elf_local_got_offsets (abfd);
596 splt = NULL;
597 dynobj = elf_hash_table(info)->dynobj;
599 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
600 if (!elf_bad_symtab (abfd))
601 sym_hashes_end -= symtab_hdr->sh_info;
603 rel_end = relocs + sec->reloc_count;
604 for (rel = relocs; rel < rel_end; rel++)
606 struct elf_link_hash_entry *h;
607 unsigned long r_symndx;
608 bfd_vma *offset;
610 r_symndx = ELF32_R_SYM (rel->r_info);
611 if (r_symndx < symtab_hdr->sh_info)
612 h = NULL;
613 else
615 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
616 while (h->root.type == bfd_link_hash_indirect
617 || h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621 switch (ELF32_R_TYPE (rel->r_info))
623 /* This relocation describes a 16-bit pointer to a function.
624 We may need to allocate a thunk in low memory; reserve memory
625 for it now. */
626 case R_M32C_16:
627 if (dynobj == NULL)
628 elf_hash_table (info)->dynobj = dynobj = abfd;
629 if (splt == NULL)
631 splt = bfd_get_section_by_name (dynobj, ".plt");
632 if (splt == NULL)
634 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
635 | SEC_IN_MEMORY | SEC_LINKER_CREATED
636 | SEC_READONLY | SEC_CODE);
637 splt = bfd_make_section_with_flags (dynobj, ".plt", flags);
638 if (splt == NULL
639 || ! bfd_set_section_alignment (dynobj, splt, 1))
640 return FALSE;
644 if (h != NULL)
645 offset = &h->plt.offset;
646 else
648 if (local_plt_offsets == NULL)
650 size_t size;
651 unsigned int i;
653 size = symtab_hdr->sh_info * sizeof (bfd_vma);
654 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
655 if (local_plt_offsets == NULL)
656 return FALSE;
657 elf_local_got_offsets (abfd) = local_plt_offsets;
659 for (i = 0; i < symtab_hdr->sh_info; i++)
660 local_plt_offsets[i] = (bfd_vma) -1;
662 offset = &local_plt_offsets[r_symndx];
665 if (*offset == (bfd_vma) -1)
667 *offset = splt->size;
668 splt->size += 4;
670 break;
674 return TRUE;
677 /* This must exist if dynobj is ever set. */
679 static bfd_boolean
680 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
681 struct bfd_link_info *info)
683 bfd *dynobj;
684 asection *splt;
686 /* As an extra sanity check, verify that all plt entries have
687 been filled in. */
689 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
690 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
692 bfd_byte *contents = splt->contents;
693 unsigned int i, size = splt->size;
694 for (i = 0; i < size; i += 4)
696 unsigned int x = bfd_get_32 (dynobj, contents + i);
697 BFD_ASSERT (x != 0);
701 return TRUE;
704 static bfd_boolean
705 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
706 struct bfd_link_info *info)
708 bfd *dynobj;
709 asection *splt;
711 if (info->relocatable)
712 return TRUE;
714 dynobj = elf_hash_table (info)->dynobj;
715 if (dynobj == NULL)
716 return TRUE;
718 splt = bfd_get_section_by_name (dynobj, ".plt");
719 BFD_ASSERT (splt != NULL);
721 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
722 if (splt->contents == NULL)
723 return FALSE;
725 return TRUE;
728 /* Function to set the ELF flag bits. */
730 static bfd_boolean
731 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
733 elf_elfheader (abfd)->e_flags = flags;
734 elf_flags_init (abfd) = TRUE;
735 return TRUE;
738 /* Merge backend specific data from an object file to the output
739 object file when linking. */
741 static bfd_boolean
742 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
744 flagword old_flags, old_partial;
745 flagword new_flags, new_partial;
746 bfd_boolean error = FALSE;
747 char new_opt[80];
748 char old_opt[80];
750 new_opt[0] = old_opt[0] = '\0';
751 new_flags = elf_elfheader (ibfd)->e_flags;
752 old_flags = elf_elfheader (obfd)->e_flags;
754 #ifdef DEBUG
755 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
756 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
757 bfd_get_filename (ibfd));
758 #endif
760 if (!elf_flags_init (obfd))
762 /* First call, no flags set. */
763 elf_flags_init (obfd) = TRUE;
764 elf_elfheader (obfd)->e_flags = new_flags;
767 else if (new_flags == old_flags)
768 /* Compatible flags are ok. */
771 else /* Possibly incompatible flags. */
773 /* Warn if different cpu is used (allow a specific cpu to override
774 the generic cpu). */
775 new_partial = (new_flags & EF_M32C_CPU_MASK);
776 old_partial = (old_flags & EF_M32C_CPU_MASK);
777 if (new_partial == old_partial)
780 else
782 switch (new_partial)
784 default: strcat (new_opt, " -m16c"); break;
785 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
786 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
789 switch (old_partial)
791 default: strcat (old_opt, " -m16c"); break;
792 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
793 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
797 /* Print out any mismatches from above. */
798 if (new_opt[0])
800 error = TRUE;
801 (*_bfd_error_handler)
802 (_("%s: compiled with %s and linked with modules compiled with %s"),
803 bfd_get_filename (ibfd), new_opt, old_opt);
806 new_flags &= ~ EF_M32C_ALL_FLAGS;
807 old_flags &= ~ EF_M32C_ALL_FLAGS;
809 /* Warn about any other mismatches. */
810 if (new_flags != old_flags)
812 error = TRUE;
813 (*_bfd_error_handler)
814 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
815 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
819 if (error)
820 bfd_set_error (bfd_error_bad_value);
822 return !error;
826 static bfd_boolean
827 m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
829 FILE *file = (FILE *) ptr;
830 flagword flags;
832 BFD_ASSERT (abfd != NULL && ptr != NULL);
834 /* Print normal ELF private data. */
835 _bfd_elf_print_private_bfd_data (abfd, ptr);
837 flags = elf_elfheader (abfd)->e_flags;
838 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
840 switch (flags & EF_M32C_CPU_MASK)
842 default: break;
843 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
844 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
847 fputc ('\n', file);
848 return TRUE;
851 /* Return the MACH for an e_flags value. */
853 static int
854 elf32_m32c_machine (bfd *abfd)
856 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
858 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
859 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
862 return bfd_mach_m16c;
865 static bfd_boolean
866 m32c_elf_object_p (bfd *abfd)
868 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
869 elf32_m32c_machine (abfd));
870 return TRUE;
874 #ifdef DEBUG
875 void
876 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
878 size_t locsymcount;
879 Elf_Internal_Sym *isymbuf;
880 Elf_Internal_Sym *isymend;
881 Elf_Internal_Sym *isym;
882 Elf_Internal_Shdr *symtab_hdr;
883 bfd_boolean free_internal = 0, free_external = 0;
884 char * st_info_str;
885 char * st_info_stb_str;
886 char * st_other_str;
887 char * st_shndx_str;
889 if (! internal_syms)
891 internal_syms = bfd_malloc (1000);
892 free_internal = 1;
894 if (! external_syms)
896 external_syms = bfd_malloc (1000);
897 free_external = 1;
900 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
901 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
902 if (free_internal)
903 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
904 symtab_hdr->sh_info, 0,
905 internal_syms, external_syms, NULL);
906 else
907 isymbuf = internal_syms;
908 isymend = isymbuf + locsymcount;
910 for (isym = isymbuf ; isym < isymend ; isym++)
912 switch (ELF_ST_TYPE (isym->st_info))
914 case STT_FUNC: st_info_str = "STT_FUNC";
915 case STT_SECTION: st_info_str = "STT_SECTION";
916 case STT_FILE: st_info_str = "STT_FILE";
917 case STT_OBJECT: st_info_str = "STT_OBJECT";
918 case STT_TLS: st_info_str = "STT_TLS";
919 default: st_info_str = "";
921 switch (ELF_ST_BIND (isym->st_info))
923 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
924 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
925 default: st_info_stb_str = "";
927 switch (ELF_ST_VISIBILITY (isym->st_other))
929 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
930 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
931 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
932 default: st_other_str = "";
934 switch (isym->st_shndx)
936 case SHN_ABS: st_shndx_str = "SHN_ABS";
937 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
938 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
939 default: st_shndx_str = "";
942 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
943 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
944 isym,
945 (unsigned long) isym->st_value,
946 (unsigned long) isym->st_size,
947 isym->st_name,
948 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
949 isym->st_name),
950 isym->st_info, st_info_str, st_info_stb_str,
951 isym->st_other, st_other_str,
952 isym->st_shndx, st_shndx_str);
954 if (free_internal)
955 free (internal_syms);
956 if (free_external)
957 free (external_syms);
960 char *
961 m32c_get_reloc (long reloc)
963 if (0 <= reloc && reloc < R_M32C_max)
964 return m32c_elf_howto_table[reloc].name;
965 else
966 return "";
968 #endif /* DEBUG */
970 /* Handle relaxing. */
972 /* A subroutine of m32c_elf_relax_section. If the global symbol H
973 is within the low 64k, remove any entry for it in the plt. */
975 struct relax_plt_data
977 asection *splt;
978 bfd_boolean *again;
981 static bfd_boolean
982 m32c_relax_plt_check (struct elf_link_hash_entry *h,
983 PTR xdata)
985 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
987 if (h->root.type == bfd_link_hash_warning)
988 h = (struct elf_link_hash_entry *) h->root.u.i.link;
990 if (h->plt.offset != (bfd_vma) -1)
992 bfd_vma address;
994 if (h->root.type == bfd_link_hash_undefined
995 || h->root.type == bfd_link_hash_undefweak)
996 address = 0;
997 else
998 address = (h->root.u.def.section->output_section->vma
999 + h->root.u.def.section->output_offset
1000 + h->root.u.def.value);
1002 if (address <= 0xffff)
1004 h->plt.offset = -1;
1005 data->splt->size -= 4;
1006 *data->again = TRUE;
1010 return TRUE;
1013 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1014 previously had a plt entry, give it a new entry offset. */
1016 static bfd_boolean
1017 m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1018 PTR xdata)
1020 bfd_vma *entry = (bfd_vma *) xdata;
1022 if (h->root.type == bfd_link_hash_warning)
1023 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1025 if (h->plt.offset != (bfd_vma) -1)
1027 h->plt.offset = *entry;
1028 *entry += 4;
1031 return TRUE;
1034 static bfd_boolean
1035 m32c_elf_relax_plt_section (bfd *dynobj,
1036 asection *splt,
1037 struct bfd_link_info *info,
1038 bfd_boolean *again)
1040 struct relax_plt_data relax_plt_data;
1041 bfd *ibfd;
1043 /* Assume nothing changes. */
1044 *again = FALSE;
1046 if (info->relocatable)
1047 return TRUE;
1049 /* We only relax the .plt section at the moment. */
1050 if (dynobj != elf_hash_table (info)->dynobj
1051 || strcmp (splt->name, ".plt") != 0)
1052 return TRUE;
1054 /* Quick check for an empty plt. */
1055 if (splt->size == 0)
1056 return TRUE;
1058 /* Map across all global symbols; see which ones happen to
1059 fall in the low 64k. */
1060 relax_plt_data.splt = splt;
1061 relax_plt_data.again = again;
1062 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1063 &relax_plt_data);
1065 /* Likewise for local symbols, though that's somewhat less convenient
1066 as we have to walk the list of input bfds and swap in symbol data. */
1067 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1069 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1070 Elf_Internal_Shdr *symtab_hdr;
1071 Elf_Internal_Sym *isymbuf = NULL;
1072 unsigned int idx;
1074 if (! local_plt_offsets)
1075 continue;
1077 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1078 if (symtab_hdr->sh_info != 0)
1080 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1081 if (isymbuf == NULL)
1082 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1083 symtab_hdr->sh_info, 0,
1084 NULL, NULL, NULL);
1085 if (isymbuf == NULL)
1086 return FALSE;
1089 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1091 Elf_Internal_Sym *isym;
1092 asection *tsec;
1093 bfd_vma address;
1095 if (local_plt_offsets[idx] == (bfd_vma) -1)
1096 continue;
1098 isym = &isymbuf[idx];
1099 if (isym->st_shndx == SHN_UNDEF)
1100 continue;
1101 else if (isym->st_shndx == SHN_ABS)
1102 tsec = bfd_abs_section_ptr;
1103 else if (isym->st_shndx == SHN_COMMON)
1104 tsec = bfd_com_section_ptr;
1105 else
1106 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1108 address = (tsec->output_section->vma
1109 + tsec->output_offset
1110 + isym->st_value);
1111 if (address <= 0xffff)
1113 local_plt_offsets[idx] = -1;
1114 splt->size -= 4;
1115 *again = TRUE;
1119 if (isymbuf != NULL
1120 && symtab_hdr->contents != (unsigned char *) isymbuf)
1122 if (! info->keep_memory)
1123 free (isymbuf);
1124 else
1126 /* Cache the symbols for elf_link_input_bfd. */
1127 symtab_hdr->contents = (unsigned char *) isymbuf;
1132 /* If we changed anything, walk the symbols again to reallocate
1133 .plt entry addresses. */
1134 if (*again && splt->size > 0)
1136 bfd_vma entry = 0;
1138 elf_link_hash_traverse (elf_hash_table (info),
1139 m32c_relax_plt_realloc, &entry);
1141 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1143 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1144 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1145 unsigned int idx;
1147 if (! local_plt_offsets)
1148 continue;
1150 for (idx = 0; idx < nlocals; ++idx)
1151 if (local_plt_offsets[idx] != (bfd_vma) -1)
1153 local_plt_offsets[idx] = entry;
1154 entry += 4;
1159 return TRUE;
1162 static int
1163 compare_reloc (const void *e1, const void *e2)
1165 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1166 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1168 if (i1->r_offset == i2->r_offset)
1169 return 0;
1170 else
1171 return i1->r_offset < i2->r_offset ? -1 : 1;
1174 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1175 static bfd_vma
1176 m32c_offset_for_reloc (bfd *abfd,
1177 Elf_Internal_Rela *rel,
1178 Elf_Internal_Shdr *symtab_hdr,
1179 Elf_External_Sym_Shndx *shndx_buf,
1180 Elf_Internal_Sym *intsyms)
1182 bfd_vma symval;
1184 /* Get the value of the symbol referred to by the reloc. */
1185 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1187 /* A local symbol. */
1188 Elf_Internal_Sym *isym;
1189 Elf_External_Sym_Shndx *shndx;
1190 asection *ssec;
1193 isym = intsyms + ELF32_R_SYM (rel->r_info);
1194 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1195 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0);
1197 symval = isym->st_value;
1198 if (ssec)
1199 symval += ssec->output_section->vma
1200 + ssec->output_offset;
1202 else
1204 unsigned long indx;
1205 struct elf_link_hash_entry *h;
1207 /* An external symbol. */
1208 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1209 h = elf_sym_hashes (abfd)[indx];
1210 BFD_ASSERT (h != NULL);
1212 if (h->root.type != bfd_link_hash_defined
1213 && h->root.type != bfd_link_hash_defweak)
1214 /* This appears to be a reference to an undefined
1215 symbol. Just ignore it--it will be caught by the
1216 regular reloc processing. */
1217 return 0;
1219 symval = (h->root.u.def.value
1220 + h->root.u.def.section->output_section->vma
1221 + h->root.u.def.section->output_offset);
1223 return symval;
1226 static int bytes_saved = 0;
1228 static int bytes_to_reloc[] = {
1229 R_M32C_NONE,
1230 R_M32C_8,
1231 R_M32C_16,
1232 R_M32C_24,
1233 R_M32C_32
1236 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1238 /* Mask for the number of relocs associated with this insn. */
1239 #define RLA_RELOCS 0x0000000f
1240 /* Number of bytes gas emitted (before gas's relaxing) */
1241 #define RLA_NBYTES 0x00000ff0
1243 /* If the displacement is within the given range and the new encoding
1244 differs from the old encoding (the index), then the insn can be
1245 relaxed to the new encoding. */
1246 typedef struct {
1247 int bytes;
1248 unsigned int max_disp;
1249 unsigned char new_encoding;
1250 } EncodingTable;
1252 static EncodingTable m16c_addr_encodings[] = {
1253 { 0, 0, 0 }, /* R0 */
1254 { 0, 0, 1 }, /* R1 */
1255 { 0, 0, 2 }, /* R2 */
1256 { 0, 0, 3 }, /* R3 */
1257 { 0, 0, 4 }, /* A0 */
1258 { 0, 0, 5 }, /* A1 */
1259 { 0, 0, 6 }, /* [A0] */
1260 { 0, 0, 7 }, /* [A1] */
1261 { 1, 0, 6 }, /* udsp:8[A0] */
1262 { 1, 0, 7 }, /* udsp:8[A1] */
1263 { 1, 0, 10 }, /* udsp:8[SB] */
1264 { 1, 0, 11 }, /* sdsp:8[FB] */
1265 { 2, 255, 8 }, /* udsp:16[A0] */
1266 { 2, 255, 9 }, /* udsp:16[A1] */
1267 { 2, 255, 10 }, /* udsp:16[SB] */
1268 { 2, 0, 15 }, /* abs:16 */
1271 static EncodingTable m16c_jmpaddr_encodings[] = {
1272 { 0, 0, 0 }, /* R0 */
1273 { 0, 0, 1 }, /* R1 */
1274 { 0, 0, 2 }, /* R2 */
1275 { 0, 0, 3 }, /* R3 */
1276 { 0, 0, 4 }, /* A0 */
1277 { 0, 0, 5 }, /* A1 */
1278 { 0, 0, 6 }, /* [A0] */
1279 { 0, 0, 7 }, /* [A1] */
1280 { 1, 0, 6 }, /* udsp:8[A0] */
1281 { 1, 0, 7 }, /* udsp:8[A1] */
1282 { 1, 0, 10 }, /* udsp:8[SB] */
1283 { 1, 0, 11 }, /* sdsp:8[FB] */
1284 { 3, 255, 8 }, /* udsp:20[A0] */
1285 { 3, 255, 9 }, /* udsp:20[A1] */
1286 { 2, 255, 10 }, /* udsp:16[SB] */
1287 { 2, 0, 15 }, /* abs:16 */
1290 static EncodingTable m32c_addr_encodings[] = {
1291 { 0, 0, 0 }, /* [A0] */
1292 { 0, 0, 1 }, /* [A1] */
1293 { 0, 0, 2 }, /* A0 */
1294 { 0, 0, 3 }, /* A1 */
1295 { 1, 0, 0 }, /* udsp:8[A0] */
1296 { 1, 0, 1 }, /* udsp:8[A1] */
1297 { 1, 0, 6 }, /* udsp:8[SB] */
1298 { 1, 0, 7 }, /* sdsp:8[FB] */
1299 { 2, 255, 4 }, /* udsp:16[A0] */
1300 { 2, 255, 5 }, /* udsp:16[A1] */
1301 { 2, 255, 6 }, /* udsp:16[SB] */
1302 { 2, 127, 7 }, /* sdsp:16[FB] */
1303 { 3, 65535, 8 }, /* udsp:24[A0] */
1304 { 3, 65535, 9 }, /* udsp:24[A1] */
1305 { 3, 65535, 15 }, /* abs24 */
1306 { 2, 0, 15 }, /* abs16 */
1307 { 0, 0, 16 }, /* R2 */
1308 { 0, 0, 17 }, /* R3 */
1309 { 0, 0, 18 }, /* R0 */
1310 { 0, 0, 19 }, /* R1 */
1311 { 0, 0, 20 }, /* */
1312 { 0, 0, 21 }, /* */
1313 { 0, 0, 22 }, /* */
1314 { 0, 0, 23 }, /* */
1315 { 0, 0, 24 }, /* */
1316 { 0, 0, 25 }, /* */
1317 { 0, 0, 26 }, /* */
1318 { 0, 0, 27 }, /* */
1319 { 0, 0, 28 }, /* */
1320 { 0, 0, 29 }, /* */
1321 { 0, 0, 30 }, /* */
1322 { 0, 0, 31 }, /* */
1325 static bfd_boolean
1326 m32c_elf_relax_section
1327 (bfd * abfd,
1328 asection * sec,
1329 struct bfd_link_info * link_info,
1330 bfd_boolean * again)
1332 Elf_Internal_Shdr *symtab_hdr;
1333 Elf_Internal_Shdr *shndx_hdr;
1334 Elf_Internal_Rela *internal_relocs;
1335 Elf_Internal_Rela *free_relocs = NULL;
1336 Elf_Internal_Rela *irel, *irelend, *srel;
1337 bfd_byte * contents = NULL;
1338 bfd_byte * free_contents = NULL;
1339 Elf_Internal_Sym *intsyms = NULL;
1340 Elf_Internal_Sym *free_intsyms = NULL;
1341 Elf_External_Sym_Shndx *shndx_buf = NULL;
1342 int machine;
1344 if (abfd == elf_hash_table (link_info)->dynobj
1345 && strcmp (sec->name, ".plt") == 0)
1346 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1348 /* Assume nothing changes. */
1349 *again = FALSE;
1351 machine = elf32_m32c_machine (abfd);
1353 /* We don't have to do anything for a relocatable link, if
1354 this section does not have relocs, or if this is not a
1355 code section. */
1356 if (link_info->relocatable
1357 || (sec->flags & SEC_RELOC) == 0
1358 || sec->reloc_count == 0
1359 || (sec->flags & SEC_CODE) == 0)
1360 return TRUE;
1362 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1363 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1365 /* Get the section contents. */
1366 if (elf_section_data (sec)->this_hdr.contents != NULL)
1367 contents = elf_section_data (sec)->this_hdr.contents;
1368 /* Go get them off disk. */
1369 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1370 goto error_return;
1372 /* Read this BFD's symbols. */
1373 /* Get cached copy if it exists. */
1374 if (symtab_hdr->contents != NULL)
1376 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1378 else
1380 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1381 symtab_hdr->contents = (bfd_byte *) intsyms;
1384 if (shndx_hdr->sh_size != 0)
1386 bfd_size_type amt;
1388 amt = symtab_hdr->sh_info;
1389 amt *= sizeof (Elf_External_Sym_Shndx);
1390 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1391 if (shndx_buf == NULL)
1392 goto error_return;
1393 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1394 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1395 goto error_return;
1396 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1399 /* Get a copy of the native relocations. */
1400 internal_relocs = (_bfd_elf_link_read_relocs
1401 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1402 link_info->keep_memory));
1403 if (internal_relocs == NULL)
1404 goto error_return;
1405 if (! link_info->keep_memory)
1406 free_relocs = internal_relocs;
1408 /* The RL_ relocs must be just before the operand relocs they go
1409 with, so we must sort them to guarantee this. */
1410 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1411 compare_reloc);
1413 /* Walk through them looking for relaxing opportunities. */
1414 irelend = internal_relocs + sec->reloc_count;
1416 for (irel = internal_relocs; irel < irelend; irel++)
1418 bfd_vma symval;
1419 unsigned char *insn, *gap, *einsn;
1420 bfd_vma pc;
1421 bfd_signed_vma pcrel;
1422 int relax_relocs;
1423 int gap_size;
1424 int new_type;
1425 int posn;
1426 int enc;
1427 EncodingTable *enctbl;
1428 EncodingTable *e;
1430 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1431 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1432 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1433 continue;
1435 srel = irel;
1437 /* There will always be room for the relaxed insn, since it is smaller
1438 than the one it would replace. */
1439 BFD_ASSERT (irel->r_offset < sec->size);
1441 insn = contents + irel->r_offset;
1442 relax_relocs = irel->r_addend % 16;
1444 /* Ok, we only have three relocs we care about, and they're all
1445 fake. The lower four bits of the addend is always the number
1446 of following relocs (hence the qsort above) that are assigned
1447 to this opcode. The next 8 bits of the addend indicates the
1448 number of bytes in the insn. We use the rest of them
1449 ourselves as flags for the more expensive operations (defines
1450 above). The three relocs are:
1452 RL_JUMP: This marks all direct jump insns. We check the
1453 displacement and replace them with shorter jumps if
1454 they're in range. We also use this to find JMP.S
1455 insns and manually shorten them when we delete bytes.
1456 We have to decode these insns to figure out what to
1459 RL_1ADDR: This is a :G or :Q insn, which has a single
1460 "standard" operand. We have to extract the type
1461 field, see if it's a wide displacement, then figure
1462 out if we can replace it with a narrow displacement.
1463 We don't have to decode these insns.
1465 RL_2ADDR: Similarly, but two "standard" operands. Note that
1466 r_addend may still be 1, as standard operands don't
1467 always have displacements. Gas shouldn't give us one
1468 with zero operands, but since we don't know which one
1469 has the displacement, we check them both anyway.
1471 These all point to the beginning of the insn itself, not the
1472 operands.
1474 Note that we only relax one step at a time, relying on the
1475 linker to call us repeatedly. Thus, there is no code for
1476 JMP.A->JMP.B although that will happen in two steps.
1477 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1480 /* Get the value of the symbol referred to by the reloc. Just
1481 in case this is the last reloc in the list, use the RL's
1482 addend to choose between this reloc (no addend) or the next
1483 (yes addend, which means at least one following reloc). */
1484 srel = irel + (relax_relocs ? 1 : 0);
1485 symval = OFFSET_FOR_RELOC (srel);
1487 /* Setting gap_size nonzero is the flag which means "something
1488 shrunk". */
1489 gap_size = 0;
1490 gap = NULL;
1491 new_type = ELF32_R_TYPE(srel->r_info);
1493 pc = sec->output_section->vma + sec->output_offset
1494 + srel->r_offset;
1495 pcrel = symval - pc + srel->r_addend;
1497 if (machine == bfd_mach_m16c)
1499 /* R8C / M16C */
1501 switch (ELF32_R_TYPE(irel->r_info))
1504 case R_M32C_RL_JUMP:
1505 switch (insn[0])
1507 case 0xfe: /* jmp.b */
1508 if (pcrel >= 2 && pcrel <= 9)
1510 /* Relax JMP.B -> JMP.S. We need to get rid of
1511 the following reloc though. */
1512 insn[0] = 0x60 | (pcrel - 2);
1513 new_type = R_M32C_NONE;
1514 irel->r_addend = 0x10;
1515 gap_size = 1;
1516 gap = insn + 1;
1518 break;
1520 case 0xf4: /* jmp.w */
1521 /* 128 is allowed because it will be one byte closer
1522 after relaxing. Likewise for all other pc-rel
1523 jumps. */
1524 if (pcrel <= 128 && pcrel >= -128)
1526 /* Relax JMP.W -> JMP.B */
1527 insn[0] = 0xfe;
1528 insn[1] = 0;
1529 new_type = R_M32C_8_PCREL;
1530 gap_size = 1;
1531 gap = insn + 2;
1533 break;
1535 case 0xfc: /* jmp.a */
1536 if (pcrel <= 32768 && pcrel >= -32768)
1538 /* Relax JMP.A -> JMP.W */
1539 insn[0] = 0xf4;
1540 insn[1] = 0;
1541 insn[2] = 0;
1542 new_type = R_M32C_16_PCREL;
1543 gap_size = 1;
1544 gap = insn + 3;
1546 break;
1548 case 0xfd: /* jsr.a */
1549 if (pcrel <= 32768 && pcrel >= -32768)
1551 /* Relax JSR.A -> JSR.W */
1552 insn[0] = 0xf5;
1553 insn[1] = 0;
1554 insn[2] = 0;
1555 new_type = R_M32C_16_PCREL;
1556 gap_size = 1;
1557 gap = insn + 3;
1559 break;
1561 break;
1563 case R_M32C_RL_2ADDR:
1564 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1566 enctbl = m16c_addr_encodings;
1567 posn = 2;
1568 enc = (insn[1] >> 4) & 0x0f;
1569 e = & enctbl[enc];
1571 if (srel->r_offset == irel->r_offset + posn
1572 && e->new_encoding != enc
1573 && symval <= e->max_disp)
1575 insn[1] &= 0x0f;
1576 insn[1] |= e->new_encoding << 4;
1577 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1578 gap = insn + posn + enctbl[e->new_encoding].bytes;
1579 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1580 break;
1582 if (relax_relocs == 2)
1583 srel ++;
1584 posn += e->bytes;
1586 goto try_1addr_16;
1588 case R_M32C_RL_1ADDR:
1589 /* xxxx xxxx xxxx dest [disp] */
1591 enctbl = m16c_addr_encodings;
1592 posn = 2;
1594 /* Check the opcode for jumps. We know it's safe to
1595 do this because all 2ADDR insns are at least two
1596 bytes long. */
1597 enc = insn[0] * 256 + insn[1];
1598 enc &= 0xfff0;
1599 if (enc == 0x7d20
1600 || enc == 0x7d00
1601 || enc == 0x7d30
1602 || enc == 0x7d10)
1604 enctbl = m16c_jmpaddr_encodings;
1607 try_1addr_16:
1608 /* srel, posn, and enc must be set here. */
1610 symval = OFFSET_FOR_RELOC (srel);
1611 enc = insn[1] & 0x0f;
1612 e = & enctbl[enc];
1614 if (srel->r_offset == irel->r_offset + posn
1615 && e->new_encoding != enc
1616 && symval <= e->max_disp)
1618 insn[1] &= 0xf0;
1619 insn[1] |= e->new_encoding;
1620 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1621 gap = insn + posn + enctbl[e->new_encoding].bytes;
1622 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1623 break;
1626 break;
1628 } /* Ends switch (reloc type) for m16c. */
1630 else /* machine == bfd_mach_m32c */
1632 /* M32CM / M32C */
1634 switch (ELF32_R_TYPE(irel->r_info))
1637 case R_M32C_RL_JUMP:
1638 switch (insn[0])
1640 case 0xbb: /* jmp.b */
1641 if (pcrel >= 2 && pcrel <= 9)
1643 int p = pcrel - 2;
1644 /* Relax JMP.B -> JMP.S. We need to get rid of
1645 the following reloc though. */
1646 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1647 new_type = R_M32C_NONE;
1648 irel->r_addend = 0x10;
1649 gap_size = 1;
1650 gap = insn + 1;
1652 break;
1654 case 0xce: /* jmp.w */
1655 if (pcrel <= 128 && pcrel >= -128)
1657 /* Relax JMP.W -> JMP.B */
1658 insn[0] = 0xbb;
1659 insn[1] = 0;
1660 new_type = R_M32C_8_PCREL;
1661 gap_size = 1;
1662 gap = insn + 2;
1664 break;
1666 case 0xcc: /* jmp.a */
1667 if (pcrel <= 32768 && pcrel >= -32768)
1669 /* Relax JMP.A -> JMP.W */
1670 insn[0] = 0xce;
1671 insn[1] = 0;
1672 insn[2] = 0;
1673 new_type = R_M32C_16_PCREL;
1674 gap_size = 1;
1675 gap = insn + 3;
1677 break;
1679 case 0xcd: /* jsr.a */
1680 if (pcrel <= 32768 && pcrel >= -32768)
1682 /* Relax JSR.A -> JSR.W */
1683 insn[0] = 0xcf;
1684 insn[1] = 0;
1685 insn[2] = 0;
1686 new_type = R_M32C_16_PCREL;
1687 gap_size = 1;
1688 gap = insn + 3;
1690 break;
1692 break;
1694 case R_M32C_RL_2ADDR:
1695 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1697 einsn = insn;
1698 posn = 2;
1699 if (einsn[0] == 1)
1701 /* prefix; remove it as far as the RL reloc is concerned. */
1702 einsn ++;
1703 posn ++;
1706 enctbl = m32c_addr_encodings;
1707 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1708 e = & enctbl[enc];
1710 if (srel->r_offset == irel->r_offset + posn
1711 && e->new_encoding != enc
1712 && symval <= e->max_disp)
1714 einsn[0] &= 0x8f;
1715 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1716 einsn[1] &= 0xcf;
1717 einsn[1] |= (e->new_encoding & 0x03) << 4;
1718 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1719 gap = insn + posn + enctbl[e->new_encoding].bytes;
1720 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1721 break;
1723 if (relax_relocs == 2)
1724 srel ++;
1725 posn += e->bytes;
1727 goto try_1addr_32;
1729 case R_M32C_RL_1ADDR:
1730 /* xxxx DDDx DDxx xxxx [disp] */
1732 einsn = insn;
1733 posn = 2;
1734 if (einsn[0] == 1)
1736 /* prefix; remove it as far as the RL reloc is concerned. */
1737 einsn ++;
1738 posn ++;
1741 enctbl = m32c_addr_encodings;
1743 try_1addr_32:
1744 /* srel, posn, and enc must be set here. */
1746 symval = OFFSET_FOR_RELOC (srel);
1747 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1748 e = & enctbl[enc];
1750 if (srel->r_offset == irel->r_offset + posn
1751 && e->new_encoding != enc
1752 && symval <= e->max_disp)
1754 einsn[0] &= 0xf1;
1755 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1756 einsn[1] &= 0x3f;
1757 einsn[1] |= (e->new_encoding & 0x03) << 6;
1758 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1759 gap = insn + posn + enctbl[e->new_encoding].bytes;
1760 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1761 break;
1764 break;
1766 } /* Ends switch (reloc type) for m32c. */
1769 if (gap_size == 0)
1770 continue;
1772 *again = TRUE;
1774 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1776 /* Note that we've changed the relocs, section contents, etc. */
1777 elf_section_data (sec)->relocs = internal_relocs;
1778 free_relocs = NULL;
1780 elf_section_data (sec)->this_hdr.contents = contents;
1781 free_contents = NULL;
1783 symtab_hdr->contents = (bfd_byte *) intsyms;
1784 free_intsyms = NULL;
1786 bytes_saved += gap_size;
1788 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1789 goto error_return;
1791 } /* next relocation */
1793 if (free_relocs != NULL)
1795 free (free_relocs);
1796 free_relocs = NULL;
1799 if (free_contents != NULL)
1801 if (! link_info->keep_memory)
1802 free (free_contents);
1803 /* Cache the section contents for elf_link_input_bfd. */
1804 else
1805 elf_section_data (sec)->this_hdr.contents = contents;
1807 free_contents = NULL;
1810 if (shndx_buf != NULL)
1812 shndx_hdr->contents = NULL;
1813 free (shndx_buf);
1816 if (free_intsyms != NULL)
1818 if (! link_info->keep_memory)
1819 free (free_intsyms);
1820 /* Cache the symbols for elf_link_input_bfd. */
1821 else
1823 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1826 free_intsyms = NULL;
1829 return TRUE;
1831 error_return:
1832 if (free_relocs != NULL)
1833 free (free_relocs);
1834 if (free_contents != NULL)
1835 free (free_contents);
1836 if (shndx_buf != NULL)
1838 shndx_hdr->contents = NULL;
1839 free (shndx_buf);
1841 if (free_intsyms != NULL)
1842 free (free_intsyms);
1843 return FALSE;
1846 /* Delete some bytes from a section while relaxing. */
1848 static bfd_boolean
1849 m32c_elf_relax_delete_bytes
1850 (bfd * abfd,
1851 asection * sec,
1852 bfd_vma addr,
1853 int count)
1855 Elf_Internal_Shdr *symtab_hdr;
1856 Elf_Internal_Shdr *shndx_hdr;
1857 int sec_shndx;
1858 bfd_byte *contents;
1859 Elf_Internal_Rela *irel;
1860 Elf_Internal_Rela *irelend;
1861 Elf_Internal_Rela *irelalign;
1862 bfd_vma toaddr;
1863 Elf_Internal_Sym *isym;
1864 Elf_Internal_Sym *isymend;
1865 Elf_Internal_Sym *intsyms;
1866 Elf_External_Sym_Shndx *shndx_buf;
1867 Elf_External_Sym_Shndx *shndx;
1868 struct elf_link_hash_entry ** sym_hashes;
1869 struct elf_link_hash_entry ** end_hashes;
1870 unsigned int symcount;
1872 contents = elf_section_data (sec)->this_hdr.contents;
1874 /* The deletion must stop at the next ALIGN reloc for an aligment
1875 power larger than the number of bytes we are deleting. */
1876 irelalign = NULL;
1877 toaddr = sec->size;
1879 irel = elf_section_data (sec)->relocs;
1880 irelend = irel + sec->reloc_count;
1882 /* Actually delete the bytes. */
1883 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1884 sec->size -= count;
1886 /* Adjust all the relocs. */
1887 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1889 /* Get the new reloc address. */
1890 if (irel->r_offset > addr && irel->r_offset < toaddr)
1891 irel->r_offset -= count;
1893 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1894 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1895 && irel->r_offset + 1 < addr
1896 && irel->r_offset + 7 > addr)
1898 bfd_vma disp;
1899 unsigned char *insn = &contents[irel->r_offset];
1900 disp = *insn;
1901 /* This is a JMP.S, which we have to manually update. */
1902 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1904 if ((*insn & 0xf8) != 0x60)
1905 continue;
1906 disp = (disp & 7);
1908 else
1910 if ((*insn & 0xce) != 0x4a)
1911 continue;
1912 disp = ((disp & 0x30) >> 3) | (disp & 1);
1914 if (irel->r_offset + disp + 2 >= addr+count)
1916 disp -= count;
1917 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1919 *insn = (*insn & 0xf8) | disp;
1921 else
1923 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1929 /* Adjust the local symbols defined in this section. */
1930 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1931 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1932 isym = intsyms;
1933 isymend = isym + symtab_hdr->sh_info;
1935 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1936 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1937 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1938 shndx = shndx_buf;
1940 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1943 if ((int) isym->st_shndx == sec_shndx
1944 && isym->st_value > addr
1945 && isym->st_value < toaddr)
1947 isym->st_value -= count;
1951 /* Now adjust the global symbols defined in this section. */
1952 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1953 - symtab_hdr->sh_info);
1954 sym_hashes = elf_sym_hashes (abfd);
1955 // sym_hashes += symtab_hdr->sh_info;
1956 end_hashes = sym_hashes + symcount;
1958 for (; sym_hashes < end_hashes; sym_hashes ++)
1960 struct elf_link_hash_entry * sym_hash = * sym_hashes;
1962 if (sym_hash &&
1963 ( sym_hash->root.type == bfd_link_hash_defined
1964 || sym_hash->root.type == bfd_link_hash_defweak)
1965 && sym_hash->root.u.def.section == sec
1966 && sym_hash->root.u.def.value > addr
1967 && sym_hash->root.u.def.value < toaddr)
1969 sym_hash->root.u.def.value -= count;
1973 return TRUE;
1977 #define ELF_ARCH bfd_arch_m32c
1978 #define ELF_MACHINE_CODE EM_M32C
1979 #define ELF_MAXPAGESIZE 0x1000
1981 #if 0
1982 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
1983 #define TARGET_BIG_NAME "elf32-m32c"
1984 #else
1985 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
1986 #define TARGET_LITTLE_NAME "elf32-m32c"
1987 #endif
1989 #define elf_info_to_howto_rel NULL
1990 #define elf_info_to_howto m32c_info_to_howto_rela
1991 #define elf_backend_object_p m32c_elf_object_p
1992 #define elf_backend_relocate_section m32c_elf_relocate_section
1993 #define elf_backend_check_relocs m32c_elf_check_relocs
1994 #define elf_backend_object_p m32c_elf_object_p
1995 #define elf_symbol_leading_char ('_')
1996 #define elf_backend_always_size_sections \
1997 m32c_elf_always_size_sections
1998 #define elf_backend_finish_dynamic_sections \
1999 m32c_elf_finish_dynamic_sections
2001 #define elf_backend_can_gc_sections 1
2003 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2004 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2005 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2006 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2007 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2009 #include "elf32-target.h"